im not sure where my bug is but something is acting really weird in here
Code:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main()
{
char totalcontents[10000]="";
char FileContents[500], WordToDelete[100], WordToInsert[100], StoreFileContents[500];
char filename[100];
cout<<"Enter the name of file: ";
cin>>filename;
cout<<"Enter word to search for: ";
cin>>WordToDelete;
cin.ignore(20,'\n');
cout<<"Enter word to replace it with: ";
cin.getline(WordToInsert,20);
fstream file;
file.open(filename,ios::nocreate|ios::in|ios::out);
if(file.fail()){
cout<<"File could not be opened"<<endl;
return (1);
}
while(file.getline(FileContents,500,'\n'))
{
int length1=strlen(FileContents);
int length2=strlen(WordToDelete);
int length3=strlen(WordToInsert);
int count,count2,count3,count4=0,count5=0;
for(count=0;count<=length1;count++)
{
if(FileContents[count]==WordToDelete[0])
{
for(count2=count;count2<=(count+length2);count2++)
{
//if(FileContents[count2]!=WordToDelete[count2-count])
// break;
if(count2==(count+length2)){
strcpy(StoreFileContents,FileContents);
if(length2==length3)
{
for(count3=count;count3<(count+length2);count3++)
{
FileContents[count3]=WordToInsert[count3-count];
}
}
else if(length2<length3)
{
for(count3=count;count3<(count+length3);count3++)//replace original
{
FileContents[count3]=WordToInsert[count3-count];
}
length1=(length3-length2)+length1;
for(count4=count3;count4<length1;count4++)
{
FileContents[count4]=StoreFileContents[count4-1];
}
}
else if(length2>length3)
{
for(count3=count;count3<(count+length3);count3++)//replace original
{
FileContents[count3]=WordToInsert[count3-count];
}
length1=(length3-length2)+length1;
for(count4=count3;count4<length1;count4++)
{
FileContents[count4]=StoreFileContents[count4+1];
}
FileContents[length1]=' ';
}
}
}
}
}
//here we put the thingy back in
strcat(totalcontents,FileContents);
}
file.close();
file.open(filename,ios::trunc|ios::in|ios::out);
if(file.fail()){
cout<<"File could not be opened"<<endl;
return (1);
}
file<<totalcontents;
file.close();
return (0);
}