tetsuo
asked on
what's wrong with my code?
there is something wrong with my code, but i can't figure out what it is. the following funciton is supposed to read in the
autoexec.bat file into a character array called largebuf. the problem seems to be occurring in th esecond while loop. it attempts to find the end of the text in the buffer, but then the contents in the buffer vanish! what's going on?
void writeAutoexec(int mode,int x) {
char largebuf[1500];
//char smallbuf[50];
int j=1;
fstream filebuf ("c:\\autoexec.bat", ios::in | ios::out);
if (mode==1) {
while (!filebuf.eof()) { //read the file into largebuf
filebuf.getline(largebuf,s izeof(larg ebuf));
cout<<largebuf;
}
while (largebuf[j]!=NULL) { //find the end of the buffer
cout<<'\n'<<largebuf[j++];
cout<<j;
cout<<largebuf; }
for (int i=j;i > x+1;i--) { //this should insert 2 spaces for the 16 to be inserted
largebuf[i] = largebuf[i-1]; //that way, the change can be made no matter where the path statement is
largebuf[i-1] = largebuf[i-2]; //in the document
}
largebuf[x+8] = '1'; //the line change
largebuf[x+9] = '6';
cout<<largebuf;
filebuf.close();
} //end if
}
autoexec.bat file into a character array called largebuf. the problem seems to be occurring in th esecond while loop. it attempts to find the end of the text in the buffer, but then the contents in the buffer vanish! what's going on?
void writeAutoexec(int mode,int x) {
char largebuf[1500];
//char smallbuf[50];
int j=1;
fstream filebuf ("c:\\autoexec.bat", ios::in | ios::out);
if (mode==1) {
while (!filebuf.eof()) { //read the file into largebuf
filebuf.getline(largebuf,s
cout<<largebuf;
}
while (largebuf[j]!=NULL) { //find the end of the buffer
cout<<'\n'<<largebuf[j++];
cout<<j;
cout<<largebuf; }
for (int i=j;i > x+1;i--) { //this should insert 2 spaces for the 16 to be inserted
largebuf[i] = largebuf[i-1]; //that way, the change can be made no matter where the path statement is
largebuf[i-1] = largebuf[i-2]; //in the document
}
largebuf[x+8] = '1'; //the line change
largebuf[x+9] = '6';
cout<<largebuf;
filebuf.close();
} //end if
}
What do you mean Vanish? I run your code, it seems everything is OK.
Each iteration reads another line and overrites the previous one.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sure, if you want to treat data to the every line you read in, you should put all the codes in the first while loop.
ASKER
thanx!
Your getline() only reads up to the next '\n'. Add a delimiter ('\0') instead of using the default one.