开发者

Reading a text document character by character

开发者 https://www.devze.com 2023-01-05 16:48 出处:网络
I am reading a text file character by character using ifstream infile.get() in an infinite while loop.

I am reading a text file character by character using ifstream infile.get() in an infinite while loop.

This sits inside an infinite while loop, and should break out of it once the end of file condition is reached. (EOF). The while loop itself sits within a function of type void.

Here is the pseudo-code:

void function (...) {
    while(true) {
        ...
        if ( (ch = infile.get()) == EOF) {return;}
        ...
    }
}

When I "cout" characters on the screen, it goes through all the character and then keeps running outputting what appears as blank spac开发者_如何学JAVAe, i.e. it never breaks. I have no idea why. Any ideas?


In C++, you don't compare the return value with EOF. Instead, you can use a stream function such as good() to check if more data can be read. Something like this:

while (infile.good()) {
  ch = infile.get();
  // ...
}


One idiom that makes it relatively easy to read from a file and detect the end of the file correctly is to combine the reading and the testing into a single, atomic, event, such as:

while (infile >> ch)

or:

while (std::getline(infile, instring))

Of course, you should also consider using a standard algorithm, such as copy:

std::copy(std::istream_iterator<char>(infile),
          std::istream_iterator<char>(),
          std::ostream_itertror<char>(std::cout, "\n"));

One minor note: by default, reading with >> will skip white space. When you're doing character-by-character input/processing, you usually don't want that. Fortunately, disabling that is pretty easy:

infile.unsetf(std::ios_base::skipws);


try converting the function to an int one and return 1 when reaching EOF


The reason it is not working is that get() returns an int but you are using the input as a char.

When you assign the result of get() to a char it is fine as long as the last character read was a character. BUT if the last character read was a special character (such as EOF) then it will get truncated when assigned to a char and thus the subsequent comparison to EOF will always fail.

This should work:

void function (...) 
{
    while(true) 
    {
        ...
        int value;
        if ( (value = infile.get()) == EOF) {return;}
        char ch = value; 
        ...
    }
}

But it should be noted that it is a lot easier to use the more standard pattern where the read is done as part of the condition. Unfortunately the get() does not give you that functionality. So we need to switch to a method that uses iterators.

Note the standard istream_iterator will not work as you expect (as it ignores white space). But you can use the istreambuf_iterator (notice the buf after istream) which does not ignore white space.

void function (...) 
{
    for(std::istreambuf_iterator<char> loop(infile);
        loop != std::istreambuf_iterator<char>();
        ++loop) 
    {
        char ch = *loop; 
        ...
    }
}
0

精彩评论

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

关注公众号