Thread: help reading from stdin

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    help reading from stdin

    Hi.
    I want to read several words from stdin (save them in string) and then process this string.
    The problem is that my code works for the first time but when i finish processing i cannot read a new string from stdin, it enters in an infinite loop, printing Request.

    Code:
    while(1) { 
            printf("\nRequest\n>"); 
            
            if(fgets(string, sizeof(string), stdin)!=NULL) { 
                    sscanf(string,"%s",comand); 
                    type = validateComand(comand); 
    
                      if(type == 0) 
                            continue; /* invalid type */ 
    
                       ptr = &string[strlen(comand)+1]; 
    
                             if ((aux = strchr(string, '\n')) != NULL) /* Remove \n */ 
                                       *aux = '\0'; process(ptr,type); 
                        } 
                   } 
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    but you asked for an infinite loop (while(1))...

    So the program is doing just what you asked for.

    in the loop it
    1. prints Request
    2. reads the line from the input (works fine by me by the way)
    3. parses the string (haven't tested this part - hope you know what you're doing)
    4. executes some actions according to the command type (or no action if the command is unknown)
    5. returns to the beggining of the loop

    So what's the problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by vart
    but you asked for an infinite loop (while(1))...

    So the program is doing just what you asked for.

    in the loop it
    1. prints Request
    2. reads the line from the input (works fine by me by the way)
    3. parses the string (haven't tested this part - hope you know what you're doing)
    4. executes some actions according to the command type (or no action if the command is unknown)
    5. returns to the beggining of the loop

    So what's the problem?
    The problem is that the function is printing request in an infinite loop and don let me write the string, here is an example of what is happening:

    Code:
    Request:
    > GET user2:4 user1:5
    
    (the program execute and retunrs output)
    
    Request:
    >
    
    Request:
    >
    
    ...
    It let me write the firsty request but then i cannot write anymore, the program is oriting request in an infinite loop.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Maybe you should take a litle bit smaller sample and start with it?
    Like I did:
    Code:
    #include <stdio.h>
    
    int validateComand(char* arg)
    {
    	return 0;
    }
    int main(void) 
    { 
    	char string[2000];
    	char comand[100];
    	int type = 0;
    	while(1) 
    	{ 
    		printf("\nRequest\n>"); 
    
    		if(fgets(string, sizeof(string), stdin)!=NULL) 
    		{ 
    			sscanf(string,"%s",comand); 
    			type = validateComand(comand); 
    
    			if(type == 0) 
    			{
    				continue; /* invalid type */ 
    			}
    		} 
    	} 
    	return 0;
    }
    check if it is working, and only then start adding additional code?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    By the way here is my output:
    Request
    >test 1

    Request
    >resr 2

    Request
    >find 3

    Request
    >sdf

    Request
    >sdf sdf sdf sdf

    Request
    >

    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I can't find anything incriminating with your code. What compiler are you using?

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You're never telling your program when it should break out of the loop...so is it supposed to guess for you ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    The problem was with another function, called in that piece of code (it works now).
    Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 09:06 AM
  2. stdin + (ctrl+z), how i detect it?
    By Olimpo in forum C Programming
    Replies: 1
    Last Post: 09-30-2006, 05:33 AM
  3. reading from stdin
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 05-03-2006, 12:14 PM
  4. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM