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 6 of 19 Topic 4067 of 4400
Post > Topic >>

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

by Richard Heathfield <rjh@[EMAIL PROTECTED] > Mar 13, 2008 at 02:35 PM

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
 




 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 13:04:43 PST 2009.