Warm tip: This article is reproduced from serverfault.com, please click

其他-在C程序中读取文件找不到指定的文件

(其他 - reading In a file in C program cannot find file specified)

发布于 2020-11-28 04:49:53

我是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);
}

Questioner
YHapticY
Viewed
0
21.1k 2020-11-28 13:23:06

关于你的问题,你的代码似乎只在你引用我相信的文件的地方起作用。

你把 fp = fopen("time_logs/time.log" , "r");

但我相信你想要 fp = fopen("./time_logs/time.log" , "r");

./使它所以你refrencing相对项目路径则包含的文件,然后最后文件的文件夹。

埃文:D