gpt4 book ai didi

c - 写入文本文件

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

所以,我有这段代码:

#include <stdio.h>


int main(int argc, char *argv[]){
FILE *ReadFile, *WriteFile;
float key;
int quantKeys,T;

int i;

/* errors verification */

if (argc < 3){
printf(" Use the correct entry parameters.\n");
exit(1);
}

if((ReadFile = fopen(argv[1],"rw")) == NULL){
printf("Error when trying to open the file\n");
exit(1);
}

if((WriteFile = fopen(argv[2],"rw")) == NULL){
printf("Error when trying to open the file.\n");
exit(1);
}

/* main code */

quantKeys = 45658;
T = 5;

fprintf(WriteFile,"%d",T);
fprintf(WriteFile,"%d",quantKeys);
fclose(ReadFile);
fclose(WriteFile);

return 0;
}

我想要做的就是在文本文件中写入变量“quantChaves”和“T”,并将其作为主函数的第三个参数传递。它编译和运行没有问题,但我的文本文件在运行后保持为空。我做错了什么?

最佳答案

rw 不是 fopen 的有效模式。如果要编写,可以使用ww+r+a+。完整的详细信息在您的 man 页面中,或 here .

主要区别:

w 不允许读取

r+ 如果文件不存在则不创建新文件

a+ 附加文本而不是覆盖文件内容。

关于c - 写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213134/

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