c - 变量文件可能尚未初始化
问题描述
这是我的功能。我的 charpeca
定义为char peca[MAX_BUFFER]
,其中max_buffer
1024。我想在我的“tabuleiro”文件中写入里面的内容peca
,但我的程序总是说该文件可能尚未初始化。
所以我的函数不打印我的 txt 文件,有什么想法吗?
void write_txt (char *peca){
FILE *file;
fopen("tabuleiro.txt","w");
fprintf(file,"M: %c\n",*peca);
fclose(file);
}
解决方案
所以我的函数没有打印在我的 txt 文件中
将返回指针分配FILE* fopen(const char* filename, const char* mode );
给您的文件,如下所示:
file = fopen("tabuleiro.txt", "w");
然后,你会在打开它后检查 if file
is not NULL
,以便知道它打开成功。
%s
此外,您可能希望使用用于字符串的 写入文件。
所以,你可以试试这个:
void write_txt (char *peca) {
FILE *file = fopen("tabuleiro.txt", "w");
if(!file) {
printf("File did NOT open successfully!\n")
// error handling here..do not execute the fprintf() or fclose()
}
fprintf(file,"M: %s\n",*peca);
fclose(file);
}
推荐阅读
- python - 什么是turtle.colormode(255)?
- python - Flask-Login 问题:在身份验证和重定向后 current_user 仍然是 AnonymousUserMixin 类型
- python-3.x - 如何为数据框的单列获取 df.isnull()
- r - 导入股票价格技术分析的最佳方法是什么?
- python - 无法在终端中安装 prttsx3
- sql - SQL中的累积计数
- reactjs - TypeError - undefined 不是一个对象(评估'c.currentObservable.query.refetch')。在本机反应
- unity3d - 为道路系统获得 1x1 大小的地形形状的最佳方法?
- python - 找不到 Heroku Flask Gunicorn 命令
- magento - Magento 2 退款错误 - 交易已被拒绝