Using *gets(char *)

This is a discussion on Using *gets(char *) within the C Programming forums, part of the General Programming Boards category; Im using the function char *gets(char*), and here's the code. Code: sprintf(line,"%s",input); if( ((gets(input)) == NULL) ) { break; } ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    Using *gets(char *)

    Im using the function char *gets(char*), and here's the code.

    Code:
    
    	  sprintf(line,"%s",input);
    
    	   if( ((gets(input)) == NULL) ) 
    	   {
    	       break;
    
                        }
    I checked if input was getting read, and it was, so data is in input, however, it still returns NULL for some reason and it breaks out of the function, and I dont know why that is. Please help. Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read this.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    215
    Hey quzah, thanks for the help. I had been using fgets() for my program when it was in a windows console program, but i have changed it to a win32 program and i was trying to change it by using gets instead. however, what ive done now is i have created a file where the command gets copied to the file and then the program uses fgets to read in from that file so it gets the command now. just thought it was a little easier to do it that way.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know you can use stdin in the file field to read from the keyboard using fgets, right?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    215
    Well I was doing that through my regular windows console program, but it wouldnt work for my win32 gui application.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    215
    Plus, a lot of the things im working with is using the mouse and stuff. The user hardly types anything in for the win32 gui application.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Where is the input actually coming from? Is it coming from an edit control, a console or somewhere else?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    None of which is a reason for using gets()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Is there a gets() convention atm, or something?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    215
    Well what im doing is, i have the user click what they want and whatever, and then when they click Get Data, it stores all the info into a char array, and then i write that to a file called client.stp. then i have the program close it and then open that file for reading in, and then i call the do_commands(array) function and it reads the command in. Before in the console one all i had to do was this:

    Code:
    do_commands(stdin);
    and everything worked fine.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>when they click Get Data, it stores all the info into a char array
    Again, where's the data coming from? (see post by anonytmouse)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by osal
    Well what im doing is, i have the user click what they want and whatever, and then when they click Get Data, it stores all the info into a char array, and then i write that to a file called client.stp. then i have the program close it and then open that file for reading in, and then i call the do_commands(array) function and it reads the command in. Before in the console one all i had to do was this:

    Code:
    do_commands(stdin);
    and everything worked fine.

    do_commands() is expecting a FILE* that it can read the input from. So assuming you have your input ready in client.stp and you have opened it with something like:
    Code:
    hInputFile = fopen("client.stp", "r");
    you would pass it to do_commands():
    Code:
    do_commands(hInputFile);
    and once do_commands() has finished reading in all the info from hInputFile you can close it:
    Code:
    fclose(hInputFile);
    There should be no reason to alter do_commands(). This function should work, whether it is reading in from stdin or hInputFile. Both are readable file handles (FILE*).

    If you are still having trouble it may be worth posting the code section that is causing the problem.
    Last edited by anonytmouse; 07-06-2004 at 03:52 PM.

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    215
    Yeah thats exactly what i have done. But i wanted to do was instead of taking in a file, i was trying to change it so it takes in a char so it would be like

    do_commands(char * input)
    and then id copy input to line
    and then try to work it that way, but it didnt really pan out the way i wanted it to. I tried using strcmp to check if it was null instead of the gets function, but instead it would go into some sort of infinite loop and keep putting the command in over and over. so I guess ill keep it the way i had it with creating a new stp file and then reading it in. I was just thinking if there was an easier way to do with with a char array.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some more of your code, something that show where you're at. Preferably just a snippet, but compilable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    So, you need to get your input from a string(char*) rather that an input stream(FILE*).
    I think, in your case, you can just replace:
    Code:
                     // input is a FILE*
                     if( fgets(line,MAXLINE,input) == NULL )
    with:
    Code:
                     // input is a char*
                     strncpy(line, input, MAXLINE);
                     line[MAXLINE - 1] = '\0';
    This may get more than one line, but I don't think this matters in your code.

    If you do need only the one line you can use this function:
    Code:
    char* GetLineFromString(char* out, int max_length, const char* in)
    {
    	char* pnew_line  = strchr(in, '\n');
    	int   char_count;
    
    	if (pnew_line)
    	{
    		char_count = min(max_length - 1, (pnew_line - in) + 1);
    	}
    	else
    	{
    		char_count = max_length - 1;
    	}
    
    	strncpy(out, in, char_count);
    
    	out[char_count] = '\0';
    
    	return (strlen(out) ? out : NULL);
    }
    
    GetLineFromString(line, MAXLINE, input);
    or maybe someone will post a much nicer way to use sscanf() to do this.

Popular pages Recent additions subscribe to a feed

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