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!
|
|
0
|
|
|
|
Reply
|
hossein.rohani (6)
|
1/23/2008 7:09:36 PM |
|
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.
|
|
0
|
|
|
|
Reply
|
santosh.k83 (3969)
|
1/23/2008 7:23:06 PM
|
|
"h03Ein" <hossein.rohani@gmail.com> wrote 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
|
|
0
|
|
|
|
Reply
|
regniztar (3128)
|
1/23/2008 7:23:47 PM
|
|
Malcolm McLean wrote:
>
> "h03Ein" <hossein.rohani@gmail.com> wrote 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>
|
|
0
|
|
|
|
Reply
|
santosh.k83 (3969)
|
1/23/2008 7:36:22 PM
|
|
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")
|
|
0
|
|
|
|
Reply
|
army1987 (668)
|
1/23/2008 8:30:07 PM
|
|
In article <fn846t$amn$1@registered.motzarella.org>,
santosh <santosh.k83@gmail.com> wrote:
>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
|
|
0
|
|
|
|
Reply
|
dj3vande3 (264)
|
1/23/2008 8:30:54 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
1/24/2008 2:11:50 AM
|
|
|
6 Replies
43 Views
(page loaded in 0.274 seconds)
Similiar Articles: data structure, fread, find - comp.soft-sys.matlabHello What i am trying to is that read only numbers from the below data structure 0ÌÌÌÌ+2.650ÌÌÌÌ-29.005ÌÌÌÌ+2.325ÌÌÌÌ+36.89ÌÌÌÌ+34.26ÌÌÌÌ-37 ... FREAD() Precision - comp.soft-sys.matlabHi, I am trying to use fread() in place of fscanf() in my matlab file and I see that the values read are different in each case, when read from t... fread and float - comp.soft-sys.matlabSo, if I read the file in with fread(fid,4,'uint32') I get 4 values which I assume are ... % mask1=16777215 ; % 2^24-1 ; % bottom 24 bits holds the mantissa % mask2=4278190080 ... coredump while reading the file using fread - comp.sys.hp.hpux ...Hi, I am getting coredump in fread, I am calling the the C routine conatining fread from COBOL. One more thing, not every time a get this error, only ... serial communication problem with fread function - comp.soft-sys ...Hello everyone, I have a problem with fread function in Matlab. fread function hangs on until there is an input or timeout, how can I set to let it ... Speed-up the reading of large binary files with complex structures ...... ACcuracy(bt_element) = fread(id, 1, 'uint16'); > > MIR1C.List_of_Grid_Point_Data(dsr_counter).Incidence_Angle(bt_element) = fread(id, 1, 'uint16')*90/(2^16 ... Gyroscopes/Accelerometers with Matlab - comp.soft-sys.matlab ...Hi, I've used Matlab a lot in the past for DSP and Image processing projects ... comparing wavread to fread 2 181 M Problem sending binary file via socket. - comp.unix.programmer ...Hi all, My client code are: send(fd, argv[2], strlen(argv[2]) + 1, 0); fp = fopen(argv[2], "rb"); while (bytes_read = fread(buf, sizeof(char), MAX_D... just like half band filters are ther 1/3 band filters too? - comp ...On 2/2/2011 6:11 PM, bharat pathak wrote: > Thanks Fred. > Well, I said" Or a compromise might be to have a "transition/stop" band with the first cutoff frequency ... Date of Last table update - comp.databases.mysqlIs there a way to query a particular table in a database and return the date that the database was last updated? Regards, Fred ... Fred: The Show | Fred Figglehorn | Fred Videos & Games | Nick.comCheck Fred: The Show on nick.com. ... Fred 3: "Camp Fred: Behind the Scenes" Fred takes us behind the scenes and deep underground to the "rat hole" - the mysterious ... Fred 2: Night of the Living Fred - Wikipedia, the free encyclopediaFred 2: Night of the Living Fred (stylized as FЯED 2: Night of the Living FRED) is a 2011 independent comedy film. The film is the sequel to the 2010 film Fred: The ... 7/28/2012 1:55:21 AM
|