fscanf() return value

C

Cross

Hello

I am using the following code to check fscanf() error value.
----------------------------------
#include <stdio.h>
int main(){
char arr[6];
int a;
FILE *fs=fopen("lessread.in", "r");
if(fs){
a = fscanf(fs, "%5s", arr);
printf("%s %d\n", arr, a);
}else{
fprintf(stderr, "read error.\n");
}
return 0;
}
-----------------------------------------

The contents of the file are:
---------------------------------
les
---------------------------------
The manual said that if end-of-file is reached before specified characters are
read, EOF is returned. However, my output is:
---------------------------------------
les 1
---------------------------------------

My actual motive: when I am using fscanf() to read a file with the format
sequence "%[^c]" where c is some character, how do I know whether EOF has been
reached or the read was successful.

Regards,
Cross
 
C

Cross

Cross said:
Hello

I am using the following code to check fscanf() error value.
----------------------------------
#include <stdio.h>
int main(){
char arr[6];
int a;
FILE *fs=fopen("lessread.in", "r");
if(fs){
a = fscanf(fs, "%5s", arr);
printf("%s %d\n", arr, a);
}else{
fprintf(stderr, "read error.\n");
}
return 0;
}
-----------------------------------------

The contents of the file are:
---------------------------------
les
---------------------------------
The manual said that if end-of-file is reached before specified characters are
read, EOF is returned. However, my output is:
---------------------------------------
les 1
---------------------------------------

My actual motive: when I am using fscanf() to read a file with the format
sequence "%[^c]" where c is some character, how do I know whether EOF has been
reached or the read was successful.

Regards,
Cross
Well I read that fscanf() shall return te number of successful conversions.
However, %5s should not count as a successful conversion when only 3 characters
are read. I also tried feof() after the fscanf(). It returns 0. Please explain.
 
M

Moi

Cross wrote:
a = fscanf(fs, "%5s", arr);
printf("%s %d\n", arr, a);
-----------------------------------------

The contents of the file are:
---------------------------------
les
---------------------------------
The manual said that if end-of-file is reached before specified
characters are read, EOF is returned. However, my output is:
--------------------------------------- les 1
---------------------------------------

My actual motive: when I am using fscanf() to read a file with the
format sequence "%[^c]" where c is some character, how do I know
whether EOF has been reached or the read was successful.

Regards,
Cross
Well I read that fscanf() shall return te number of successful
conversions. However, %5s should not count as a successful conversion
when only 3 characters are read. I also tried feof() after the fscanf().
It returns 0. Please explain.

"Number of successful conversions" := "number of %'s that have been
successfully matched" (and assignments made)

So it is *not* the number of _characters_, but the number of "items"

HTH,
AvK
 
J

James Kuyper

Cross said:
Cross said:
Hello

I am using the following code to check fscanf() error value.
----------------------------------
#include <stdio.h>
int main(){
char arr[6];
int a;
FILE *fs=fopen("lessread.in", "r");
if(fs){
a = fscanf(fs, "%5s", arr);
printf("%s %d\n", arr, a);
}else{
fprintf(stderr, "read error.\n");
}
return 0;
}

The 5 in "%5s" doesn't mean "read 5 non-whitespace characters, report an
error if there are less than 5". It means "read in no more than 5
non-whitespace characters".
 
C

Cross

James said:
The 5 in "%5s" doesn't mean "read 5 non-whitespace characters, report an
error if there are less than 5". It means "read in no more than 5
non-whitespace characters".
Thank you for the clarification.
 
B

Barry Schwarz

Hello

I am using the following code to check fscanf() error value.
----------------------------------
#include <stdio.h>
int main(){
char arr[6];
int a;
FILE *fs=fopen("lessread.in", "r");
if(fs){
a = fscanf(fs, "%5s", arr);
printf("%s %d\n", arr, a);
}else{
fprintf(stderr, "read error.\n");

This error message is misleading. The problem did not occur during
reading the file but during opening it.
}
return 0;
}
-----------------------------------------

The contents of the file are:
---------------------------------
les
---------------------------------
The manual said that if end-of-file is reached before specified characters are
read, EOF is returned. However, my output is:
---------------------------------------
les 1
---------------------------------------

My actual motive: when I am using fscanf() to read a file with the format
sequence "%[^c]" where c is some character, how do I know whether EOF has been
reached or the read was successful.

Regards,
Cross
 

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
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top