Solved

what's wrong with my code?

Posted on 1998-07-15
6
183 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 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