开发者

Problems with garbage characters when reading file [closed]

开发者 https://www.devze.com 2023-01-23 12:14 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I'm having trouble reading data from a file, and concatenating selected parts of the data (text) into a buffer of my own.

The code is like follows:

 char buffer[1000];
  char* allNewData = (char *)malloc(10000);

  while (! myfile.eof() )
  {
   myfile.getline (buffer, 1000);
   pch = strstr (buffer,"bla bla");
   if(pch == NULL)
   {
    char* temp = buffer;
    strcat(allNewData, temp);
    strcat(allNew开发者_开发技巧Data, "\n");
   }
   else
   {
    strcat(allNewData, "here's bla bla");
    strcat(allNewData, "\n");
   }
  }

  cout<<allNewData<<endl;

When I run the program, allNewData first has some garbage text, followed by the proper/expected results, like this:

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii <-rubbish data
hello  <- actual data

I need to get rid of this rubbish data, how can I change the code to achieve this?


You need to clear your newly allocated buffer before using string concatenation functions. They expect a valid string, to be able to find the end and thus the start of where to concatenate.

Use:

allNewData[0] = '\0';

this makes allNewData into an empty string. Do this before the loop, before you start concatenating all the found data.

Also, your code needs to better take care of the various "gotchas" when it comes to I/O and handling memory:

  • Don't check for EOF before doing a read access.
  • Check that the read was successful, before using the results of the read.
  • Make sure you don't exceed the capacity of your buffer when storing data.


Some comments, which you may find helpful or disregard:

  1. What if there is a line longer than 1000 characters? (and say, that 1001-1008 is 'blah blah')? The line will be split into two in your new file and there will be an extra line before "here's blah blah"? Is this now a bug or desired functionality?
  2. What if the line is longer than 1000, but "blah" is 996-1000 and the second "blah" is on the second segment - now you've lost one
  3. What if your file is longer than 10000 characters?

They may sound like trivial questions, but answering them correctly will mean that you'll have to change your approach, I suggest purer C++ approach:

  ifstream f_in(<file>);
  ostringstream s_out;
  string line;
  while(f_in.good())
  {
    getline(f_in, line); // global getline function in <string>
    if (line.find("blah blah") != string::npos)
    {
      s_out << "here's blah blah" << endl;
    }
    else
    {
      s_out << line << endl;
    }
  }

This way you don't have to worry about any of the questions above...


You can also use a combination of getline and ignore


Again... you have to check that your IO operation don't fail and eof() should be used only after a failed IO operation.

0

精彩评论

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