NoFear

导航

文件读格式字符串【CSDN】

Posted on 2011-12-06 14:11  Fear_Hao  阅读(194)  评论(0编辑  收藏  举报
View Code
//in.txt:
//File Flag 2DPatch
//1 3216
//2 3216 (2548-3216 x 4)
//3 3216 (2552-3216 x 4)
//4 3212 (2556-3212 x 4)
//5 3208 (2564-3216 x 4)
//6 3204 (2572-3216 x 4)
//7 3196 (2580-3216 x 4)
//8 3192 (2588-3216 x 4)
//9 3188 (2596-3216 x 4)
//10 3184 (2548-3216 x 4)
//11 3180 (2548-3216 x 4)
//文档就是这样的格式,怎么才能提取出第一列,第二列,和第三列中的第一部分呢?
//结果应该是这样子的:
//out.txt
//1 3216
//2 3216 2548
//3 3216 2552
//4 3212 2556
//5 3208 2564
//6 3204 2572
//7 3196 2580
//8 3192 2588
//9 3188 2596
//10 3184 2548
//11 3180 2548
#include <stdio.h>
FILE *fi,*fo;
char ln[80]={0};
int a,b,c;
void main()
{
fi=fopen("in.txt","r");
if (NULL==fi)
{
printf("Can not open file in.txt!\n");
return;
}
fo=fopen("out.txt","w");
if (NULL==fo)
{
fclose(fi);
printf("Can not open file out.txt!\n");
return;
}
while (1)
{
if (NULL==fgets(ln,80,fi)) break;
if (3==sscanf(ln,"%d%d (%d",&a,&b,&c))
{
fprintf(fo,"%d %d %d\n",a,b,c);
}
else if (2==sscanf(ln,"%d%d",&a,&b))
{
fprintf(fo,"%d %d\n",a,b);
}
}
fclose(fo);
fclose(fi);
}