2 years ago

#71970

test-img

JAJA

fstream read retuns additional characters

Say we have this example file.xml

<?xml version="1.0" encoding="utf-8"?>
<root>
    <something>yes</something>
    <something2>no</something2>
</root>

Now if we try to read it using this piece of code:

int main() {
    fstream file;
    file.open("test.xml", fstream::in);
    const int GIGA_SIZE = 65535;
    char buffer[GIGA_SIZE + 1] = {};
    file.read(buffer, GIGA_SIZE);
}

What we get in the buffer is this:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <something>yes</something>
    <something2>no</something2>
</root>
ot>

From where those additional characters keep coming from? In the documentation it is stated that istream::read after reaching eof should only extract characters read up to that point. Buffer is initialized with '\0', even I added line buffer[111] = '\0' where 110 is the amount of characters the file have. Problem still occurs. What is interesting when we change the code to this:

int main() {
    fstream file;
    file.open("test.xml", fstream::in);
    const int GIGA_SIZE = 65535;
    char buffer[GIGA_SIZE + 1] = {};

    int i = 0;
    while (!file.eof()) {
        file.read(&buffer[i], 1);
        ++i;
    }
}

Then the file is read properly, without additional "ot>". I'm using c++17 on Visual Studio 2017

c++

c++17

fstream

0 Answers

Your Answer

Accepted video resources