File Operation Method fscanf

Source: Internet
Author: User
Tags first string

First, put the classic method above the topic:

In the following cases, a large amount of regular data is stored in the formatted reading method of the file:

Almond # eed9c4

Antique brass # c88a65

Cot cot # fdd5b1

Aquamarine #71d9e2

Asparagus # 7ba05b

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

First run the Code:

 

1 file * FP; // defines the file pointer
2 char a [20] = {0}; // define two arrays to accept data 3 char B [20] = {0 }; 4 5 FP = fopen ("/users/My/file", "R"); // open the file 6 if (FP = NULL) 7 {8 printf ("open the file is error! \ N "); 9 exit (0); 10} 11 fseek (FP, 0, seek_set ); // obtain the file internal pointer 12 13 15 while (2 = fscanf (FP, "% [^ #] % s \ n", a, B )) // use the fscanf function to format and read the data in the file. Use # As the separator 16 {17 printf ("A = % s -------- \ NB = % s \ n",, b); 18 // printf ("sizea = % LD \ n", sizeof (a); 19 // nslog (@ "astring = % @", [nsstring stringwithformat: @ "% s", a]); 20} 21 22 fclose (FP); // close the file


Read results:

 

A = shadow, B = #837050

 

A = Shamrock, B = #33cc99

 

A = shocking, pink, B = # ff6fff

 

A = silver B = # c9c0bb

 

A = sky blue B = # 76d7ea

 

A = spring green B = # ecebbd

 

A = sunglow B = # ffcc33

 

A = sunset orange B = # fe4c4

 

Explanation:

The C language used here provides a formatting and reading method, which is very similar to the regular expression reading method, "% [^ #] % s \ n" indicates that the data is read using the separator #. The data is divided into two parts. \ n indicates that the data is read until the line break. you can use the while loop to read a complete file until the end.

Therefore, using a and B At the end can receive and store the data before and after #, and read only one row at a time.

Note:

If you replace % [^ #] % s \ n with "% [^ #] # % s \ n", the result is as follows:

A = shadow, B = 837050.

A = Shamrock, B = 33cc99

A = shocking, pink, B = ff6fff

A = silver B = c9c0bb

A = sky blue B = 76d7ea

A = spring green B = ecebbd

A = sunglow B = ffcc33

A = sunset orange B = fe4c40

If you replace it% [^ #] % [# ^] \ N results with the following error:

A = shadow, B = #

A = 837050

Shamrock, B = #

A = 33cc99

Shocking, pink, B = #

Replace"% [^ #] % [#83 ^] \ n" result:

A = shadow, B = #83

A = 050

Shamrock, B = #33

A = c99

Shocking, pink, B = #

A = f6fff

Silver B = #

A = 9c0bb

Sky Blue B = #

A = 6d7ea

Spring green B = #

(The above is a bit unintelligible. Not only is it intercepted by matching as much as possible, but the position of the file pointer also skips

B = #83

A = 050

) It turned out to be #837050, But he skipped 7, why ??????????????????????????????????????? ????????

 

Replace"% [^ #] % [^] \ N" or % [^ #] % [^ \ n] Results:

A = shadow, B = #837050

A =

Shamrock, B = #33cc99

A =

Shocking, pink, B = # ff6fff

A =

Silver B = # c9c0bb

A =

Sky Blue B = # 76d7ea

A =

Spring green B = # ecebbd

A =

Sunglow B = # ffcc33

We found that a linefeed \ n was read each time starting from the second time. At this time, we can think that after the first read introduction, the internal pointer of the file is marked as \ n each time, therefore, the first line break \ n is returned each time. (I guess there is no specific test, and I don't quite understand these changes ).

Later, I added that the above guess has confirmed that it is correct. When I change the while to the following:

While (2 = fscanf (FP, "% [^ #] % [^] \ n", a, B ))

 

{

Fseek (FP, ftell (FP) + 1, seek_set );

 

Printf ("A = % s B = % s \ n", a, B );

 

}

 

The result is as follows:

A = shadow, B = #837050

A = Shamrock, B = #33cc99

A = shocking, pink, B = # ff6fff

A = silver B = # c9c0bb

A = sky blue B = # 76d7ea

Summary: The differences between "% [^ #] % s \ n and" % [^ #] % [^] \ n or % [^ #] % [^ \ n:

The former % s \ n is read to the next position of \ n, the latter % [^] \ n or % [^ #] % [^ \ n] is read to \ n.

% [^ #] % S \ n, % [^ #] # % s \ n, % [^ #] % [# XXX ^] \ n:

% [^ #] % S \ n: separator with #. The previous one reads the # sign until it does not contain the # sign ), the last character is read to the next pointer position of \ n.

% [^ #] # % S \ n:% [^ #] Is read to the previous position of # (not including #),# % S reads (excluding #) from the last position of # And reads to the next position of \ n.

% [^ #] % [# XXX ^] \ n:% [# XXX ^] matching as much as possible# XxxRead (including #). The read ends after the maximum match. For example"% [#1232 ^] encountered #1525 read result: #1

 

Not quite familiar with the above ?? It's okay. Let's look at a similar example. I'll learn more!

The file contains the following data:

20.585828, 41, W
52.012547, 51, R

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

File * FP; int FD; long dev; long offset; long length; char ch; double Ts = 0.000000; FP = fopen ("/users/My/file. save "," R "); If (FP = NULL) {printf (" open the file is error! \ N "); exit (0);} lseek (FD, 0, seek_set); // Description: fscanf (FP," % lD, % lD, % C, % lf \ n ", & Dev, & offset, & length, & Ch, & TS)
// Dear user, Here % lD, % C, % lf \ n is not the meaning of the string in printf. the comma (',') represents the comma (',') as the separator (or unit), which serves as the basis for data segmentation, therefore, commas are required. line breaks \ n are the basis for parsing a row of data.
// Therefore, the commas (,), commas (,), and '\ n' are all essential. Otherwise, program errors may occur and read failures may occur (understanding more)
While (5 = fscanf (FP, "% lD, % C, % lf \ n", & Dev, & offset, & length, & Ch, & TS ))
{
// Here, comma ',' and '\ n' are used to standardize the output format. They can be omitted, but they cannot be omitted in fscanf.
Printf ("% lD, % C, % lf \ n", Dev, offset, length, CH, TS );
}
Fclose (FP );

Another example: This example shows how fscanf formats and reads files.

Long L;

Float FP;

Char s [81];

Char C;

Stream = fopen ("/users/lanou3g/fscanf. Out", "W + ");

If (Stream = NULL)

Printf ("thefilefscanf. outwasnotopened \ n ");

Else

{

Fprintf (stream, "% s, % d, % F, % C", "a-string ",

65000, 3.14159, 'x'); // This is the opposite operation for formatting and writing files. The specific meaning of the string is similar to that of fscanf, and they work perfectly together.

/* Setpointertobeginningoffile :*/

Fseek (stream, 0l, seek_set );

/* Readdatabackfromfile :*/

Fscanf (stream, "% [^,]", S); // note that the format for formatting and reading strings here is % [^,] it indicates that the string is read and separated by commas. This is similar to the usage of % [^ #] % s \ n in the first example, % [^ #] % s \ n means to read the string before and after # Division. The first string must be formatted % [^ #] to distinguish it from the subsequent string, because the second row is followed by the end of the row, you can directly write the % s [note when % s # % s \ n write method and % [^ #] % [^] Write method is incorrect, I am not quite clear about the specific reasons].

Fscanf (stream, ", % lD", & L); // The following data uses ", format character" as the output standard for formatting, "% lD, "This comma is written incorrectly.

Fscanf (stream, ", % F", & FP );

Fscanf (stream, ", % C", & C );

/* Outputdataread :*/

Printf ("% s \ n", S );

Printf ("% LD \ n", L );

Printf ("% F \ n", FP );

Printf ("% C \ n", C );

Fclose (Stream );

}

In the above example, change fscanf (stream, "% [^,]", S); To fscanf (stream, "% s,", S ); the read result is incorrect as follows:

A-string, 65000, 3.141590, X

0

0.000000

 

 

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.