473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fread()

Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer[i]!=EOF ; ++i)
printf("%c" , buffer[i]);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?
Thanks!
Jan 23 '08 #1
6 15850
h03Ein wrote:
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
I hope you actually check that the above call succeeded?
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
Mixed declarations and code are new to C99, which isn't implemented
widely yet.
for(i = 0 ; buffer[i]!=EOF ; ++i)
printf("%c" , buffer[i]);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?
No. It returns the count of characters that it actually read, which may
be less than what you asked it for or zero. To find out if it was
end-of-file or an error that was responsible for the short count you
must use feof() or ferror() after the fread() call.

Specifically in the above for loop you must consider only the first 'i'
characters of buffer as valid.

Jan 23 '08 #2

"h03Ein" <ho************@gmail.comwrote in message
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer[i]!=EOF ; ++i)
printf("%c" , buffer[i]);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?
Thanks!
fread() will stop reading when it encounters EOF.
The return value is the number of items read successfully, and anything
afterwards is garbage - in your case, strange characters.
The EOF is not itself placed into the buffer and is in any case an integer.
If you think about it, it is necessary for EOF to be wider than a character,
if every character is to be representable.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jan 23 '08 #3
Malcolm McLean wrote:
>
"h03Ein" <ho************@gmail.comwrote in message
>Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer[i]!=EOF ; ++i)
printf("%c" , buffer[i]);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why?
does fread consider EOF while reading from file?
Thanks!
fread() will stop reading when it encounters EOF.
<pedantic>
When it encounters end-of-file condition. EOF is the object like macro
used to signal end-of-file /or/ error to user code, and it is /not/
used by fread().
</pedantic>

<snip>

Jan 23 '08 #4
h03Ein wrote:
Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
You should verify that it succeeds:
if (fp == NULL) {
perror("Cannot open file");
exit(EXIT_FAILURE);
}
or whatever is appropriate. exit and EXIT_FAILURE are declared/defined in
stdlib.h.
int c = fread(buffer , 1 , 1024, fp);
fread returns a size_t. You're using 1024 bytes, and int is required to
hold numbers to 32767, so, in this case, it is OK, but, in general, I
would use a size_t. Also, decimal constants ("magic numbers") tend to make
programs harder to understand. Use sizeof buffer in place of 1024 on that
line. Or use #define BUFFERSIZE 1024, declare buffer as `char
buffer[BUFFERSIZE]`, and use fread(buffer, 1, BUFFERSIZE, fp);
int i = 0;
for(i = 0 ; buffer[i]!=EOF ; ++i)
Make it: (see below)
for (i = 0; i < c; i++)
printf("%c" , buffer[i]);
Why? putchar(buffer[i]) would do that. Do you use a hammer to plant
drawing pins?
BTW, you could just use fwrite(buffer, 1, c, stdout); to do the same thing.
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of strange
characters why?
EOF is a negative integer. If char is signed, that loop will stop when
hitting a valid character, e.g. ÿ. If it is unsigned, it will go on until
the end of the memory you're allowed to read, and then the behavior is
undefined. See www.c-faq.com, section 12.
does fread consider EOF while reading from file?
It returns the numbers of characters successfully read, so the loop should
be changed as above.

--
Army1987 (Replace "NOSPAM" with "email")
Jan 23 '08 #5
In article <fn**********@registered.motzarella.org>,
santosh <sa*********@gmail.comwrote:
>h03Ein wrote:
>Hi!
how many characters will be printed in output if our input file has
just 3 characters?
Sinppet Code :
#include<stdio.h>
main()
{
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");

I hope you actually check that the above call succeeded?
> int c = fread(buffer , 1 , 1024, fp);
int i = 0;

Mixed declarations and code are new to C99, which isn't implemented
widely yet.
I don't see any mixed declarations and code here. A bunch of
initializations that would be better done after the declarations, yes,
but all the executable code above this point is in the
initializations.

If he was mixing declarations and code, he'd have a program that was
not valid under any C standard, since he's implicit-inting main (which
went away in C99).
dave

--
Dave Vandervies dj3vande at eskimo dot com
Why does all this bother you with parachutes and not with cars? Many cars
can take you past terminal velocity.
--Maarten Wiltink in the scary devil monastery
Jan 23 '08 #6
h03Ein wrote:
>
how many characters will be printed in output if our input file
has just 3 characters?

Sinppet Code :
#include<stdio.h>
main() {
char buffer[1024];
char input[] = "some file"
FILE *fp = fopen(input , "r");
int c = fread(buffer , 1 , 1024, fp);
int i = 0;
for(i = 0 ; buffer[i]!=EOF ; ++i)
printf("%c" , buffer[i]);
fclose(fp);
}
the result for me is my 3 input characters in file plus lots of
strange characters why? does fread consider EOF while reading
from file?
Yes, but it is a characteristic of the streamed device, not of the
char stream. The value in c should be 3 if only 3 chars were
read. What you have in buffer is not a string, because it lacks a
final '\0'.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 24 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
10
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like...
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
3
by: juleigha27 | last post by:
Hi, First off, I want to apologize if this already got posted it seemed like something happened when I tried to post it previously and it didn't work. I am new to file manipulation with c. I...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.