我正在試圖編寫一個簡單的程序來寫入一個.txt文件,但是這段代碼不起作用。fprintf無法正常工作C
#include <stdio.h>
#include <string.h>
#include "main.h"
int main(int argc, const char * argv[])
{
FILE *f = fopen("text.txt", "w+");
char c[256];
printf("What's your name?\n");
scanf("%s", c);
fflush(f);
if (c!=NULL)
{
printf("not null\n");
int q = fprintf(f, "%s", c);
printf("%d", q);
}
else
{
printf("null\n");
}
printf("Hello, %s\n", c);
fclose(f);
return 0;
}
的printf
回報,它不爲空,且int q
返回無論字符的長度。爲什麼不寫這個文件?
我想上的Microsoft Visual C++程序,它工作正常。我在工作目錄中觀察到一個text.txt文件。你能分享關於你的環境的更多細節嗎?另外,你爲什麼包含'main.h'? – Ganesh 2013-03-07 03:01:38
'c'不會爲空,不需要檢查。相反,確保'f'不爲空。 – perreal 2013-03-07 03:01:49
並打開一些編譯器警告... – 2013-03-07 03:03:26