开发者

Ofstream writing the wrong thing into a file

开发者 https://www.devze.com 2022-12-16 12:28 出处:网络
Hey guys, I couldn\'t really think of what to call this error in the title, so here goes. I\'m starting an assignment where I have to read the contents of a file, perform some calculations and write

Hey guys, I couldn't really think of what to call this error in the title, so here goes.

I'm starting an assignment where I have to read the contents of a file, perform some calculations and write the contents + the new calculations to a file.

I wrote the code to read in the file, and to right away write it into an output file to test if the read happened correctly. As I did that, I see ofstream writing my "filename" string(used to ask the user for the name of the file to open) into the file in random places, and there is no mention of it in the code.

Here is my code:

   #include <string>
    #include <fstream>
    #include <vector>
    #include <iostream>
    using namespace std;

    int main()  
    {
     char filename[256] = "";
     char currentLine[256] = ""; 

     cout << "Please enter the name of the input file: " << endl;
     cin.getline(filename,256);

     vector <string> storage;//disregard for now

     ifstream infile;
     infile.open(filename);

     string outputFile = ".output";
     outputFile = filename + outputFile;

     ofstream outfile(outputFile.c_str());

     string line = "";
     while(!infile.eof())
     {
      infile.read(currentLine, 256);
      line = currentLine;
      storage.push_back(line); //disregard for now
      outfile << line; //testing to see if it read properly
     }
    } 

Here is the input text:

1034 BLUE ELECTRIC FROBULATOR 5 1026

1039 GREEN ELECTRIC FROBULATOR 10 1026

1054 BLUE ELECTRIC DEFROBULATOR (MAGNESIUM COATING) 7 2000

1069 JELLO HAMMER V2 111 12

1050 BELL SILENCER 0 50

1090 SNAKE OIL 34 150

1070 MECHAGODZILLA COSTUME (PINK) 1 5000

1090 REFROBULATOR 3 9999

1091 REFROBULATOR REFILL (PACK OF 5) 1 4999

1092 REFROBULATOR REFILL (PACK OF 10) 1 8999

2003 FROBULATION TODAY (MAR) 4 5

2004 FROBULATION TODAY (APR) 9 5

2005 FROBULATION TODAY (MAY) 2 5

3102 FROBULATOR CUSTOMER WARRANTY 2YR 3 199

3103 FROBULATOR CUSTOMER WARRANTY 3YR 3 299

and here is the output text:

1034 BLUE ELECTRIC FROBULATOR 5 1026

1039 GREEN ELECTRIC FROBULATOR 10 1026

1054 BLUE ELECTRIC DEFROBULATOR (MAGNESIUM COATING) 7 2000

1069 JELLO HAMMER V2 111 12

1050 BELL SILENCER 0 50

1090 SNAKE OIL 34 150

1070 MECHAGODZILLA COSTUME (PINK) 1 5000

1090 Rinvoice.txtEFROBULATOR 3 9999

1091 REFROBULATOR REFILL (PACK OF 5) 1 4999

1092 REFROBULATOR REFILL (PACK OF 10) 1 8999

200开发者_开发问答3 FROBULATION TODAY (MAR) 4 5

2004 FROBULATION TODAY (APR) 9 5

2005 FROBULATION TODAY (MAY) 2 5

3102 FROBULATOR CUSTOMER WARRANTY 2YR 3 199

3103 invoice.txtFROBULATOR CUSTOMER WARRANTY 3YR 3 299

FILL (PACK OF 5) 1 4999

1092 REFROBULATOR REFILL (PACK OF 10) 1 8999

2003 FROBULATION TODAY (MAR) 4 5

2004 FROBULATION TODAY (APR) 9 5

2005 FROBULATION TODAY (MAY) 2 5

3102 FROBULATOR CUSTOMER WARRANTY 2YR 3 199

3103 invoice.txt

As you can see the "invoice.txt" shouldn't be in the output. Now what am I missing?


The correct way to read a file line by line is:

string line;
while( getline( file, line ) ) {
   // do something with line
}

For why this is so, you might want to take a look at this blog post of mine.


I think your problem is here:

infile.read(currentLine, 256);

because I note that read

is an unformatted input function and what is extracted is not stored as a c-string format, therefore no ending null-character is appended at the end of the character sequence.

So when you go to copy currentLine, the copy walks right off the end into whatever else is in memory there, which happens to be filename.

0

精彩评论

暂无评论...
验证码 换一张
取 消