Solved

what's wrong with my code?

Posted on 1998-07-15
6
180 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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