Fscanf function, fscanf

Source: Internet
Author: User

Fscanf function, fscanf
Function Definition:

int fscanf( FILE *stream, const char *format [, argument ]... );
The following is an example of csdn:
/* FSCANF.C: This program writes formatted * data to a file. It then uses fscanf to * read the various data back from the file. */#include <stdio.h>FILE *stream;void main( void ){   long l;   float fp;   char s[81];   char c;   stream = fopen( "fscanf.out", "w+" );   if( stream == NULL )      printf( "The file fscanf.out was not opened\n" );   else   {      fprintf( stream, "%s %ld %f%c", "a-string",                65000, 3.14159, 'x' );      /* Set pointer to beginning of file: */      fseek( stream, 0L, SEEK_SET );      /* Read data back from file: */      fscanf( stream, "%s", s );      fscanf( stream, "%ld", &l );      fscanf( stream, "%f", &fp );      fscanf( stream, "%c", &c );      /* Output data read: */      printf( "%s\n", s );      printf( "%ld\n", l );      printf( "%f\n", fp );      printf( "%c\n", c );      fclose( stream );   }}Outputa-string650003.141590x
The following is an example of a number of similar function operations. (It is also a general operation of files) 1 write operation function
# Include <stdio. h> main () {char * s = "That's good news");/* define the string pointer and initialize */int I = 617; /* define Integer Variables and initialize */FILE * fp;/* define FILE pointer */fp = fopne ("test. dat "," w ");/* create a text file and write only */fputs (" Your score of TOEFLis ", fp ); /* write a string of characters */fputc (':', fp) to the created file;/* write a colon to the created file: */fprintf (fp, "% d \ n", I);/* write an integer to the created file */fprintf (fp, "% s", s ); /* write a string to the created file */fclose (fp);/* close the file */}

After execution: test. dat is a file with the following content: Your score of TEFLis: 617That's good news.

2. Read operation functions
<Span style = "font-size: 14px;" >#include <stdio. h> main () {char * s, m [20]; int I; FILE * fp; fp = fopen ("test. dat "," r ");/* Open the text file read-only */fgets (s, 24, fp ); /* read 23 characters from the file */printf ("% s", s);/* output the read string */fscanf (fp, "% d ", & I);/* read integer count */printf ("% d", I);/* output read integer count */putchar (fgetc (fp )); /* read one character and output at the same time */fgets (m, 17, fp);/* read 16 characters */puts (m ); /* output the read string */fclose (fp);/* close the file */getch ();/* wait for any key */} </span>


The execution result is as follows: Your score of TOEFL is: 617
That's good news


Fscanf Function

Fscanf is similar to scanf: When data can be normally read, fscanf returns the number of data read; otherwise, EOF is returned.

# Include <stdio. h>
Const int MAX_N = 6;
Int main ()
{
Int x, I;

// Note that a new 1.txt file must be created in the same directory of the program, which contains less than MAX_N data,
// If there are only three data records: 1 2 3

FILE * myfile = fopen ("1.txt"," r ");
If (! Myfile)
{
For (I = 1; I <= MAX_N; I ++)
Fscanf (myfile, "% d", & x), printf ("% d", x );
}
Return 0;
}

Program output: 1 2 3 3 3 3, read-only from the file into the first three data. Because
Fscanf has read the end of the file and no data is readable. Therefore, the most
The next data, that is, the third data 3.

How does the C language use the fscanf () function to read all the file content?

Int [] account;
For (int I = 0 ;! Feof (file_p); ++ I)/* file_p is the File Identifier */
{
Fscanf (file_p, "% d", account [I]);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.