Hal Vaughan said:
> Richard Heathfield wrote:
>
>> Hal Vaughan said:
>>
>>> Richard Heathfield wrote:
>>>
>> <snip>
>>>>
>>>> unsigned char buf[8] = { 0x00, 0x9E, 0x3A, 0x00, 0x00, 0x5F, 0xCD,
>>>> 0xFE }; fwrite(buf, sizeof buf / sizeof buf[0], sizeof buf[0], fp);
>>>
>>> Okay, got it. Just to be sure, fp is filepointer,
>>
>> Yes, a FILE *
>>
>>> the same as an int file descriptor, right?
>>
>> Er, well, it does a similar kind of job, sort of.
>>
>> FILE *fp = fopen(filename, "wb");
>> if(fp != NULL)
>> {
>> fwrite calls go here
>> if(ferror(fp))
>> {
>> /* something screwy somewhere */
>> }
>> if(fclose(fp))
>> {
>> /* something went wrong while
>> closing the file! Yes, this can happen...
>> */
>> }
>> }
>
> This is basically responding to both Richard and Ralph, since both posts
> contain similar info.
>
> I'm dealing with a serial ****t on Linux, which I know is OS specific (I
> got blessed out for daring to ask what was going on with this in
> c.l.c++), but I'm opening and reading it like a binary file, like this:
>
> ifstream myFile;
OHHHHH! You're using C++! Oops. :-)
> From what I am beginning to understand, it looks like the way I am
> opening the file now is the old C way (from what Ralph said)
int descriptors, yes.
> and the fopen way you
> show above is the current C++ way.
Well, it works in C++, but the current C++ way is using iostreams such as
you do appear to be using now.
> From what I see in the reference I
> use at cplusplus.com,
Uurgh.
> it looks like you're opening the file for writing in binary.
Yes.
> Is there an advantage to using this method for reading from a file (or,
> in this case, a serial ****t) as opposed to the ifstream I've been using
> that I mention above?
I'm not going to start a C vs C++ style war on file handling, so I'll
confine my answer to why I chose binary mode. If you are handling raw
data, as opposed to text, binary is the way to go because it will turn
*off* any line-end conversions (e.g. turning 0x0A into 0x0D0A on a Win32
system). Generally, you don't want such conversions when dealing with raw
data. (Trust me on this!)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www.
+rjh@[EMAIL PROTECTED]
users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


|