Home > OS >  Why do I get FILE pointer error while checking NULL condition?
Why do I get FILE pointer error while checking NULL condition?

Time:11-11

#include<stdio.h>
#include <string.h>
#include <windows.h>

#define PATH "F:\\c\\projects\\Banking Management System\\data\\"
#define F_PWD "pwd.txt"

#define FILENAME(q1, path, f_)  q1 path f_ q1

void main() {
    printf("\n%s", FILENAME("\"",PATH, F_PWD));

    FILE *fp=fopen(FILENAME("\"",PATH, F_PWD), "w");

    if (fp==NULL){
        perror("\nERROR: ");
        exit(EXIT_FAILURE);
    }

    fprintf(fp,"%d,%s,%f\n",1,"Anil Dhar",1000.00);
    fclose(fp);
}

Problem

  1. The printf shows the correct file name with path: "F:\c\projects\Banking Management System\data\pwd.txt"

  2. However,perror shows: ERROR: Invalid argument

  3. It doesn't create/write into the file, even if, I remove the following condition:

    if (fp==NULL){ perror("\nERROR: "); exit(EXIT_FAILURE); }

Why is perror showing "Invalid argument" ?

CodePudding user response:

A file name isn't surrounded by quotes. You just use those when calling out a file from the command prompt to escape and spaces in the filename.

So take the quote out of the macro:

#define FILENAME(path, f_)  path f_
...
printf("\n%s", FILENAME(PATH, F_PWD));
  • Related