Hello

I need some help as to the limitations or flexibility of header files. I have written a code that updates a base folder when it is compared to its newer version. I have hit on a roadblock though.

If a new file is added to the folder, the program throws up an error. I plan to create a header file, which I can include into the program, which contains a function that will compare the file names in both files and in case a file doesn't exist in folder1 but is present in folder2, it copies the file to folder1.

I plan on using the replace and rename commands again, but am unsure of using them in a .h file. Is it okay to define functions in the header file?

Please input your suggestions on how to work around this hitch. Any examples are welcome!

Here is my actual program, just in case:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <cstdlib>
using namespace std;



int main()
{
	
	
	string Line1,Line2;
	int check=0,i=0,j=0;

				//finding files in the folders entered
	string FolderPath1,FolderPath2,SearchPattern1,SearchPattern2,FullSearchPath1,FullSearchPath2;
	cout<<"enter path of folder1, ex:C:\\folder1\\folder2\\ :\t";
	cin>>FolderPath1;
	cout<<"enter path of folder2, ex:C:\\folder1\\folder2\\ :\t";
	cin>>FolderPath2;
	cout<<"enter extension type of files to load in folder1, ex: *.txt:\t";
	cin>>SearchPattern1;
	cout<<"enter extension type of files to load in folder2, ex: *.txt:\t";
	cin>>SearchPattern2;

	FullSearchPath1=FolderPath1+SearchPattern1;//full path of files stored
	FullSearchPath2=FolderPath2+SearchPattern2;

	

	WIN32_FIND_DATA FindData1; //defines FindData1 as member of WIN32_FIND_DATA structure
    WIN32_FIND_DATA FindData2; //defines FindData2 as member of WIN32_FIND_DATA structure
	HANDLE hFind1;//establishes a search handle for first folder
	HANDLE hFind2;//establishes a search handle for second folder

	hFind1 = FindFirstFile( FullSearchPath1.c_str(), &FindData1 );//find first file in folder 1
	hFind2 = FindFirstFile( FullSearchPath2.c_str(), &FindData2 );//find first file in folder 2

	if( hFind1 == INVALID_HANDLE_VALUE || hFind2==INVALID_HANDLE_VALUE ) //error check
	{        
		cout << "Error searching folder1/folder2\n";     
		return -1;   
	}
	
	do//iterate through all files in the folder defined
	{
		string output = FolderPath1+"Report.txt"; //creating tempfile to be used for each file 
		ofstream out(output.c_str());

		string filePath1 = FolderPath1 + FindData1.cFileName;//storing complete file path of file1
		string filePath2 = FolderPath2 + FindData2.cFileName;//storing complete file path of file2
		
		//cin.get();
		string File1=FindData1.cFileName;//storing filenames in string
		string File2=FindData2.cFileName;
		cout<<"Processing "<<File1<<" and "<<File2<<endl;
		

		
		if (stricmp(File1.c_str(),File2.c_str())==0)//only if filenames are same, further processing done
		{
			
		
			ifstream Ifile1(filePath1.c_str()); //create ifstream object for each file in folder
			ifstream Ifile2(filePath2.c_str());
					

	
	
			while (getline(Ifile2,Line2)) //comparison starts here
			{
				i=0;//variable used for checking for matches
				j=0;//variable used for 
			
				if (Ifile1.eof())//if eof is reached
				{
					Ifile1.clear();
					Ifile1.seekg(0,ios::beg);//reset to beginning of file
				}
				else
				{
					Ifile1.seekg(0,ios::cur);//read from current position, time saving measure
				}
		
				while(getline(Ifile1,Line1))//loop iterates till eof
																			
	
				{
												
					check=Line1.compare(Line2);//comparison of strings
					if(check==0)//strings same
					{
						i++;//counts number of matches
						j=1;//this variable helps in determination of whether updates is required      												
						break;//break once a match occurs, time saving measure
					}
						
			
				}
			
				if(i==0)
				{
					out<<Line2<<endl;//outputs line if i equals 0, i.e. no matches found
					
				
				}
				if(j==1)
				{
					out<<Line1<<endl;//because j is 1, line is original file is printed as it is in tempfile.No update or change is made
				}
			
				
			}

			
		}
		
		
		else//condition for file names not matching, no need to process
		{
			cout<<"filenames do not match, press enter";
			cin.get();
			exit(-1);
		}
		
		out.flush();//flushes the o/p buffer, prints contents to tempfile
		out.close();//closes tempfile
		remove(filePath1.c_str());//removes original file i.e.file1
		rename(output.c_str(),filePath1.c_str());//renames tempfile to file1 and moves to original location
				
		
	}
	while(FindNextFile(hFind1, &FindData1) > 0 && FindNextFile(hFind2,&FindData2) > 0);//next file in folder

	
return 0;
}

Hello

I need some help as to the limitations or flexibility of header files. I have written a code that updates a base folder when it is compared to its newer version. I have hit on a roadblock though.

If a new file is added to the folder, the program throws up an error.

I'm sorry, but what is it about new people that think that just telling us "an error happened" is useful? With 1000 possible errors, you think we're psychic? :icon_rolleyes: Next time tell your mechanic "my car makes a noise" and see if he fixes it. <<annoyed>>

I plan to create a header file, which I can include into the program, which contains a function that will compare the file names in both files and in case a file doesn't exist in folder1 but is present in folder2, it copies the file to folder1.

I plan on using the replace and rename commands again, but am unsure of using them in a .h file. Is it okay to define functions in the header file?

No it's not. the only thing that goes into header files are declarations -- things that don't use any space at all.

Function prototypes, constants, defines, externs are OK. Variables, arrays, and functions are not.

By putting definitions in the header, if you use the header in 2 modules, you will have 2 definitions of the same item -- a definite no-no.

Hello

The program exits with '-1'.

Okay, so headers cannot be used. May I know any other way to do it?

Thanks

Okay, so headers cannot be used. May I know any other way to do it?

Please ask a full question. It's too hard to piece together what you want to know by picking and choosing from your last post. Be specific.

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.