讲一个600行资料 只读取每行前九位 写入另一个文件
讲一个600行文件 只读取每行前九位 写入另一个文件
讲一个600行文件 只读取每行前九位 写入另一个文件
------解决方案--------------------
写了几行代码,可以参考一下,没有考虑行不够9个的情况,其他细节也没有考量.
讲一个600行文件 只读取每行前九位 写入另一个文件
------解决方案--------------------
写了几行代码,可以参考一下,没有考虑行不够9个的情况,其他细节也没有考量.
- C/C++ code
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream ifile("input.txt"); if (!ifile) { cerr << "error: unable to open input file: " << ifile << endl; return -1; } ofstream ofile("outfile.txt"); string line; while (getline(ifile, line)) ofile << line.substr(0, 9) << endl; ifile.close(); ofile.close(); return 0; }
------解决方案--------------------
//参考一下吧
bool InputFile(const char *ifilename,
const char *ofilename)
{
ifstream ifile(ifilename,ios::in);
if(!ifile)
{
cout<<"Îļþ²»´æÔÚ"<<endl;
return false;
}
ofstream ofile(ofilename,ios::out);
if(!ofile)
{
cout<<"Îļþ²»´æÔÚ"<<endl;
return false;
}
string str;
int num=0;
//文件到达末尾或则读取完九行时结束
while(getline(ifile,str,'\n') && num<9)
{
ofile<<str<<endl;
num++;
}
ifile.close();
ofile.close();
return true;
}