如何解决不知道为什么文件打不开
这是我的代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct {
int M,N;
int min,max;
int width;
int height;
unsigned char **pixels;
}PPMIMG;
int fnReadPPM(char* fileNm,PPMIMG* img);
int main(int argc,char ** argv)
{
PPMIMG img;
if(fnReadPPM(argv[1],&img) != FALSE)
{
return TRUE;
}
return 0;
}
int fnReadPPM(char* fileNm,PPMIMG* img)
{
FILE* fp;
fp = fopen("/users/ashton/Downloads/test.txt","rb");
if(fileNm == NULL){
fprintf(stderr,"Unable to File ! : %s\n",fileNm);
return FALSE;
}
fclose(fp);
return TRUE;
}
int fnWritePPM(char* fileNm,PPMIMG* img)
{
FILE *fp =fopen(fileNm,"w");
if(fp == NULL)
{
fprintf(stderr,"Failed to create the file.");
return FALSE;
}
return TRUE;
}
Unable to File ! : (null)
Program ended with exit code: 0
解决方法
您错误地测试了 fopen 返回值:
fp = fopen("/users/ashton/Downloads/test.txt","rb");
if (fp == NULL) { //<===== Was WRONG,you used fileNm instead of fp
perror("Unable to File");
return FALSE;
}
,
问题很可能在这里:
int fnReadPPM(char* fileNm,PPMIMG* img)
{
FILE* fp;
fp = fopen("/users/ashton/Downloads/test.txt","rb"); // you assign fp
if (fileNm == NULL){ // and here you check for fileNm
fprintf(stderr,"Unable to File ! : %s\n",fileNm);
return FALSE;
}
...
你想要这个:
int fnReadPPM(char* fileNm,"rb");
if (fp == NULL) {
fprintf(stderr,"Unable to open file %s.\n",fileNm);
return FALSE;
}
...
但是在 fnWritePPM
函数中,您做得对。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。