Solved

what's wrong with my code?

Posted on 1998-07-15
6
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.

724 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