读书人

从文件读取字符串的有关问题

发布时间: 2012-02-05 12:07:14 作者: rapoo

从文件读取字符串的问题
我创建了一个文本 内容如下

a === advise
b === blood
c ===command
.........

请问要将等号前面的一行一行读出来 放到另外一个文件 该怎么做呢??
同理 还有将等号后面的读出来 放到一个文件里

我用的是 vc
谢谢

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

using namespace std;

int main()
{
ifstream openf( "test.txt "); //测试文件
ofstream prefix( "out1.txt "); // 前缀部分
ofstream suffix( "out2.txt "); //后缀部分
string line;
string::size_type loc;

while(!openf.fail())
{
getline(openf, line);
cout < <line < <endl;
loc = line.find( "== ", 0);
prefix < <line.substr(0, loc) < <endl;
suffix < <line.substr(loc+3) < <endl;;
}
system( "pause ");
return 0;
}

/*
test.txt内容:
a === advise
b === blood
c === command

out1.txt内容:
a
b
c
c

out2.txt内容:
advise
blood
command
command

*/

读书人网 >C++

热点推荐