0

I have the below code (not mine) trying to get it to work out of curiousity. from here C low-level standard-in to accept filename then printing file contents to stdout

int checkfile(void)
{
   char buffer[4096];
   char userInput[100];
   int input_file1;
   int n;

   /* Check file existence.  */ 

   printf("Enter the filename: \n");
   fflush(NULL);

   if (!fgets(userInput,sizeof(userInput),stdin))
   {   
      perror("fgets"); 
      exit(EXIT_FAILURE); };  

   if((input_file1 = open(userInput, O_RDONLY)) < 0)
   {
      perror(userInput);
      exit(1);
   }

   while((n = read(input_file1, buffer, sizeof(buffer))) > 0)
   {
      if((write(STDOUT_FILENO, buffer, n)) < 0)
      {
         perror("failed to display file to output");
         close(input_file1);
         exit(1);
      }
   }
}

whenever i run the code and try to open a file called "success.txt" i get a segmentation fault

"RUN FINISHED; Segmentation fault; core dumped; real time: 3s; user: 0ms; system: 0ms"

i'm also calling this from my main code as

checkfile();

if that makes any difference.

Could someone point out to me what i'm missing because i can't see it at the moment. i have a feeling some of my variables are not set properly...but unsure thank you.

Community
  • 1
  • 1
LewisFletch
  • 125
  • 3
  • 6
  • 16

1 Answers1

3
if (!fgets(userInput,sizeof(userInput),stdin))

is wrong on couple of accounts.

  1. userInput does not point to any valid memory.
  2. sizeof(userInput) is the same as sizeof(char*), which is not what you want.

Change

char *userInput;

to something like:

char userInput[100];

Next problem

if((input_file1 = open(userInput, O_RDONLY)) < 0)

That's wrong. The return value of open is an int. Type of input_file1 is FILE*. I am surprised you didn't get compiler errors/warnings.

Change

FILE *input_file1;

to

int input_file1;

And the next problem

It's probably caused by fgets() including the newline character in userInput. Add code to trim the newline.

int len = strlen(userInput);
if ( userInput[len-1] == '\n' )
{
   userInput[len-1] = '\0';
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • okay awesome, that solved my seg fault. now it's completing successfully but still not displaying the contents of my file, just goes onto the next bit of code in my main function?? seems that it isn't writing to stdout – LewisFletch Apr 05 '15 at 06:19
  • Yeah didn't get compiler errors at all. alright so after updating that bit of code i now get "No such file or directory" even though the file is sitting in the root directory of the compile folder..? any idea why this might be? sorry still wrapping my head around file handling... thanks @R Sahu – LewisFletch Apr 05 '15 at 06:30
  • may sound like a stupid question, but am i adding that within the `fgets()` `if` nest? where perror(userinput) is?? – LewisFletch Apr 05 '15 at 06:39
  • @LewisFletch, add those lines right before the call to `open`. – R Sahu Apr 05 '15 at 06:40
  • or adding it to `fgets(userInput[len-1] == '\n', sizeof(userinput),stdin))`?? – LewisFletch Apr 05 '15 at 06:40
  • 1
    @LewisFletch, No, not at all. That raises some red flags. You don't seem to understand the basics of how these functions work. – R Sahu Apr 05 '15 at 06:42
  • ah no makes sense now. i just took a wild guess. thanks for your help. – LewisFletch Apr 05 '15 at 06:56