Re: confused abt file operations



"siliconwafer" <spdandavate@xxxxxxxxx> writes:
> Hi Again,
> So what about numbers wirtten to /read from the text/binary files?
> I read that in text files numbers are stored as characters or strings
> rather than their actual values.So a number 1234 will occoupy5 bytes(4
> + ' \0 ' ).In binary files,numbers are stored as actual values i.e 1234
> is stored as 2 bytes integer and so on..
> was the book correct?

Since I've been telling people to search the newsgroup for the phrase
"Context, dammit!", I should occasionally post the explanation along
with the phrase.

Don't assume that your readers have easy access to the article to
which you're replying. It's important to provide some context,
generally relevant quotes from the previous article, so each article
can be understood on its own.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

As for your question, the way numbers are stored in a file depends on
how you write them. If you use

int n = 1234;
fprintf(myfile, "%d\n", n);

you'll get the 5 characters '1', '2', '3', '4', '\n' written to the
file. If you use

fwrite(&n, sizeof n, 1, myfile);

you'll get a binary representation of the value 1234, typically 2 or 4
bytes; the order in which the bytes are written is system-specific.

Either fprintf() or fwrite() can be used with either binary or text
files; both are defined to write sequences of bytes. It just usually
makes more sense to use fprintf() with text files and fwrite() with
binary files.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: Historical question, why fwrite and not binary specifier for fprintf?
    ... write, but no fwrite. ... There was putc(), which is portable. ... were added to the C standard to allow portable binary IO to files. ... standard C function fprintf to allow binary operations? ...
    (comp.lang.c)
  • Re: to tidy my file
    ... have it in the path setting as well, ... You can´t use fwrite to write this, use fprintf. ... Why is top-posting such a bad thing? ...
    (comp.soft-sys.matlab)
  • Re: read and write binary file
    ... There can be other differences as well, and this is why C makes a clear distinction. ... C allows you to use fprintf on a binary stream and fwrite on a text stream. ...
    (comp.lang.c)
  • Re: convert decimal to hexadecimal number
    ... an integer valueand then after converting it into ... print it..so using fprintf along with %lx would not help me.for eg..if ... file, in binary mode, as a series of bytes, use fwrite. ... the underlying I/O package may care. ...
    (comp.lang.c)
  • Re: read a string..writing a string
    ... > How will I read a string using scanf in that function: ... > to the write what scanedf will be written using fprintf ... the broken "Reply" link at the bottom of the article. ...
    (comp.lang.c)