1
#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" ?

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed. Removing quotes did the trick. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.