使用 fopen() 时文件指针未被赋值

File Pointer Not Being Assigned a Value When Using fopen()

我正在尝试编写一个简单的 C 程序,它将从 csv 文件中读取数据并对这些数据执行一些计算。

不幸的是,我的文件指针 fptr 在调用 fopen() 后没有被赋值。我知道在单步执行 VS 2017 的调试器后就是这种情况。但我不知道为什么会这样。这是一个大问题,意味着每当我尝试从文件中读取数据或关闭文件时,我的程序都会抛出一些非常讨厌的异常。

我的代码如下:

main.c

#include<stdio.h>
#include <stdlib.h>         // For exit() function
#include"constants.h"       //For access to all project constants

/***************************************************************************************************************
To keep the terminal from automatically closing
Only useful for debugging/testing purposes
***************************************************************************************************************/
void preventTerminalClosure() {
    //flushes the standard input 
    //(clears the input buffer) 
    while ((getchar()) != '\n');
    printf("\n\nPress the ENTER key to close the terminal...\n");
    getchar();
}

/***************************************************************************************************************
Read the given input file
***************************************************************************************************************/
void readInputFile(char fileName[]) {
    FILE *fptr;
    char output[255];

    //open the file
    if (fptr = fopen(fileName, "r") != NULL) {          //read file if file exists
        //fscanf(fptr, "%[^\n]", output);
        //printf("Data from the file:\n%s", output);
        printf("<--Here-->");
    }else {                             
        printf("\nERROR 1: File %s not found\n", fileName);
        preventTerminalClosure();
        exit(1);
    }

    fclose(fptr);                       //close the file
}

/***************************************************************************************************************
                                        *   *   *   Main    *   *   *
***************************************************************************************************************/
void main() {
    char testName[MAX_NAME_SIZE];

    printf("Hello World!\n");
    printf("Please enter your name: ");
    scanf("%s", testName);

    printf("It's nice to meet you %s!", testName);

    readInputFile("dummy.txt");

    preventTerminalClosure();   //Debug only

}

我已经确定我的假文件确实存在并且位于正确的位置。否则我的代码会命中 readInputFile() 内的 else 块。那是我已经彻底测试过的东西。

很明显,我缺少一些基本的东西来解释这个指针的行为;但那是什么,我不确定。任何帮助,将不胜感激! :)

使用括号强制执行顺序,以便在 fptrNULL 分配由 fopen 返回的值后与 NULL 进行比较:

FILE *fptr;
char output[255];

//open the file
if ( (fptr = fopen(fileName, "r")) != NULL)