Sign in to follow this  

file access problems

This topic is 4199 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

Hello, For whatever reason, read data from a file, it wants to read data that isn't even in the file:
char Header[] = "[SWNMV1.0]";
char Read[sizeof(Header)];
char ch;

std::ifstream file;
file.open(strFilename, std::ios::binary);

file.read(Read, sizeof(Header)); 
Read[sizeof(Header)] = '\0'; //loc 1

return S_OK; //loc 2

In the debugger, I hold my mouse over the Read variable (loc 2), and all it shows are a bunch of "I"'s. If I hold it at location 1, it shows a bunch of "i"'s, followed by the data in my file. Thanks for any help, exorcist_bob

Share this post


Link to post
Share on other sites
Well? Have you even reached that point in execution before inspecting the variable? Could you describe the problem in more detail? I don't think I get that behavior.


Also, you have a buffer overflow. Change your code to either of the following:

char Header[] = "[SWNMV1.0]";
char Read[sizeof(Header) + 1];
char ch;

std::ifstream file;
file.open(strFilename, std::ios::binary);

file.read(Read, sizeof(Header));
Read[sizeof(Header)] = '\0'; //loc 1

return S_OK; //loc 2


Or:

char Header[] = "[SWNMV1.0]";
char Read[sizeof(Header)];
char ch;

std::ifstream file;
file.open(strFilename, std::ios::binary);

file.read(Read, sizeof(Header) - 1);
Read[sizeof(Header) - 1] = '\0'; //loc 1

return S_OK; //loc 2



jfl.

Share this post


Link to post
Share on other sites
yeah, this is a common problem that i used to have when i was first learning how to do fileIO

like jfl said, you have a buffer overflow, which means its trying to read stuff when its not there, so it fills it with a "null" character, even tho its not really null, its just something that the file can read, even if it doesnt exist

try his code, and that should work

Share this post


Link to post
Share on other sites

This topic is 4199 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this