1

As the title says I'm trying to read a null terminated string from a binary file.

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string result = "";
    char ch;
    while (stream.get(ch) != '\0') {
        result += ch;
    }
    return result; }

My null character is '\0'

But any time I call the method it reads to the end of the file

std::ifstream myFile(file, std::ios_base::in | std::ios_base::binary);
myFile.seekg(startByte);

this->name = ObjElement::ReadStringFromStream(myFile);

Any idea what I'm doing wrong here?

6
  • You don't need to provide ios::in or set empty string to "" explicitly. Commented Aug 8, 2014 at 14:58
  • 1
    stream.get(ch) doesn't return the character. Commented Aug 8, 2014 at 14:59
  • im not reading from the start of the file, I have the start byte I need from somewhere else. Commented Aug 8, 2014 at 15:00
  • Are you sure the file contains the ASCII Zero character in it? How did you get/create the file? Commented Aug 8, 2014 at 15:00
  • Ooops my bad about the start byte. Need moar coffee Commented Aug 8, 2014 at 15:00

5 Answers 5

9

Use std::getline:

#include <string> // for std::getline

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string s;
    std::getline(stream, s, '\0');
    return s;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please leave a comment when downvoting.
This comment gives a straightforward implementation of the question in the title, but it does not answer the 'Why does my code fail question' related to the body of the question. Answering the title helped me most.
6

istream::get(char &) returns a reference to the istream, not the character read. You can either use the istream::get() variant like so:

while ((ch = stream.get()) != '\0') {
    result += ch;
}

Or use the returned stream reference as a bool:

while (stream.get(ch)) {
    if (ch != '\0') {
        result += ch;
    } else {
        break;
    }
}

Comments

4

Use getline and pass \0 (null character) as the delimiter.

2 Comments

Could the downvoter explicitly explain why this wouldn't work?
yeah, this is easier than my way of doing it. upvoted
1

The get() function returns a reference to the stream , not the character which has been put in ch .

You need to test ch for being a '\0'.

1 Comment

ahhhh I see what I've done. Thats probably it alright
0

The get function returns the reference of the stream not the read character.

Modifiy the code like:

std::string ObjElement::ReadStringFromStream(std::ifstream &stream) {
    std::string result = "";
    while (stream.get(ch)) { // exit at EOF
        if (ch != '\0')
            result += ch;
        else
            break;  // Stop the loop when found a '\0'
    }
    return result; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.