新手求助~怎么读文本文件中的以下内容!急
A1A1 96 96 1 1 2 2 3 4 7 9 10 20 23 34 12 45 256 0 10 10 6 3 1 1;中间为空格,数字两两一对,最后数字个数不为单数,读到分号结束,望大家指点下,最好能写段代码,
谢谢了
![](images/smilies/emot/em04.gif)
ifstream fin("文件名");
int num1,num2;
while (fin.get() != ";"){
fin >> num1 >> num2;
//处理num1和num2
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
FILE* fp;
int a, b;
char buf[5];
char c;
fp=fopen("input.txt", "r");
if(!fp)
{
printf("cannot open input file.\n");
exit(0);
}
fscanf(fp, "%s", buf);
printf("%s", buf);
do
{
fscanf(fp, "%c", &c);
if(c==';')
break;
fscanf(fp, "%d %d", &a, &b);
printf("%c%d %d", c, a, b);
}
while(1);
printf("\n");
fclose(fp);
return 0;
}
大哥~你说的那不行啊,文件没有一点格式的排列,就直接输出了