Storage and read Analysis of files for subsequent specific applications

Source: Internet
Author: User
Tags strtok

This day, the .txt file is used to read information. Content format: 1, XXX, 111,111

2, xxxx, 333,333

X represents a character, and a number represents an integer. Put the read content in a structure, struct placelocation {int number; char name [20]; int X, int y }. Currently, one job is to read the content in one row. 2. Read content is stored in the struct. 3, separated.

First open the file pfile = fopen (cfilename, "R"), while (fgets (Cline, 100, pfile )! = NULL) {the code you want to process} is the read content of a row.

Char SEPs [] = ","; // delimiter
Char * token; // character pointer after separation

While (fgets (Cline, 100, pfile )! = NULL)

{

Token = strtok (Cline, SEPs );//
Placelocation. Number = strtol (token, null, 10); // convert the read characters into numbers.
Token = strtok (null, SEPs );
Strcpy (placelocation. Name, token );//

Token = strtok (null, SEPs );
Placelocation. x = strtol (token, null, 10 );

Token = strtok (null, SEPs );
Placelocation. Y = strtol (token, null, 10 );

}

Fclose (pfile); // close the file

This is the simplest application. On this basis, you can read more complex content.

Some common conversion functions are listed here:

Char * to int: char * c = "123" int I = strtol (char * C, null, 10); // 10 is in hexadecimal format, 8 is the 8-in-order format.

Char [] to char *; strcpy (char *, char []);

Tchar tch [] to cstring STR; Str. Format (_ T ("% s"), TCH );

Cstring STR to Char cfilename []; wcstombs (cfilename, STR, str. getlength ());

Here are some of the most common ones that are also quite needed. You can simply search for the Internet.

 

 

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.