c 从txt中读取数据

txt 数据排列

1 0 0 0
542 1547 388 1393
1282 569 0 0
1226 569 0 0
1171 569 0 0
1115 572 0 0
1060 576 0 0
1006 588 0 0
952 600 0 0
901 617 0 0
851 635 0 0
812 657 0 0
774 679 0 0
740 706 0 0
707 733 0 0
677 764 0 0
647 796 0 0
621 833 0 0

 

C代码:

 1     FILE *handle = NULL;
 2     char buf[MAXELEMENT];
 3     int line_element[4];
 4 
 5     fopen_s(&handle, fileIn, "rb");
 6     if (NULL == handle)
 7     {
 8         printf("cannot open file!\n");
 9         return;
10     }
11 
12     while (fgets(buf, MAXELEMENT, handle) != NULL)
13     {
14         sscanf_s(buf, "%d %d %d %d\n", &line_element[0], &line_element[1], &line_element[2], &line_element[3]);
15         printf("%d %d %d %d\n", line_element[0], line_element[1], line_element[2], line_element[3]);
16     }
17 
18 
19     fclose(handle);
20     handle = NULL;

 

关键是sscanf_s的用法。

 

转载于:https://www.cnblogs.com/Keven-Lee/p/7270170.html

你可能感兴趣的:(c 从txt中读取数据)