码迷,mamicode.com
首页 > 其他好文 > 详细

c 从txt中读取数据

时间:2017-08-01 20:40:04      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:span   handle   取数   txt   code   open   读取   nbsp   printf   

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的用法。

 

c 从txt中读取数据

标签:span   handle   取数   txt   code   open   读取   nbsp   printf   

原文地址:http://www.cnblogs.com/Keven-Lee/p/7270170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!