Solved

what's wrong with my code?

Posted on 1998-07-15
6
181 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

  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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now