#include <fstream>
#include <string>
#include <vector>
using namespace std
int main()
{
ifstream inFile("1.txt")
vector<string>v
string s
while(getline(inFile, s))
v.push_back(s)
inFile.close()
int n
cout <<"输入要读的行数: "
cin >>n
try {
cout <<"该行内容为: " <<v.at(n-1)
} catch (const exception&e) {
cerr <<e.what()
}
}
***********************************************************************逐行进行文件读取:
hello MMM
Input terminated by data mismatch.
Press any key to continue
***********************************************************************
#include <iostream>
#include <fstream>
using namespace std
void main(void) {
const getline_size = 10
fstream file
file.open("e:\\litcool.txt",ios::out)//在e根目录下生成一个litcool.txt的文件
file<<"hello MMMM university"<<'\n'<<"in tianjin."
file.close()
file.open("e:\\litcool.txt",ios::in)
cout<<"逐行进行文件读取:"<<endl
char temp[100]
while(file.good()) { //这儿有改动
file.getline(temp, getline_size)// 这儿也有
cout<<temp<<endl
}
if(file.eof()) cout <<"End of file reached.\n"
else if(file.fail()) cout <<"Input terminated by data mismatch.\n"// 由于输入数据失配而结束
else cout <<"Input terminated for unknown reason.\n"
cout<<endl
file.close()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)