【发布时间】:2020-06-30 17:31:23
【问题描述】:
问题陈述
当我写入文件时,第二行会被截断,而其他行不会。
代码
void write_out(char** quotes) {
FILE* outFile = fopen(OUT_FILE, "w");
if (outFile == NULL) {
perror("Error with reading the file!");
exit(1);
}
// Overwrites the contents of the file
fprintf(outFile, "%s", quotes[0]);
// Makes the file "appendable"
outFile = fopen(OUT_FILE, "a");
if (outFile == NULL) {
perror("Error with reading the file!");
exit(1);
}
// Write the rest of the quotes into the file
for (int i = 1; i < MAX_QUOTES; i++) {
fprintf(outFile, "%s", quotes[i]);
}
fclose(outFile);
}
原始数据
Treat programmers like bad kids.
Good design adds value faster than it adds cost.
Perl is the only language that looks the same before and after RSA encryption.
Don't worry if it doesn't work right. If everything did, you would be out of a job.
A programming language is low level when its programs require attention to the irrelevant.
输出(在文件中)
Treat programmers like bad kids.
it adds cost. // Line cut off
Perl is the only language that looks the same before and after RSA encryption.
Don't worry if it doesn't work right. If everything did, you would be out of a job.
A programming language is low level when its programs require attention to the irrelevant.
任何帮助都会很棒!提前致谢。
【问题讨论】:
-
outFile = fopen(OUT_FILE, "a");--> 检查它的返回值。也许你想在这里freopen()? -
您在重新打开文件之前没有关闭它。
-
@WeatherVane 是的!当然!那行得通!谢谢,请发布您的答案,以便我检查。有时,简单的事情可能会让您感到困难。
-
这毫无意义。无需在附加模式下关闭和打开。继续写吧。但我会在输出中添加
newline:fprintf(outFile, "%s\n", quotes[i]); -
不,写入是顺序的。但是如果您以
"w"模式打开一个现有 文件,该文件将被覆盖。