Thread: Stdin & fgets problem

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    11

    Stdin & fgets problem

    Hello
    I could not solve why it exit with error :

    Code:
    using namespace std;
    int main(){
    
    
        
        char c[70];
        char l[80];
        fgets(c, 100, stdin);
        FILE *file;
        errno_t err = fopen_s(&file, c, "r");
        printf("err:%d",err);
        while (fgets(l, 100, file)) {
    
    
            char *s1 = strstr(l, "VELOCITY");
            char *s2 = strstr(l, "POSITION");
            //doing somethings with s1 and s2
        }
        
        
        fclose(file);
    
    return 0;
    }
    If I
    Code:
    fopen_s(&file, "some/path", "r")
    it is working . Why ?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,687
    Do not use magic numbers in your code.

    fgets - C++ Reference
    fgets info that makes your test different that real value.
    A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    11
    Quote Originally Posted by stahta01 View Post
    Do not use magic numbers in your code.

    fgets - C++ Reference
    fgets info that makes your test different that real value.

    Tried with replace '\n' with '\0' , same error. Could not understand your point ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,529
    > using namespace std;
    First off, this is C++ and not C.

    Don't just type in random stuff and expect it to work.
    That your visual studio defaults to C++ when you create a new project is a PITA when you're trying to write C.

    > char c[70];
    > char l[80];
    > fgets(c, 100, stdin);
    > while (fgets(l, 100, file)) {
    DO NOT LIE about your buffer sizes.
    There is no point protecting your toes with safety boots if you then go and lop off your entire leg with a chainsaw.

    > fopen_s(&file, "some/path", "r")
    vs
    > errno_t err = fopen_s(&file, c, "r");
    Because the buffer c will contain a \n from the fgets call you used to get input from the user.
    You need to remove that newline.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2019
    Posts
    11
    Quote Originally Posted by Salem View Post
    > using namespace std;
    First off, this is C++ and not C.

    Don't just type in random stuff and expect it to work.
    That your visual studio defaults to C++ when you create a new project is a PITA when you're trying to write C.

    > char c[70];
    > char l[80];
    > fgets(c, 100, stdin);
    > while (fgets(l, 100, file)) {
    DO NOT LIE about your buffer sizes.
    There is no point protecting your toes with safety boots if you then go and lop off your entire leg with a chainsaw.

    > fopen_s(&file, "some/path", "r")
    vs
    > errno_t err = fopen_s(&file, c, "r");
    Because the buffer c will contain a \n from the fgets call you used to get input from the user.
    You need to remove that newline.
    I already said removed newline and got same error again, not understand why fail? Is ıt because sizeof(c) ? I only want top open a file from user input and readlines. So please write more understandable...

  6. #6
    Registered User
    Join Date
    Jul 2019
    Posts
    11
    Quote Originally Posted by scorp08 View Post
    I already said removed newline and got same error again, not understand why fail? Is ıt because sizeof(c) ? I only want top open a file from user input and readlines. So please write more understandable...
    I put random but big enough buffers. It would be great if someone write logic of stdin and fgets using together ...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    You need to make better use of sizeof in order to avoid magic numbers, as was already mentioned, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char filepath[80];
        if (!fgets(filepath, sizeof(filepath), stdin))
        {
            fprintf(stderr, "failed to read file path\n");
            return EXIT_FAILURE;
        }
        filepath[strcspn(filepath, "\n")] = '\0';
    
        FILE *file;
        errno_t err = fopen_s(&file, filepath, "r");
        printf("err:%d", err);
        char line[100];
        while (fgets(line, sizeof(line), file))
        {
            char *s1 = strstr(line, "VELOCITY");
            char *s2 = strstr(line, "POSITION");
            //doing somethings with s1 and s2
        }
        fclose(file);
     
        return 0;
    }
    You also need to make it such that you act on an error from fopen_s, not just print the error code then continue as if there's no error.

    Notice that I named the variables with descriptive names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,529
    Quote Originally Posted by scorp08 View Post
    I already said removed newline and got same error again, not understand why fail? Is ıt because sizeof(c) ? I only want top open a file from user input and readlines. So please write more understandable...
    Saying you did something, and showing what you actually did are two completely different things.

    Another common source of trouble is mistaking what the programs idea of 'current directory' is, which often varies depending on whether you run it from the IDE, double-click on it in explorer, or run it directly from the command line.

    Show your work before complaining that something doesn't work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with stdin, fgets, and sscanf
    By cronuscronus in forum C Programming
    Replies: 9
    Last Post: 05-24-2014, 03:14 AM
  2. Fgets not reading stdin. Help please.
    By Kurtanius21 in forum C Programming
    Replies: 12
    Last Post: 11-26-2012, 09:17 PM
  3. fgets() from stdin and strcat()
    By rocketman03 in forum C Programming
    Replies: 9
    Last Post: 11-01-2008, 01:31 PM
  4. fgets() for stdin not working
    By Yasir_Malik in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 12:12 PM
  5. using stdin in fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 09:14 PM

Tags for this Thread