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

求C++读文本一程序,分不是有关问题

2012-04-01 
求C++读文本一程序,分不是问题,在线等文本格式:32141234#1234123#132413#2341234#3214#12341#...反正就是

求C++读文本一程序,分不是问题,在线等
文本格式:
32141234#1234123#132413#
2341234#3214#12341#
.
.
.反正就是这样一行一行的,怎么取得文本中所有内容,在取到每一行,在取到没行中的每个小字符串(小字符串是用#分开的,每行用#结尾,三个小字符串)

注明:不要用到MFC的,用C++语言



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

using namespace std;

int main()
{
string line, str;
ifstream fin( "test.txt "); //测试文件,
/*
内容:32141234#1234123#132413#
2341234#3214#12341#abcde#
*/

string::size_type index, preindex;
while(!fin.eof())
{
getline(fin, line);
cout < <line < <endl; //读取文件, 显示

preindex=0;
index = line.find( "# ", 0); //寻找分割字符
while(index <line.length() && string::npos != index)
{
str = line.substr(preindex, index-preindex); //获取子串
cout < <str < <endl; //输出
index++;
preindex = index;
index = line.find( "# ", index); //下一次循环,继续寻找
}
}
system( "pause ");
return 0;
}
[解决办法]
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

ifstream fin( "test.txt "); //测试文件,
char line[80], str[80];
/*
内容:32141234#1234123#132413#
2341234#3214#12341#abcde#
*/

while(!fin.eof())
{
fin.getline(line, 80);
int linei=0;
int stri=0;
while (linei < strlen(line))
{
if (line[linei]> = '0 ' && line[linei] <= '9 ')
{
str[stri++] = line[linei];
}
else
{
str[stri] = '\0 ';
cout < <str < <endl;
stri = 0;
}
linei++;
}
}
system( "pause ");
return 0;
}

热点排行