Talk About Network

Google





Programming > C - C++ Learning > Re: If NULL End...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 19 Topic 4067 of 4400
Post > Topic >>

Re: If NULL Ends a String or Char[], How to Send NULL to a File?

by Hal Vaughan <hal@[EMAIL PROTECTED] > Mar 13, 2008 at 02:27 PM

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
 




 19 Posts in Topic:
If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 08:28:33 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 10:33:41 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 13:38:19 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 14:00:08 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 14:27:55 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 14:35:06 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 14:38:36 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 16:14:14 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 16:47:31 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"R. Scott Mellow&quo  2008-03-13 13:07:37 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 18:59:08 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ralph D. Ungermann&  2008-03-13 15:03:06 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Hal Vaughan <hal@[EMAI  2008-03-13 16:56:49 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Richard Heathfield <rj  2008-03-13 17:04:36 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Anand Hariharan <mailt  2008-03-13 15:45:18 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ralph D. Ungermann&  2008-03-13 14:38:28 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
Philip Potter <pgp@[EM  2008-03-13 16:20:55 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ralph D. Ungermann&  2008-03-13 18:57:23 
Re: If NULL Ends a String or Char[], How to Send NULL to a File?
"Ed Norris" <  2008-03-15 20:05:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 10:03:03 PST 2009.