FSCANF () function specific explanation

Source: Internet
Author: User

Once parsing a regular file is either a regular table or a silly self-tapping code to parse the regular file. Today, I suddenly found out that C's library function has a ready-made function to parse a regular file, which is the fscanf () function. Alas, I have done so much work on my own, here to specifically analyze the FSCANF function:

FSCANF () function (a bit like the normal form):

Function: Run formatted input from a stream, fscanf encounters a space and ends with a newline, and the space is also closed.

Usage: int fscanf (FILE *stream, char *format,[argument ...]);

int fscanf ( file pointer , format string, input list);

For example:

FILE*FP;

CHARA[10];

Intb

Doublec;

FSCANF (FP, "%s%d%lf", A,&b,&c)

Return value: integral type, value equal to [argument ...] The number

The format in this case is the equivalent of the form in the normal expression, that is, what format to separate the information in the file. Light is not easy to understand, a sample to illustrate how to use:

First I have a data. The data format for the TXT file in the polygon is as follows:

2,50,41,w,20.585828

4,52,51,r,52.012547

.........................

A lot of similar records are separated by,

.......................


My function is to assign five fields of data in the above file to the corresponding five variables, and output the values of these variables. The implementation of the program is as follows:

#include <stdio.h>
#include <stdlib.h>


int main ()
{
int FD;
Long Dev;
Long offset;
Long length;
Char ch;
Double ts=0.000000;
if ((Fd=fopen ("/home/haixian/ceshi/data.txt", "R")) <0)
{
printf ("Open the file is error!\n");
Exit (0);
}
Lseek (Fd,0,seek_set);


while (5==FSCANF (FD, "%ld,%ld,%ld,%c,%lf\n", &dev,&offset,&length,&ch,&ts))
{Here is the second parameter specifying the format of the delimited parameter, which is used here to separate. This is very easy to get the records of the fields of the values do not need to write their own functions to parse what.
printf ("%ld,%ld,%ld,%c,%lf\n", dev,offset,length,ch,ts);
}
Close (FD);
return 0;
}

The example above is a good way to understand how the FSCANF function is used. Once you knock the code to parse such a file, laborious still not pleasing, the result is not accurate. Ah just can sigh I am too weak, later still have to study!!!!


FSCANF () function specific explanation

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.