Jump to content
  • Advertisement
Sign in to follow this  
justin12343

Help With ReadString function

This topic is 2954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm getting a access violation in my ReadString function.


bool CFile::ReadString(char *buffer) {

bool readedOk = true;
size_t lenChar = sizeof(UINT8);
UINT8 character = 1;
char *string = buffer;

readedOk = (lenChar == Read(&character, lenChar)) && (character != 0);
do {
*string++ = character; //<------Breaks Here-----
readedOk = readedOk && (lenChar == Read (&character, lenChar));
} while (readedOk && character != 0);
*string = NULL;

return readedOk;
}



error:
First-chance exception at 0x00418b2a in SADE_2D.exe: 0xC0000005: Access violation writing location 0xcdcdcdf9.
Unhandled exception at 0x00418b2a in SADE_2D.exe: 0xC0000005: Access violation writing location 0xcdcdcdf9.

I don't really understand why I'm getting this error. Help will be much appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Well the most likely cause is that the value of buffer you're passing to the function is invalid.

Share this post


Link to post
Share on other sites
1) This is C++. In C++, we do not invent a class for file I/O, because the standard library provides one. We especially do not write code that works specifically on files, because well-written C++ code can perform the same operation on any kind of stream (e.g. the standard input/standard output, or a "stringstream" object wrapped around an in-memory string).

2) This is C++. In C++, we do not hack around trying to represent strings by manipulating memory with char*'s, because the standard library provides a real string type.

3) This is C++. In C++, the sizeof a byte is 1, by definition.

4) In English, the past tense of "read" (pronounced "reed") is "read" (pronounced "red").

The following C++ code accomplishes the task you are attempting, given the appropriate inclusions from the standard library:


bool readNullTerminatedStringFromStream(istream& is, std::string& result) {
return std::getline(is, result, '\0');
}


Notice that it is so simple that there is no reason to make the function at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!