简体   繁体   中英

Difference in reading newlines using std::ifstream and getc

If I read in a file using this:

std::string readfile(const std::string &filename)
{
    std::ifstream t(filename);
    std::string str;
    str.assign(std::istreambuf_iterator<char>(t),
               std::istreambuf_iterator<char>());
    return str;
}

I find that the returned string has newlines which differ from the file's actual content. The file uses \\r\\n , whereas the returned string only contains \\n .

I confirmed this by using an old c-style function:

std::string readfile_c(const std::string &filename)
{
    FILE *f = fopen(filename.c_str(), "rb");
    std::string str;
    char c;
    while ((c = getc(f)) != EOF)
        str.push_back(c);
    fclose(f);
    return str;
}

Trying them both out:

int main(int argc, char **argv)
{
    const std::string filename = "work\\actionlabel.html";
    std::string content1(readfile(filename));
    std::string content2(readfile_c(filename));
}

gives me this:

调试器

As shown, content1 (from readfile ) has only \\n newlines, and content2 (from readfile_c ) has the file's actual newlines \\r\\n .

Why is there a difference?

Streams default to text mode in which the content of the file may not match the content seen by the stream. For example in Windows, a disk file contains \\r\\n line endings but the C++ text stream will only see \\n for the line endings. This feature helps in writing portable code that has to deal with differing OS conventions.

In binary mode it is intended that the file content, exactly matches the content seen by the stream.

In the code fopen(filename.c_str(), "rb"); you specified binary mode, but in your ifstream you did not; giving you text mode.

To get the same behaviour in both cases you could either use "r" for fopen , giving text mode in both cases, or use std::ios::binary as second argument to ifstream , giving binary mode in both cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM