-2

I'm prompting the user for the file name, but once the user presses enter, it takes that into the file name as well. so the file is never found.

int main (){
char file[100];
FILE *fp;

printf("Please enter a valid filename:\n");
fgets(file,100,stdin);
fp=fopen(file, "r");

if(!fp){
printf("File not found.\n"); \\This will always happen because a new line is added to the user's input.
return 1;}

If I use

scanf("%s", file);

The issue doesn't happen, but I heard scanf is not a good function to use and would introduce new issues. How can I solve the new line problem with fgets?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Osama Qarem
  • 1,359
  • 2
  • 13
  • 15
  • 1
    But also see [When using gets to get a file name in C, the file opens but when using fgets it does not](http://stackoverflow.com/q/25311542/1281433), and [fgets() Not Ignoring New Line](http://stackoverflow.com/q/21270323/1281433). – Joshua Taylor Sep 02 '14 at 11:11
  • 1
    -1 for "This question does not show any research effort". A [Google search for `fgets Introducing new line in user's input site:stackoverflow.com`](https://www.google.com/search?q=fgets+Introducing+new+line+in+user%27s+input+site%3Astackoverflow.com) (that is, the *title of this question*) brings up that duplicate as the third result. – Joshua Taylor Sep 02 '14 at 11:13

2 Answers2

2

After fgets(file,100,stdin);, do this file[strlen(file)-1]='\0';, it will remove \n from the code. To use strlen() function you need to include string.h in your code.

Try this modified code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (){

    char file[100];
    FILE *fp;

    printf("Please enter a valid filename:\n");

    fgets(file,100,stdin);
    file[strlen(file)-1]='\0'; //Removing \n from input
    fp=fopen(file, "r");

    if(fp==NULL)
    {
        printf("File not found.\n");
        return 1;
    }
    else
    {
        printf("File found!\n");
        fclose(fp);
        return 0;
    }
}
ani627
  • 5,578
  • 8
  • 39
  • 45
-2

fgets() returns the \n new line code.... that's what it does. You must wipe out that character.

Given that overflowing, or at least totally filling, incoming buffers is a popular attack vector I prefer code that defends against such.

char *cp;
file[(sizeof file)-1)] = '\0'; /* assure \0 termination on buffer fill attack */
cp = strchr( file, '\n' );     /* find expected \n, but allow for none */
if ( cp ) *cp = '\0';          /* safely clear closing \n */
Gilbert
  • 3,740
  • 17
  • 19
  • Your heart is in the right place, but the `\0` check does not prevent reading uninitialiazed data. Instead you can check the return value of `fgets`. If it retuns `NULL` then don't read `file` at all; otherwise you can be guaranteed that the buffer contains a string. – M.M Sep 02 '14 at 11:33
  • Challenge: Been awhile since you wrote your comment, BUT nothing in your words address a total buffer full condition, which what I'm guarding against. Sure fgets() returns NULL on EOF/Error, but that's another bug. I'm guarding against overlong names where the final \n\0 are not posted because they would go beyond the end of the buffer. No good fputs() should allow for this (but I tend to have two extra guard-bytes in fear of buggy fgets()). Nothing in fgets() return value gives you a hint buffer full has happened. Call me paranoid but I fear buffer overflow conditions by accident or design. – Gilbert Jul 23 '15 at 00:42