【问题标题】:in c file operation not to append在c文件操作中不要追加
【发布时间】:2017-03-19 19:44:51
【问题描述】:

我正在尝试从文件中读取这样的矩阵

1 2 3 5 6

4 5 3 5 6

之后,我更改其中一个并希望将其写入同一个文件。 但它附加到文件的末尾;在不使用 fseek() 的情况下是否有解决方案?我想像每次使用文件一样使用文件,它从头开始。

in main(){
FILE* in; int s[2][5];
in=fopen("input","r+");
read(in,s);
s[1][0]=7;
write(in,s);
}

void read(FILE* in, int s[][5]) {
    int i, j, x;
    for (i = 0; i<2; ++i) {
         for (j = 0; j<5; ++j) {
            fscanf(file_pointer, "%d", &x);
            s[i][j] = x;
        }
    }
}
void write(FILE* out;int s[][5]){
int i, j, x;
    for (i = 0; i<2; ++i) {
         for (j = 0; j<5; ++j) {
            fscanf(file_pointer, "%d", &x);
            s[i][j] = x;
        }
    }
}

【问题讨论】:

  • 打开文件r,然后在read() 之后关闭并在write 之前使用w 重新打开。这样,您只需覆盖现有文件。

标签: c file url-rewriting append


【解决方案1】:

读写函数之间的使用:

fclose(in);
in = fopen("input","r")

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 2017-05-03
    相关资源
    最近更新 更多