Skip to main content
gamedev.net
🔒 Locked

Binary file output

Started by Joel_h May 26, 2002 at 10:01 PM 3 replies 200+ views
Original Post
Joel_h
Joel_h
hello, in a program i''m making i need to use a file to store coordinates. I was using fputc() and fgetc() to store the integers for the coordinates but i was having a problem when the number for the coordinate was too high, it wouldn''t save the right number to the file for some reason. i changed the code and now i use fprintf() and fscanf() for the file I/O and it works the only problem is that when i use fprintf() instead of making a small binary file with stuff like "f¯G#EÁÎÿÿÿÿÿÿÿ" in it, it generates a huge text file with the actual numbers writen in it. It works, but the file is a whole lot bigger than the binary file that i had before is there anyway that i can reduce the size of the text file? or can someone tell me why i was getting the problem with fputc()? thanks
Zipster
Zipster
Use the binary file like before, but use fwrite and fread, which are meant for binary files, instead of fputc and fgetc, which are meant for text files.
DrPizza
DrPizza
Whether the I/O is character oriented (fXxxc()), string oriented (fXxxs()), block oriented (fread()/fwrite()), or formatted (fXxxf()) is neither here nor there.

"binary" or "text" determines whether some translation occurs, not what functions are suitable.

Files opened as text may transform the characters you write to them. Those transformations can include:
line-ending alteration (conversion of bare \ns to \r\n pairs, for instance)
line termination (to ensure the last line is terminated with a new line)
insertion of a literal "EOF" character (ctrl-D or ctrl-Z, for instance)

Files opened as binary undergo none of those changes. What you write is passed through verbatim, except that IIRC any number of nulls can be written beyond the last character you write.

The functions can be freely intermixed. They work properly with files opened as text or binary. They don't care (any translation occurs at a lower level anyway).

What you *shouldn't* mix is operations that work with file descriptors (POSIX read() and write()) with operations that work with file streams (fXxx()).

Anyway.

The likely problem with fputc() is that, whilst it takes an int, it casts to an unisigned char, which is probably resulting in the truncation of your number. fprintf() isn't doing this, because it converts everything to human-readable text. To write an integer to the file, you are probably best off with fwrite(). To write a single integer myInt to a file stream file fwrite(&myInt, sizeof(int), 1, file);. To read it back again, fread(&myInt, sizeof(int), 1, file);.


[edited by - DrPizza on May 27, 2002 2:36:38 AM]
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
Zipster
Zipster
I know what they mean, I was only trying to create a good rule of thumb, not be uber technical.
DrPizza
DrPizza
quote:
Original post by Zipster
I know what they mean,

Certainly not clear from your post.

quote:
I was only trying to create a good rule of thumb,

You did nothing of the sort.

quote:
not be uber technical.

It''s not a question of "uber technical". It''s a question of "your rule of thumb was absurd".
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.