c - f = fopen("textfile","a") 的代码在另一个设备中为 f 返回 NULL,但在我的设备上运行完美
问题描述
嗨,我已经在我的机器上创建并运行了一个程序,但是当我尝试在另一台机器上运行代码时它没有运行。
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
char buff[50];
f = fopen("textfile.txt","r");
if(f==NULL)
exit(1);
printf("Contents before appendiing: ");
while(fgets(buff,50,f)!=NULL)
fputs(buff,stdout);
fclose(f);
f = fopen("textfile.txt","a");
if(f==NULL)
{
printf("Unable to open file");
exit(1);
}
printf("\nEnter contents to append (type 'exit'to stop): ");
do{
fgets(buff,50,stdin);
if(strcmp(buff,"exit\n")==0)
break;
fputs(buff,f);
}while(f!=NULL);
fclose(f);
f = fopen("textfile.txt","r");
if(f==NULL)
{
printf("Unable to open file");
exit(1);
}
printf("\nContents after appendiing: ");
while(fgets(buff,50,f)!=NULL)
fputs(buff,stdout);
fclose(f);
return 0;
}
该程序在我的原始机器中提供了这个输出,它应该是这样工作的:
Contents before appendiing: hello world
Enter contents to append (type 'exit'to stop): hello
exit
Contents after appendiing: hello world
hello
Process returned 0 (0x0) execution time : 10.006 s
Press any key to continue.
虽然它在我的另一台机器上给出了这个:
Contents before appendiing: hello world
Unable to open file
Process returned 1 (0x1) execution time : 4.452 s
Press any key to continue.
我知道发生这种情况是因为 f 的值变为 NULL,但为什么它发生在一台机器上而不是另一台机器上。我正在运行 Windows 10 并在两台机器上使用 code::blocks IDE。
解决方案
推荐阅读
- java - 顺序读取文件时出现 NumberFormatException?
- python - 带有标记位置的张量流图像分类器
- regex - Perl 正则表达式 | 如何从文件中排除单词
- mysql - MySQL 更新更新的行数超过了应有的行数
- c++ - 具有虚拟和非虚拟析构函数的删除运算符的不同行为
- django - 如何将 Python 中的 sqlite3 版本从 3.7.17 升级到 > 3.8
- php - 在管理新产品页面中默认将产品类型设置为“变量”
- typescript - 详细列表过滤器办公室ui面料
- sql - 编译时出错,PLS-00103,遇到符号“(”,预期如下:;
- openapi-generator - 是否可以在 Openapi 代码生成中排除路径?