Hello everyone, I just made a .cpp file to modify a data file in .txt (not relevant actually) file. I basically want to apply what this program is doing to all the .txt files in a ./input folder , and I couldn't find anything relevant so far.

The code is the following :

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

int main () 
	{//0
	long long memory[100000];
	int i,index,one=0,two=0,three=0,four=0,five=0,six=0;
	string line;
	ifstream input_file ("example.txt");
	ofstream output_file ("example2.txt");
	if (input_file.is_open())
		{//1
		while ( input_file.good() )
			{//2
			index=0;
			getline (input_file,line);
			for(i=0;line[i];++i)
				{//3
				if(line[i]=='=' && line[i+1]=='S' && line[i+12]==' ' && line[i+13]=='1' )
					{//4
					one=1;
					memory[index]=1;
					}//4
				if(line[i]=='=' && line[i+1]=='S' && line[i+12]==' ' && line[i+13]=='2' )
					{//5
					two=1;
					memory[index]=2;
					}//5
				if(line[i]=='=' && line[i+1]=='S' && line[i+12]==' ' && line[i+13]=='3' )
					{//6
					three=1;
					memory[index]=3;
					}//6
				if(line[i]=='=' && line[i+1]=='S' && line[i+12]==' ' && line[i+13]=='4' )
					{//7
					four=1;
					memory[index]=4;
					}//7
				if(line[i]=='=' && line[i+1]=='S' && line[i+12]==' ' && line[i+13]=='5' )
					{//8
					five=1;
					memory[index]=5;
					}//8
				if(line[i]=='=' && line[i+1]=='S' && line[i+12]==' ' && line[i+13]=='6' )
					{//9
					six=1;
					memory[index]=6;
					}//9
				}//3
			
			if(one || two)  
				output_file <<"---------------------------------------------"<<endl<< line <<"---------------------------------------------" << endl ;
			if(!(one || two || three || four || five || six)) 
				output_file << line << endl;
			if(three || four || five || six)
				output_file <<"**********************************"<<endl<< line <<"**********************************" << endl ;
			one=0;
			two=0;
			three=0;
			four=0;
			five=0;
			six=0;
			index=index+1;
			}//2
		input_file.close();
  		}//1
	else cout << "Unable to open file";
	return 0;
	}//0

Recommended Answers

All 3 Replies

Standard C++ doesn't support directory management, you'll need to use some sort of non-standard library. I'd suggest Boost::filesystem as a start.

I see, I was kind of expecting that :(
so if I have for example

ifstream input_file ("example.txt");
       	if (input_file.is_open())
        {
        // modify one single file here
	input_file.close();        
        }

How can I quickly modify it (using boost::filesystem for example) to just go through all .txt files in ./input folder ?

Create a batch file that executes the program on each .txt file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.