[英]reading In a file in C program cannot find file specified
我是 C 編程的新手,並試圖讓這個簡單的程序運行以從名為“time_logs”的目錄中讀取文件,該目錄包含我要從中讀取的文件,名為“time.log”我並不完全確保我有正確的語法,但是當我嘗試在 Visual Studio 2019 中運行該程序時,我收到消息“無法啟動程序 C:programpath///.... 系統找不到指定的文件”
我最初在 atom 中編寫了這個程序,然后用 Visual Studio 打開它,我猜我可能沒有自動創建一些必需的文件,如果我通過 Visual Studio 創建程序,這些文件通常會在那里,但我我不確定。 任何輸入都會很有幫助,謝謝。
#include <stdio.h>
int main () {
FILE *fp;
char str[100];
/* opening file for reading */
fp = fopen("time_logs/time.log" , "r");
if(fp == NULL) {
perror("unable to open file");
return(-1);
}
if( fgets (str, 100, fp)!=NULL ) {
puts(str);
}
fclose(fp);
return(0);
}
關於您的問題,您的代碼似乎就在您引用我相信的文件的地方工作。
你把fp = fopen("time_logs/time.log", "r");
但我相信你想要fp = fopen("./time_logs/time.log", "r");
./
使您可以引用相對項目路徑,然后是包含文件的文件夾,最后是文件。
埃文:D
time_logs/time.log 是您要打開的文件的名稱。 指定完整路徑。
例如“C:\\myfiles\\time_logs\\time.log”
time.log 文件應該在您的指定目錄中可用
#include <stdio.h>
int main () {
FILE *fp;
char str[100];
/* opening file for reading */
fp = fopen("C:\\myfiles\\time_logs\\time.log" , "r");
if(fp == NULL) {
perror("unable to open file");
return(-1);
}
if( fgets (str, 100, fp)!=NULL ) {
puts(str);
}
fclose(fp);
return(0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.