我正在写一个函数,它应该(如果文件已经存在)将第一个数字加1并将函数的参数追加到文件末尾。C++文本文件指针问题
实施例:
- 追加(4,9);
- append(5,6);
文件内容物在1: 1 \ N 4 \ n 9
文件内容物在2: 2 \ N 4 \ n 9 \ N 5 \ N 6
int append (int obj, int objType) {
ifstream infile;
infile.open("stuff.txt");
if (infile.fail()){
infile.close();
ofstream outfile;
outfile.open("stuff.txt");
outfile << 1 << endl << obj << endl << objType;
outfile.close();
}
else {
int length = 0;
while (!infile.eof()){
int temp;
infile >> temp;
length ++;
}
infile.close();
infile.open("stuff.txt");
int fileContents[length];
int i = 0;
while (!infile.eof()){ /*PROGRAM DOES NOT ENTER HERE*/
infile >> fileContents[i];
i ++;
}
infile.close();
ofstream outfile;
outfile.open("stuff.txt");
fileContents[0] +=1;
for (i = 0; i < length; i++){
outfile << fileContents[i] << endl ;
}
outfile << obj << endl << objType;
}
的程序永远不会进入第二个while循环,所以内容永远不会复制到数组中,然后复制到文件中。我不确定问题是什么或如何解决。任何帮助将不胜感激。 :)
您可能想要修复该格式。 – Catskul 2009-12-01 18:55:42
...通过在每行加上4个空格。 – 2009-12-01 18:57:08
谢谢:)新手在这里:) – Erica 2009-12-01 19:02:21