#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);
}
問題
printf 顯示帶有路徑的正確檔案名:“F:\c\projects\Banking Management System\data\pwd.txt”
但是,perror 顯示:錯誤:無效的引數
它不會創建/寫入檔案,即使我洗掉了以下條件:
if (fp==NULL){ perror("\n錯誤:"); 退出(EXIT_FAILURE);}
為什么 perror 顯示“無效引數”?
uj5u.com熱心網友回復:
檔案名沒有被引號包圍。您只需在從命令提示符呼叫檔案以轉義檔案名和空格時使用它們。
所以從宏中取出參考:
#define FILENAME(path, f_) path f_
...
printf("\n%s", FILENAME(PATH, F_PWD));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/356332.html
上一篇:在C中實作鏈表
