Jump to content
  • Advertisement
Sign in to follow this  
janjanjan

Basic Binary I/O

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

Hey!
I'm still wondering why this code is not working...
the output in the file is "abc" and the output in the console are a bunch of wierd characters. I'm sure it's not a big deal to find the mistake but I got code-blind already...


#include <cstdio>
#include <string>
#include <iostream>

void write()
{
char buffer[] = {'a','b','c'};
FILE* filehandle;
filehandle = fopen("binary.bin", "wb");

fwrite(buffer,1,sizeof(buffer),filehandle);
fclose(filehandle);
}

void read()
{
fpos_t position;
size_t size;
char* buffer;
FILE* fhandle;

fhandle = fopen("binary.bin", "rb");
position = fseek(fhandle,0, SEEK_END);
size = ftell(fhandle);

buffer = (char*) malloc (sizeof(char)*size);
fread(buffer,1,size,fhandle);
fclose(fhandle);

printf("%s",buffer[1]);
free (buffer);
}

int main ()
{
write();

read();

int i;
std::cin >> i;

}




thanks a lot!

Share this post


Link to post
Share on other sites
Advertisement
This line:

printf("%s",buffer[1]);

probably doesn't do what you think it does. The %s format specifier indicates that the element you pass it is a null terminated string. However, you don't pass it a string, instead you give it a single char.

Share this post


Link to post
Share on other sites

position = fseek(fhandle,0, SEEK_END);
size = ftell(fhandle);

buffer = (char*) malloc (sizeof(char)*size);
fread(buffer,1,size,fhandle);


You're seeking to the end, but never move the file pos back to the beginning of the file after ftell.

Share this post


Link to post
Share on other sites
@ janjanjan
in your code you jump to the end of the file 'position = fseek(fhandle,0, SEEK_END)' but you never return to the beginning of your file. And since you are at the end of the file, Nothing is read. Therefore you're printing random memory.

@SiCrane: Isn't he passing a pointer to a character? I think the printf functions would just check if that first character is not a NULL terminator and print the character ans o on. I think it would just print characters until it finds a 0 (as it should)

What I don't understand is why he is starting at index 1 and not at index 0.

assainator

Share this post


Link to post
Share on other sites
Quote:
Original post by assainator
@SiCrane: Isn't he passing a pointer to a character?

No. For a pointer p and an integer i, p is equivalent to *(p + i). buffer is a char *. A char * plus an integer is still a char *. When the dereference operator is applied a char * and that expression is passed to a variadic function, it becomes a single char with integral promotion.

Share this post


Link to post
Share on other sites
You don't null terminate your string.

edit: this post was a response to a question that the previous poster edited out.

Share this post


Link to post
Share on other sites
Quote:
Original post by assainator
@SiCrane: Isn't he passing a pointer to a character? I think the printf functions would just check if that first character is not a NULL terminator and print the character ans o on. I think it would just print characters until it finds a 0 (as it should)

assainator


No, he passes a value instead of a pointer.
The OP tries to print a string, that is not being null terminated in the buffer, and he doesn't even pass that as a pointer. The printf will handle the value as a pointer that points to a random junk somewhere in the abyss.

Even if he passed a pointer, the output would be something like this:

abc;>i7z43tg;>*<;><;z`°`˘°75.8é84.8l8ő3p8,m3┌Ç

(until a zero is reached)

The whole thing is totally wrong.

It's not a good idea to print a binary file as a string. Because there can be zeros anywhere in that file.

So I'd print the values like this:
for( int i = 0; i < size; i++ )
{
printf("%c",buffer);
}

Share this post


Link to post
Share on other sites
Quote:
Isn't he passing a pointer to a character?

buffer[1] is a character. To pass it as a char* --> &buffer[1].

I agree, though, that there's a couple problems as noted above (including buffer[1] instead of buffer [0]).

The biggest problem is not resetting the file pointer.

printf("%c",buffer) could be used in a loop.

printf("%s",buffer) could be used if a null terminator is added to the buffer.

EDIT: szecs ninja'd me. [wink]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!