首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

文件读取有关问题

2012-03-04 
文件读取问题准备从文件中读取一些内容,文件如下:test1hello34test243test345test2orange32..............

文件读取问题
准备从文件中读取一些内容,文件如下:
test1   hello   34   test2   43   test3   45
test2   orange   32
...................
格式就是如此,但是每行长度不定,包含内容个数也不同,想把每行读出后再将其中内容分割出来:
test1
hello   34
test2   43
test3   45

test2
orange   32
。。。。。。。。。。。。。
试了sscanf等函数,但是由于水平问题效果达不到,求教各位大侠,谢谢。

[解决办法]
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream is( "test.txt ");
string line;
while(getline(is,line))
{
//cout < <line < <endl;
char h[10]={0};
char t[10]={0};
char d[10]={0};
sscanf(line.c_str(), "%s ",h);
cout < <h < <endl;
int i=strlen(h)+1;
while(1)
{
if(i> =line.length())
break;
sscanf(line.c_str()+i, "%s%s ",t,d);
cout < < " " < <t < < " " < <d < <endl;
i+=strlen(t)+strlen(d)+2;
}
cout < <endl;
}
system( "pause ");
return 0;
}

[解决办法]
try stringstream
[解决办法]
while(fgets(line,sizeof(line),fp))
{
//deal one line
}

热点排行