question on fread()

S

syntax

hi here is my test code

include<stdio.h>
#include<stdlib.h>



int main()

{
FILE *ptr1 = fopen("c:\\test1.txt","rb");
if(!ptr1) printf("not openedl");


int b[50];

fread(b,9,4,ptr1);

for(int k=0;k<6;k++)
printf("%d\n",b[k]);


}

test1.txt
----------
1 2 3 4 5
6 7 8 9 0

output
-------
540155953
540287027
906628405
941635360
807418144
-390643


my question > how this output is coming ? why not the output is 1 2 3
4 5
6 ?

i am not asking how can i do it .....but i am asking the explanation
why and how that garbage output is coming ? is that a garbage at all?
or have some maths behind that?





is it true that i can not use fread() to read text file? if not can u
show a code which uses fread() like above but outputing 1 2 3 4 5 6
instead of garbage ?

thanks
 
J

Joona I Palaste

syntax said:
hi here is my test code

int main()
{
FILE *ptr1 = fopen("c:\\test1.txt","rb");
if(!ptr1) printf("not openedl");
int b[50];
fread(b,9,4,ptr1);

for(int k=0;k<6;k++)
printf("%d\n",b[k]);

test1.txt
----------
1 2 3 4 5
6 7 8 9 0
output
-------
540155953
540287027
906628405
941635360
807418144
-390643


my question > how this output is coming ? why not the output is 1 2 3
4 5
6 ?
i am not asking how can i do it .....but i am asking the explanation
why and how that garbage output is coming ? is that a garbage at all?
or have some maths behind that?

Your code is reading the bytes in the file into integers (which I
think are 4 bytes on your computer) and then printing the decimal
value of those integers. This decimal value is not the same as the
characters those bytes represent in your character set. You'll
probably want to look up fgets() instead of fread() and use "%s" for
printing instead of "%d".
is it true that i can not use fread() to read text file? if not can u
show a code which uses fread() like above but outputing 1 2 3 4 5 6
instead of garbage ?

Of course you can use fread() to read text files. But you have to be
careful what you make it read. It's usually best to make the element
size 1 byte - any higher and the phenomenon I described above will
occur. Also note that you really must use "%s" or "%c" in printf()
rather than "%d", because "%d" is for printing the byte's decimal
value, not the character it represents.
I could write a full program to do what I am trying to describe but I
am too tired. Some C guru can write it for us.
 
D

Default User

syntax said:
hi here is my test code

include<stdio.h>
#include<stdlib.h>

int main()

{
FILE *ptr1 = fopen("c:\\test1.txt","rb");

You opened in binary . . .
fread(b,9,4,ptr1);

Read in binary . . .
for(int k=0;k<6;k++)
printf("%d\n",b[k]);

}

test1.txt


From a text file. Why did you think this would work? Does that look like
binary data to you?



Brian Rodenborn
 
S

syntax

occur. Also note that you really must use "%s" or "%c" in printf()
rather than "%d", because "%d" is for printing the byte's decimal
value, not the character it represents.

....ok, then in that way, it would be only usefull for printing,bcoz it
will read chars and print chars in the same way... i can not use that
for math operation!!

i think....
the real difficulty will come , if i read integers from the file using
FREAD()
AND then i try to increase each number by 1(i.e something like b[k]
= b[k] +1....its a math operation)....probabily i can not do it by
using fread() or can i?

is it too difficult to use fread() and do the math operation as i am
trying to do.

i can do it using fscanf()....but its a question to me whether fread()
can do it ??

N.B : i did a mistake ...the second argument and thrid argument should
be flipped...second argument should be = sizof (int)=4 , and third
argument = no of elements(9) want to read at one go.....anyway , still
my question remains same.

if fread() can not do these kind of math operation then what is the
importance of it!!!

any simple answer
 
J

Joona I Palaste

...ok, then in that way, it would be only usefull for printing,bcoz it
will read chars and print chars in the same way... i can not use that
for math operation!!

So you want to use the numbers those characters represent, and not the
raw bytes? In that case, look into fscanf(), or use fgets() like I told
you and use strtol() to convert them to integers.
Note that in C, the character '1' is not the same thing as the integer
1 at all! If your text file contains the following:
1 2 3 4
Then (assuming an ASCII system) the raw bytes will be:
0x20 0x31 0x20 0x32 0x20 0x33 0x20 0x34
which a call to fread() with 4-byte elements will read as:
0x20312032 0x20332034 in big-endian or
0x32203120 0x34203320 in little-endian.
(Shown above in hexadecimal. Use a calculator to get the decimal
values.)
i think....
the real difficulty will come , if i read integers from the file using
FREAD()
AND then i try to increase each number by 1(i.e something like b[k]
= b[k] +1....its a math operation)....probabily i can not do it by
using fread() or can i?

Of course you can. But note that what you will be adding 1 to is the
raw byte, not the number the character glyph represents. For example,
in the above example, it's going to be 0x20, 0x31, 0x20 and so on,
not 1, 2, 3 or 4.
is it too difficult to use fread() and do the math operation as i am
trying to do.
i can do it using fscanf()....but its a question to me whether fread()
can do it ??

If you can do it with fscanf(), why do you insist on fread()? Is this
homework? Well, as I said above, of course fread() can do it. Integers
are integers. C does not care if they represent printable characters or
not. You must care about this yourself, and use strtol() to get the
digits those printable characters represent.
N.B : i did a mistake ...the second argument and thrid argument should
be flipped...second argument should be = sizof (int)=4 , and third
argument = no of elements(9) want to read at one go.....anyway , still
my question remains same.
if fread() can not do these kind of math operation then what is the
importance of it!!!

Gosh. Do you think these math operations are all there is to C? The
whole point in fread() is that it reads the raw bytes from the file,
without any conversion whatsoever. Thus it is most useful for reading
binary files. It is not even meant generally for text files. You have
fgets() and fscanf() for that.
 
K

Kelsey Bjarnason

[snips]
int b[50];

fread(b,9,4,ptr1);

Okay. This says "read 4 items of 9 bytes length, storing the data into b,
from file ptr1". First, I suspect you wanted to change the order around
to reading 9 items of 4 bytes, which, presumably, is the size of an int
on your implementation.

That said... the net result should be the same; that is, it should read 36
bytes from the file, storing them in ptr1. However, that's the kicker:
it's reading 36 bytes. Now let's see...
test1.txt

1 has a value (in ASCII, which I'm assuming you're using) of 49. 2 has a
value of 50. Assuming those things in between are spaces, they'll have
values of 32. So your file is a set of byte values thusly:

[49][32][50][32][51][32]...
540155953

Okay, well, watch this:

unsigned char buff[4] = { 49, 32, 50, 32 };
printf( "Result: %d\n", *(int *)buff );

Any bets what the result is? On my machine - which apparently has the
same integer size, layout, etc as yours - I get the exact same value
displayed.
my question > how this output is coming ? why not the output is 1 2 3 4
5
6 ?

The problem is that you're not reading text and converting it to its
numeric representation. That is, you're not reading the characer '1' and
converting it to a value of 1. What you're doing is reading the charatcer
_code_ of the '1', along with the character code of the space, and so on,
then using those values as the values of the bytes in an int.

What you want to be doing is reading the _representation_ of the numbers
in the file (eg '1'), converting those to their numeric values (eg 1),
then storing those in the array.
i am not asking how can i do it .....but i am asking the explanation why
and how that garbage output is coming ? is that a garbage at all? or
have some maths behind that?

Hopefullly the explanation made some sense.
is it true that i can not use fread() to read text file? if not can u
show a code which uses fread() like above but outputing 1 2 3 4 5 6
instead of garbage ?

You _can_ use fread to read a text file... but to do what you're doing,
it's the wrong tool. You might want to take a look at fscanf.
 
O

Old Wolf

Okay, well, watch this:
unsigned char buff[4] = { 49, 32, 50, 32 };
printf( "Result: %d\n", *(int *)buff );

Any bets what the result is?

I'd bet at 3:1 on a runtime error, unless you're using a compiler
that aligns char arrays (or a system that doesn't require int alignment)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,140
Messages
2,570,810
Members
47,357
Latest member
sitele8746

Latest Threads

Top