Re: confused abt file operations
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 31 Aug 2005 05:51:45 GMT
"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.
.
- References:
- confused abt file operations
- From: siliconwafer
- Re: confused abt file operations
- From: Gordon Burditt
- Re: confused abt file operations
- From: Peter "Shaggy" Haywood
- Re: confused abt file operations
- From: siliconwafer
- confused abt file operations
- Prev by Date: memset doubt
- Next by Date: Re: memset doubt
- Previous by thread: Re: confused abt file operations
- Next by thread: Re: confused abt file operations
- Index(es):
Relevant Pages
|