c++中怎样读取文本文件中的某行中的某些字段?

来源:百度知道 编辑:UC知道 时间:2024/05/07 12:44:28
如题,请各位多多帮忙啊。
就是说比如打开了一个文本文件,ifstream fin("1.txt");
我想直接获取该文本第三行第五至第七个字符,请问有没有什么好办法?

//只能满足你的要求:
//直接获取该文本第三行第五至第七个字符。
//假如你的1.txt在C盘
//Enter filaname:
//输入c:\1.txt,然后输入3(回车),5(回车),7(回车)即可
//请每次都编译一次!!
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;

int fileToVector(string filename,vector<string>& svec)
{
ifstream inFile(filename.c_str() );
if (!inFile)
return 1;
string s;
while(getline(inFile,s))
svec.push_back(s);
inFile.close();
return 0;
}

void rndread(vector<string>& svec,int line,string::size_type& byteBeg,string::size_type& byteEnd)
{
cout<<"该文本的第"<<line<<"行第"<<byteBeg<<"至第"<<byteEnd<<"个字符为:"<<endl;
for(string::size_type index=byteBeg-1;index!=byteEnd;++index)
cout<<svec[line-1][index]