0
我试图将txt文件拆分成几个新文件。这是我到目前为止已经完成:将文本文件拆分为多个文件C++
long c = 0;
string s;
vector<string> v;
我需要算我的txt文件有多少行有(它的工作原理):
while(getline(inputFile, s, '\n')){
v.push_back(s);
c++;
}
long lineNumber = c;
long max = 100;
long nFiles;
检查多少新文件将被创建:
if((lineNumber % max) ==0)
nFiles = lineNumber/max;
else
nFiles = lineNumber/max + 1;
创建文件的新名称:
long currentLine = 0;
for(long i = 1; i <= nFiles; i++){
stringstream sstream;
string a_i;
sstream <<i;
sstream >> a_i;
string outputfiles = "name" +"_" + a_i +".txt";
ofstream fout(outputfiles.c_str());
for (int j = currentLine; j<max; j++){
fout << v[j]<<endl;
}
fout.close();
currentLine = max;
}
inputFile.close();
它创建文件,但突然停止工作。有谁知道为什么?
你在调试器中运行你的代码吗?代码中哪里停止工作? – 2014-12-02 14:51:42
是的,我做到了。以下是我所得到的结果:“程序接收信号sigsegv分段故障” – 2014-12-02 14:53:25
在发生分段故障的位置应该有一个行号。 – Max 2014-12-02 14:54:44