gpt4 book ai didi

c - 在c文件操作中不要追加

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:59 24 4
gpt4 key购买 nike

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

1 2 3 5 6

4 5 3 5 6

在那之后我更改了其中一个并想将它写入同一个文件。但它 append 到文件末尾;有没有不使用 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;
}
}
}

最佳答案

读写函数之间的使用:

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

关于c - 在c文件操作中不要追加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40448683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com