Re: can't read file?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 23 Sep 2005 01:15:33 GMT
nick <i141802596@xxxxxxxxx> writes:
> the following code is used to read a file called "input.txt"
> # include <stdio.h>
> int main(int argc,char *argv[]){
> FILE *fp;
> int ch;
> fp=fopen("input.txt","r");
> ch=fscanf(fp,"%d",&ch);
> printf("%d\n",ch);
>
> fclose(fp);
> return 0;
> }
>
> the contains in "input.txt"
> 3
> 3 14
> 2 8
> -1 0
>
>
> when i run the program the output is:
> 1
>
> what's going on?
> why the output is not 3?
fscanf() returns the number of items scanned, which in this case will
be 1 if it succeeds. You're passing &ch as an argument to fcanf()
*and* you're assigning the result to ch. Don't do that.
(I'm not certain whether it invokes undefined behavior, but it's
certainly not what you want to do .)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Prev by Date: Re: Return statement twice in the same expression
- Next by Date: Re: Floats to chars and chars to floats
- Previous by thread: Catching the unsigned int
- Next by thread: Re: can't read file?
- Index(es):
Relevant Pages
|
Loading