Solved

what's wrong with my code?

Posted on 1998-07-15
6
184 Views
Last Modified: 2010-04-10
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,sizeof(largebuf));
          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
}
0
Comment
Question by:tetsuo
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1167844
istream& getline( char* pch, int nCount, char delim = '\n' );

Your getline() only reads up to the next '\n'.  Add a delimiter ('\0') instead of using the default one.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1167845
What do you mean Vanish? I run your code, it seems everything is OK.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167846
Each iteration reads another line and overrites the previous one.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1167847
while (!filebuf.eof()) {
    filebuf.getline(largebuf,sizeof(largebuf)); // This reads one line.  Next iteration overrites it
    cout<<largebuf;
}

0
 
LVL 8

Expert Comment

by:trestan
ID: 1167848
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.
0
 

Author Comment

by:tetsuo
ID: 1167849
thanx!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question