写入文件切断第二行 - C

Writing to file cutting off second line - C

问题陈述

当我写入文件时,第二行被截断而其他行没有。


代码

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.

任何帮助都会很棒!提前致谢。

您在重新打开文件之前没有关闭它。

fprintf(outFile, "%s", quotes[0]);
fclose(outFile);                        // add this line
outFile = fopen(OUT_FILE, "a");