1、读取文本文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
int main()
{
FILE *fp;
int i, len, rows=0;
char buf[MAX_LINE];
int pos[MAX_LINE][2];
//读取文本文件
if((fp = fopen("test.txt","r")) == NULL)
{
perror("fail to read");
exit (1) ;
}
//一行一行读取
while(fgets(buf,MAX_LINE,fp) != NULL)
{
len = strlen(buf);
//去掉换行符
buf[len-1] = '\0';
//以空格分割字符串,并将坐标信息赋值给二维数组pos
sscanf(buf, "%d %d", &pos[rows][0], &pos[rows][1]);
rows++;
}
for (i=0;i<rows;i++)
{
printf("%d,%d\n", pos[i][0], pos[i][1]);
}
return 0;
}
test.txt内容如下:
运行结果:
100,50
200,100
300,150