FSCANF () function of __ function

Source: Internet
Author: User

Function: Performs formatted input from a stream, fscanf encounters a space and ends a newline, and ends when noting a space.

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: integer, value equals [argument ...] Number of

The format is the equivalent of the formatting in the regular expression, that is, what format separates the information in the file. The light is not good to understand, to use an example to illustrate the specific how to use:

First I have a data. TXT file inside the data format is as follows:

2,50,41,w,20.585828

4,52,51,r,52.012547

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

Many similar records are separated by

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


The function I implement is to assign five fields of the data in the above file to the corresponding five variables and output the values of those 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);
  &NBSP;}
  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 specifies the format of the separator parameter, which is used here to separate. This makes it easy to get the values of each field of the record without having to write a function to parse it or anything.
     printf ("%ld,%ld,%ld,%c,%lf\n", dev,offset,length,ch,ts);
 }
Close (FD);
return 0;
}

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.