0

I'm new to C programing and was trying to get this simple program to run for reading in a file from a directory called "time_logs" that has the file that I want to read from called "time.log" I'm not entirely sure that I have the correct syntax for this but when I try to run this program in Visual Studio 2019 I get the message "Unable to start program C:programpath///.... The system cannot find the file specified"

I originally wrote this program In atom and then opened it with Visual studio, I'm guessing that I might not have some of the required files auto-created that would normally be there if I had created the program through Visual Studio, but I'm not sure. Any input would be helpful full thank you.

#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);
}

YHapticY
  • 177
  • 11
  • 1
    Does this answer your question? ["The system cannot find the file specified" when running C++ program](https://stackoverflow.com/questions/16511925/the-system-cannot-find-the-file-specified-when-running-c-program) – Krishna Kanth Yenumula Nov 28 '20 at 05:02
  • There is a problem in your project definitions, so the program is not started. The error is that Visual Studio is not able to find the executable file to run it. See Krishna's link for how to adjust the project's settings in VS. – wallyk Nov 28 '20 at 05:33

2 Answers2

1

In regards to your question your code seems to work its just where you are referencing the file I believe.

you put fp = fopen("time_logs/time.log" , "r");

but I believe you want fp = fopen("./time_logs/time.log" , "r");

The ./ makes it so you are refrencing the relative project path then the folder containing the files and then finally the file.

Evan :D

Dharman
  • 30,962
  • 25
  • 85
  • 135
1

time_logs/time.log is the name of the file, which you want to open. Specify the full path.

E.g. “C:\\myfiles\\time_logs\\time.log”

The time.log file should be available in your Specified Directory

#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);
}
Joundill
  • 6,828
  • 12
  • 36
  • 50
ChandanKC
  • 11
  • 3