Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question

Reply
Thread Tools

question

 
 
Barry Schwarz
Guest
Posts: n/a
 
      01-16-2008
On Sun, 13 Jan 2008 03:11:39 GMT, "Bill Cunningham" <(E-Mail Removed)>
wrote:

> I wrote this small program to read a 512 block of binary data and write
>the same to a file. My code compiled well. The only thing is when I ran the
>compilers binary instead of a data file of 512 bytes I got one of 2048
>bytes.
>
>#include <stdio.h>
>
>main(){
> int buf[512];
> FILE *fp;
> fp=fopen("r.dsk","rb");
> if (fp==NULL) {printf("Error"); exit(0);}
> fread(buf,sizeof(int),512,fp);
> fclose(fp);
> fp=fopen("dat","wb");
> if (fp==NULL) {printf("Error");}
> fwrite(buf,sizeof(int),512,fp);
> fclose(fp);}
>
>Is it the code or some overhead from the compiler or linker?
>
>Bill
>The unix dd command copies exactly 512 bytes of I asked it too.


If your input file was exactly 512 bytes long, then it probably
contained 128 integers, not 512. There are very few home systems
where sizeof(int) is 1. On your system, sizeof(int) is apparently 4
and 512 int will require 2048 bytes.

You forgot to check the return from fread which would have told you
exactly how many int had been read (which you would then use as the
third argument to fwrite). Your code as written says:
Read up to 512 int
Regardless of how many were read, write 512 to the output.

If you really meant to process bytes, change buf to a char array and
change the second argument of both fread and fwrite to sizeof *buf.


Remove del for email
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      01-16-2008

"Kenneth Brody" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bill Cunningham wrote:
>>
>> I wrote this small program to read a 512 block of binary data and
>> write
>> the same to a file. My code compiled well. The only thing is when I ran
>> the
>> compilers binary instead of a data file of 512 bytes I got one of 2048
>> bytes.
>>
>> #include <stdio.h>
>>
>> main(){
>> int buf[512];

>
> Why "int"?
>
>> FILE *fp;
>> fp=fopen("r.dsk","rb");
>> if (fp==NULL) {printf("Error"); exit(0);}
>> fread(buf,sizeof(int),512,fp);

>
> Here, you read up to 512 int-sized values, and discard the return
> value, so you don't know how many were actually read.
>
>> fclose(fp);
>> fp=fopen("dat","wb");
>> if (fp==NULL) {printf("Error");}

>
> You should do something about the failure, besides simply say
> "Error" and then fwrite() to the NULL stream.
>
>> fwrite(buf,sizeof(int),512,fp);

>
> Here, you unconditionally write 512 int-sized values, regardless
> of how many were read.
>
>> fclose(fp);}
>>
>> Is it the code or some overhead from the compiler or linker?

>
> It's your code. If sizeof(int)==4, then the program did exactly
> what you told it to do.
>
>> The unix dd command copies exactly 512 bytes of I asked it too.

>
> That's because you told "dd" to copy 512 bytes, not 512 int-sized
> values.
>
> Why are you reading int-sized chunks when you say you want bytes?


char on my system is 8 bit. I guess I could've used it instead of int
which is 32 bit.

> Why are you writing 512 of these, regardless of how many were read?
>
> Why are you discarding the return from fread()?
>

I'm not quite sure what you mean here.


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      01-16-2008
Bill Cunningham wrote:

>
> "Kenneth Brody" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...


> > Why are you reading int-sized chunks when you say you want bytes?

>
> char on my system is 8 bit. I guess I could've used it instead of
> int which is 32 bit.


No "could" about it. You should have. What was your reason for int?

> > Why are you writing 512 of these, regardless of how many were read?
> >
> > Why are you discarding the return from fread()?
> >

> I'm not quite sure what you mean here.



fread() returns a value. You did not capture or examine this value.
That value is there for a reason, that reason being to tell you how
many items were successfully read.

I again STRONGLY urge you to thoroughly read up on every function you
to try to use. You use many of them incorrectly or haphazardly. This
makes your programs incorrect and/or unsafe.





Brian
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2008

> fread() returns a value. You did not capture or examine this value.
> That value is there for a reason, that reason being to tell you how
> many items were successfully read.
>
> I again STRONGLY urge you to thoroughly read up on every function you
> to try to use. You use many of them incorrectly or haphazardly. This
> makes your programs incorrect and/or unsafe.


I get most of my info from these two places the tutorial is the best. I
can't read the standard functions prototype and return values so the second
site really doesn't help me much.
http://www.eskimo.com/~scs/cclass/
http://www-ccs.ucsd.edu/c/

I guess I can break out the old k&r2.

Bill


 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      01-17-2008
Bill Cunningham wrote:
>
> "Kenneth Brody" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Bill Cunningham wrote:

[...]
> >> int buf[512];

[...]
> >> fread(buf,sizeof(int),512,fp);

[...]
> >> fwrite(buf,sizeof(int),512,fp);

[...]
> > Why are you reading int-sized chunks when you say you want bytes?

>
> char on my system is 8 bit. I guess I could've used it instead of int
> which is 32 bit.
>
> > Why are you writing 512 of these, regardless of how many were read?
> >
> > Why are you discarding the return from fread()?
> >

> I'm not quite sure what you mean here.


fread() gives a return value -- the number of items read, or zero
if an error occurred. You discard this return value, and always
call fwrite() telling it write 512 int-sized chunks, regardless of
how many were actually read.

Your code could have still "worked", had you used the return value
from fread() as the third parameter to fwrite(). I say "worked" in
quotes because of the limited scope of this program. Given your
particular system and input file, fread() would say that it read
128 int-sized chunks (512 bytes), and passing that 128 back into
fwrite() would have caused those same 512 bytes to have been
written. In fact, it would "work" as long as the input file was
an exact multiple of sizeof(int) bytes, and no longer than 512
times that size.

Of course, the correct way, given that you simply want to copy a
sequence of bytes, is to use "char" instead of "int".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      01-17-2008
Bill Cunningham wrote:

>
> > fread() returns a value. You did not capture or examine this value.
> > That value is there for a reason, that reason being to tell you how
> > many items were successfully read.
> >
> > I again STRONGLY urge you to thoroughly read up on every function
> > you to try to use. You use many of them incorrectly or haphazardly.
> > This makes your programs incorrect and/or unsafe.

>
> I get most of my info from these two places the tutorial is the
> best. I can't read the standard functions prototype and return values
> so the second site really doesn't help me much.


Tutorials are fine, but they are no substitute for a reference book. If
you have to have something online, this isn't too bad, although sparser
than I'd prefer (usage examples would help).

<http://www.cppreference.com/all_c_functions.html>

Example:

fread
Syntax:

#include <stdio.h>
int fread( void *buffer, size_t size, size_t num, FILE *stream );

The function fread() reads num number of objects (where each object is
size bytes) and places them into the array pointed to by buffer. The
data comes from the given input stream. The return value of the
function is the number of things read. You can use feof() or ferror()
to figure out if an error occurs.


> I guess I can break out the old k&r2.


You should probably have that at hand.




Brian

--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      01-17-2008
Bill Cunningham wrote:
> "Kenneth Brody" <(E-Mail Removed)> wrote in message


>>
>> Why are you reading int-sized chunks when you say you want bytes?

>
> char on my system is 8 bit. I guess I could've used it instead of int
> which is 32 bit.


If you want 512 lots of 8-bit objects, ask for that. Dont' tell the
compiler to read 512 lots of some other size.

>>
>> Why are you discarding the return from fread()?
>>

> I'm not quite sure what you mean here.


fread returns a value. The value is an error status. Do not ignore error
statuses.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2008

> fread
> Syntax:
>
> #include <stdio.h>
> int fread( void *buffer, size_t size, size_t num, FILE *stream );
>
> The function fread() reads num number of objects (where each object is
> size bytes) and places them into the array pointed to by buffer. The
> data comes from the given input stream. The return value of the
> function is the number of things read. You can use feof() or ferror()
> to figure out if an error occurs.
>
>
>> I guess I can break out the old k&r2.

>
> You should probably have that at hand.


I see that the f* family of functions don't mean file. That is confusing
for a beginner. I under printf and now fprintf but I've never used vprintf.
I suppose it can be redirected to stdin, stdout, and stderr too right? I've
never used it.

Bill


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-17-2008
Bill Cunningham wrote:
>> fread
>> Syntax:
>>
>> #include <stdio.h>
>> int fread( void *buffer, size_t size, size_t num, FILE *stream );
>>
>> The function fread() reads num number of objects (where each object is
>> size bytes) and places them into the array pointed to by buffer. The
>> data comes from the given input stream. The return value of the
>> function is the number of things read. You can use feof() or ferror()
>> to figure out if an error occurs.
>>
>>
>>> I guess I can break out the old k&r2.

>> You should probably have that at hand.

>
> I see that the f* family of functions don't mean file. That is confusing
> for a beginner. I under printf and now fprintf but I've never used vprintf.
> I suppose it can be redirected to stdin, stdout, and stderr too right? I've
> never used it.


Interpreting the initial 'f' as "file" or "FILE*" seems reasonable
to me; I don't understand why you find it confusing.

vprintf() writes to stdout, and cannot be redirected anywhere else.
vfprintf() can write to any open output stream, including stdout and
stderr. It is most unusual to find anything that writes to stdin.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2008
> vprintf() writes to stdout, and cannot be redirected anywhere else.
> vfprintf() can write to any open output stream, including stdout and
> stderr. It is most unusual to find anything that writes to stdin.
>

fgets writes to stdin maybe that's what I'm thinking of. I know advice was
given to me not to consider the functions begining with f to associated with
the FILE struct or text or binary files. Is vprintf like printf? If so why
use it? If vfprintf is like fprintf what is the purpose of the v family of
functions?

Cheers
Bill


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments