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;
myFile.open (serdev, ios::out | ios::binary);
That way my input is buffered so I don't miss anything ant it seems to be
working fine.
On the other end, in what will be another thread once I get it all
working,
I'm writing to the serial ****t as a binary file and I open it like this:
int serfd;
string serdev = "/dev/ttyS0";
serfd = open(serdev, O_RDWR | O_NOCTTY | O_NDELAY);
Eventually I'll be using "/dev/ttyUSB0" instead, but that shouldn't make a
difference. I would also think, since the serial ****t is a file, that the
OS should not make a difference here. I'm reading from and writing to a
file. I'll be setting the serial ****t params elsewhere.
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) and the fopen way you
show above is the current C++ way. From what I see in the reference I use
at cplusplus.com, it looks like you're opening the file for writing in
binary.
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?
Thanks!
Hal


|