Advertising (This ad goes away for registered users. You can Login or Register)

text file search and write...

Programming on your favorite platform, for your favorite platform? Post here
Locked
varun
Banned
Posts: 651
Joined: Thu Jan 27, 2011 12:32 pm
Location: Mars

text file search and write...

Post by varun »

i was making a program that reads a text file(2of12.txt) word by word, searches it in second one(all_dict.txt)..if not found it pastes it into second one....the problem is that it only pastes 1st letter of each word plz help.

Code: Select all

void main()
{	ifstream myReadFile,x2;
	ofstream myWriteFile;
	char output[100],s[100];
	myReadFile.open("2of12.txt");
	x2.open("all_dict.txt");
	
	int i,flag;
	for( i = 0; i<100; i++, output[i] = '\0',s[i]= '\0');
	
	if (myReadFile.is_open() && x2.is_open()) 
	{	
		while(!myReadFile.eof())
		{	
			flag = 0;
			myReadFile >> output;
			while(!x2.eof())
			{	for( i = 0; i<100; i++, output[i] = '\0',s[i]= '\0');
				x2>>s;
				if(strcmp(s,output)==0) flag = 1;
			}
			x2.seekg(0,ios::beg); //reset read pos to beginning
			x2.clear(); //clear eofbit
			
			if(flag==0)
			{
				
				myWriteFile.open ("all_dict.txt",ios_base::app);
				myWriteFile<<output<<endl;
				myWriteFile.close();
				myWriteFile.clear(); //clear eofbit
			
			}
				
		}
	}

	myReadFile.seekg(0,std::ios::beg); //reset read pos to beginning
	myReadFile.clear(); //clear eofbit
	myReadFile.close();

	


}
also after some time i get this error

Code: Select all

Run-Time Check Failure #2 - Stack around the variable 's' was corrupted.
plz help
Advertising
Attachments
text.rar
(120.63 KiB) Downloaded 169 times
psPea
Posts: 42
Joined: Sat May 21, 2011 7:04 pm

Re: text file search and write...

Post by psPea »

Code: Select all

         while(!x2.eof())
         {   for( i = 0; i<100; i++, output[i] = '\0',s[i]= '\0');
            x2>>s;
            if(strcmp(s,output)==0) flag = 1;
         }
Your for loop is erasing all but the first character in output
I'm assuming you're using it to clear the buffers, which is not achieved
you should use memset or some c++ equivalent instead.
Advertising
Locked

Return to “Programming”