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.