【发布时间】:2012-04-17 22:54:25
【问题描述】:
我一直在尝试对我的代码进行错误检查,以确保给定的参数是有效文件。以下代码:
int main(int argc, char *argv[]){
for(int i = 1, i <=argc, i++){
FILE *fileIn = fopen(argv[i],"r");
if(fileIn == NULL){
fprintf(stderr,"The file %s doesn't exist.",fileIn);
}
else if (fileIn != NULL){
do a bunch of stuff, including printing out values from a struct
}
我遇到的问题是,当我运行我的程序时,我得到了这个输出:
The (null) file doesn't exist
Output from else if loop
所以基本上,它是说 fileIn 为 null 并且同时不为 null。它正在正常读取文件并按应有的方式进行操作,但每次都会遇到错误。有没有办法将指针分配给 argv[i 或其他东西?这是怎么回事?
【问题讨论】:
标签: c file if-statement error-handling null