[英]C - Can't create new .txt file from Xcode
我剛剛開始使用Xcode進行C編程。 但是我現在有一個問題。 我寫了這段代碼:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
FILE *fp;
fp = fopen("toto.txt", "w+");
fputs("hi", fp);
fclose(fp);
return 0;
}
由於toto.txt
不存在,應該創建並打開它,但是什么也沒發生,我不知道為什么。
我認為您的問題是Xcode當前的工作目錄設置。 您需要更新項目的當前方案。
現在,您的C項目將知道應該在哪里寫入輸出文件。
檢查fopen
的返回值。 創建文件可能失敗。 原因可能是某些權限。 您應檢查文件應寫入的當前目錄,或為文件名添加完整路徑。
int main(int argc, const char * argv[])
{
FILE *fp;
fp = fopen("/mydir/toto.txt", "w+");
if (fp != NULL)
{
fputs("hi", fp);
fclose(fp);
}
else {
printf("Failed to create the file.\n")
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.