Return codes....

This is a discussion on Return codes.... within the C Programming forums, part of the General Programming Boards category; Code: while fgets(buf,sizeof(buf),stdin) != NULL when would this ever return NULL? Because it seems that just hitting enter it doesn't, ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    Return codes....

    Code:
    while fgets(buf,sizeof(buf),stdin) != NULL
    when would this ever return NULL?

    Because it seems that just hitting enter it doesn't, and that's what I figured would make it return NULL. Same issue with Scanf, I can get my program to do what I want, but I'm wondering if someone can provide an example where this would ever return null.... thanks

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    fgets returns NULL on end of file or error.

    So on a unix system, if the user hit Control-D before typing anything, or on a Windows system, if the user hit Control-Z before typing anything.

    This will also come up if you read stdin from a file instead of a terminal.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    oh ok thanks much

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Just another quick question, same subject
    So in this example, when it gets to "rc = sscanf(ui,"%d%n",&input,&nchars);" , because that comes under the "while(fgets..." loop, is it actually only reading 1 value of ui, and then loops back to the while(fgets statement, to loop through it all, or does rc=sscanf actually read the entire string ui the first time you call it?

    On a side note, DevC is a great compiler, but it won't let me use // to make comments in my program, only */ /*, weird.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void mm();
    
    int main()
    {
      mm();
      return 0;
    }
    
    void mm() {
         int input;
         int nchars;
         
         char ui[4];
    
         int rc;     /*for testing the returncode */
         char spaces[] = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
         system("cls"); 
         printf(spaces);
         printf("\t\tWelcome to Smith Sayings - Freeware Edition\n\n");
         printf("\t\t            Choose an option:\n\n");
         printf("\t\t               1.Run Story\n\n");
         printf("\t\t               2.About\n\n");
         printf("\t\t               3.Exit\n\n");
    
         while (fgets(ui,sizeof(ui),stdin) != NULL) {
    
    
               rc = sscanf(ui,"%d%n",&input,&nchars);
    
               if (rc == 1) {
    
                  if (ui[nchars] == '\n') {
                                 if (input == 1) { printf("Run Story"); getchar();/*runstory();*/ }
                                 if (input == 2) { /*about();*/ }
                                 if (input == 3) { exit(0); }
                                 }
                                 else mm();
                                 }
              mm();
    
              }

  5. #5
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by Ash1981
    J
    So in this example, when it gets to "rc = sscanf(ui,"%d%n",&input,&nchars);" , because that comes under the "while(fgets..." loop, is it actually only reading 1 value of ui, and then loops back to the while(fgets statement, to loop through it all, or does rc=sscanf actually read the entire string ui the first time you call it?
    sscanf() returns only one value, so it will read 1 value

    On a side note, DevC is a great compiler, but it won't let me use // to make comments in my program, only */ /*, weird.
    Try DevC++. It is a great compiler.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    thanks

  7. #7
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Ash1981
    Just another quick question, same subject


    On a side note, DevC is a great compiler, but it won't let me use // to make comments in my program, only */ /*, weird.
    [

    Using // for comments in C is in the C99 standard. If you can set compiler options in DevC to allow C99, you might be able to do that.

    I use the C99 stuff myself, but I use gcc on Linux, and am not familiar with DevC.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Thanks, I'll try that - btw, in the program above, I decided I didn't want a ctrl - z being able to allow the person to escape the loop, so I took out the while statement, and left it just as fgets(ui,sizeof(ui),stdin); - now when a user just enters a letter, like "m" for some odd reason the program terminates...

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    nevemind, i see what i did and fixed it

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    I was noticing, that if you clear the buffer at some time when the user didn't enter more than what the string could hold, that the user will have to hit enter to make it through your "while((cb=getchar()) != '\n'" line, whereas if the user did type more than what the string could hold, the clear buffer works just fine. I was wondering if this is the reason in all the examples I've seen, people have user char buf[BUFSIZ], does that "BUFSIZ" represent the whole buffer, so that they could never enter more char's than your string could hold, so that that's not a problem? Am i making sense?

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    btw, I just tried that BUFSIZ, same problem. If there are extra characters over the size of the string, cb gets rid of them like it should. However, if not, when the cb=getchar line comes up, the user has to hit enter to get past it....

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Here's an example below, same problem. I know that it's highly unlikely for a user to type 255 characters or whatever, but just the same, I'd like to have a program where if they do it stops it. My problem is that if the user types more than the size of STRING, then everything works fine when I clear the buffer. but if they DON'T, then when my program gets to the line designed to clear the buffer, the user has to hit enter to provide it with that \n it's waiting for. How can I fix this? Thanks


    Code:
    #include <stdio.h>
    
    int main() {
        int i;
        int cb;
    
        char string[BUFSIZ];
    
        fgets(string,sizeof(string),stdin);
        for (i = 0; i < sizeof(string); i++) {
            if (string[i] == '\n') { string[i] = '\0';}
            }
         //I had left this part out before, making the '\n' a '\0'
         //however, same issue, even setting the size to BUFSIZ, if the user enters
         //6 solid lines of garbadge, it's enough to over flow it, and that getchar down
         //below grabs the \n like it should not. But if I put in a line to clear the buffer, then
         //the user has to hit enter to get past that line if they didn't overflow the string size.
    
    
        printf("got it");
    
        getchar();
    
        return 0;
    
       } //as this example illustrates, setting the string length only to 2 makes it so that you don't need to
       //set up a loop to take the \n and change it to \0 like you normally would with fgets, because in this
       //case it doesn't have room to store it, so it stores the 1st letter, then the \0, and tosses the \n in the buffer.
       //However, you would then need to clear the buffer, so that the getchar doesn't eat up the \n. But if you do that
       //then if the user just hits ENTER at the fgets, he has to hit enter 2 more times, because the while((cb=getchar))
       //eats up the second \n. So it's better to just always have the string value be at least 3(for where you only need
       //1 single digit value), so there's room for the value, the \n, and the \0. Of course, then you'd still probably
       //need to clear the buffer at some point. And if they didn't type more than what fgets read, then clearing the
       //buffer will force the user to hit ENTER to continue when you do it, but if they did, it will eat up the extra
       //characters like it should.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21