Issue
#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
The printf shows the correct file name with path: "F:\c\projects\Banking Management System\data\pwd.txt"
However,perror shows: ERROR: Invalid argument
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" ?
Solution
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));
Answered By - dbush