Thread: getting 1000 chars at a time from stdin

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    getting 1000 chars at a time from stdin

    Hi all,

    My program has needs to ask for standard input.. the user can enter a single number (that could be millions of digits long) and i need to get 1000 digits at a time.

    Which function should i use? fgets()? but im not sure where i specify the 1000 digit buffer? Here is what i tried, could some one tell me what im doing wrong.

    Thank you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (int argc , char** argv) {
    
    	int buffer[1000];
    	int current;
    	
    	while (current = fgetc(stdin) != '\n') {
    		
    		current = current - '0';
    		
    		printf("&#37;d", current); 
    	}
    	
    	return 0;
    }

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Without using a specialized library, the maximum size for a number is 1E+37 when you store it as a float, this value is defined in float.h under the macro FLT_MAX. If you store it as an unsigned long, the biggest value is ULONG_MAX or 4294967295.

    If you want to store bigger numbers, find yourself a specialized library or read and store the number inside an array, where each place in the array could be of a numeric type, for example integer. But then you had to define your own operations so you could actually use that number.

    What do you need this for anyway ?

    And to answer your question:

    Code:
    int maxSize = 1000;
    char myArray[maxSize];
    while ((fgets(myArray, maxSize, stdin) != NULL)
    {
      // stuff
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by KONI View Post
    Without using a specialized library, the maximum size for a number is 1E+37 when you store it as a float, this value is defined in float.h under the macro FLT_MAX. If you store it as an unsigned long, the biggest value is ULONG_MAX or 4294967295.

    If you want to store bigger numbers, find yourself a specialized library or read and store the number inside an array, where each place in the array could be of a numeric type, for example integer. But then you had to define your own operations so you could actually use that number.

    What do you need this for anyway ?

    And to answer your question:

    Code:
    int maxSize = 1001;
    char myArray[maxSize]; /* C99 only */
    while ((fgets(myArray, maxSize, stdin) != NULL)
    {
      // stuff
    }
    Since we assume you wanted a 1000 digit number, you'll have to leave room for the null (and/or newline).


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

  4. #4
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    thanks .. but it keeps asking the user to input a number. I only require the user to enter 1 number.. how do i prevent it from continually asking.

    thanks.

  5. #5
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    i think you guys dont understand.. i need to receive a really large number from stdin, then get 1000 digits at a time from that big number. The code given is fine.. just it keeps asking for more input.. i just want to ask for input once.. and parse 1000 digits at a time, digit by digit.. if you get what i mean?

    please help, thanks
    Last edited by waxydock; 04-18-2007 at 07:41 AM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Very impatient. You didn't even wait an entire hour. LOL.

    This is something you should be able to do. Your programming is looping and you want to stop it. So take the loop out.

  7. #7
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    but i need the loop right? cause what will happen if the input number is greater than 1000 digits long... i need to read the next lot of digits in right?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Depends which block of code you're using. Thought you were using the one given to you that uses fgets() since you thanked him for the code and commented on it. If that is the case, the number is already read in one statement, unless you think the number could be greater than 1000. What you need to do then is check if the last char of the buffer (ie. buf[strlen(buf)-1]) is equal to '\n'. If it is, then you've read the number, and you can leave the loop.

  9. #9
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    k, this seems to work, but not sure if its logically right. Does this seem ok to you?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (int argc , char** argv) {
    
    	int counter = 0;
    	int maxSize = 1001;
    	char myArray[maxSize];
    
    	
    
    	while (myArray[counter] != '\n' && fgets(myArray, maxSize, stdin) != NULL ) {
    		
    		for (counter=0; counter<maxSize; counter++) {
    			
    			if (myArray[counter] != '\n') {
    				printf("&#37;d", myArray[counter]-'0');
    			}
    			else {
    				break;
    			}
    		}
    		
    	}
    	
    	return 0;
    }
    Last edited by waxydock; 04-18-2007 at 08:17 AM.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Did you even test it? I think not.

  11. #11
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    yes, why is something wrong?

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by waxydock View Post
    Code:
    	while (myArray[counter] != '\n' && fgets(myArray, maxSize, stdin) != NULL ) {
    On the first iteration through this loop, counter is zero, but myArray[counter] is some uninitialized value. So just BY CHANCE, this value could happen to be '\n' in which case your loop terminates before it reads a single byte.

    Before you start the loop, set myArray[0] = '\0';

    Also, although you do loop over and over, you simply overwrite the data you previously read. Thirdly, declaring myArray[maxSize] is not standard C (unless you are talking about C99). And since maxSize could conceivably be huge, even if it was standard, it would still be a bad idea.
    Last edited by brewbuck; 04-18-2007 at 09:53 AM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, you have very little error checking: what happens if one of the characters entered was not a space, newline, or digit?

    There doesn't have to be a newline at the end of stdin. DOS at least allows you to put an EOF at the end of a non-blank line:
    Code:
    some text^Z
    So checking for one as your ending condition is probably a bad idea. I would just skip spaces and newlines if you encounter them, and terminate on a '\0'. Of course, you'd then have to set myArray[0] to something other than '\0', or, better yet, use a do-while loop instead of a while loop.

    Also, you do not use anything from <stdlib.h>, so you don't have to include it.

    Turn on compiler warnings! Nearly every compiler could have told you that you were using myArray without initializing it. For Dev-C++, add -W -Wall -ansi -pedantic to the compiler's command line in the Compiler Options dialog.
    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.

  14. #14
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    Hi,

    I had a go at what you guys said but im having a problem when i enter a really big number in. The final digit in the large number is not printed... any ideas?

    Code:
    #include <stdio.h>
    
    #define maxSize 10001
    
    int main (int argc , char** argv) {
    
    	int counter = 0;
    	char myArray[maxSize];
    	
    	myArray[0] = '\1';
    	
    	while (myArray[counter] != '\n' && myArray[counter] != '\0' && fgets(myArray, maxSize, stdin) != NULL ) {
    		
    		for (counter=0; counter<maxSize; counter++) {
    			
    			if (myArray[counter] != '\n' && myArray[counter] != '\0') {
    				printf("&#37;d", myArray[counter]-'0');
    			}
    			else {
    				break;
    			}
    		}
    		
    	}
    	
    	return 0;
    }
    Last edited by waxydock; 04-18-2007 at 05:11 PM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - You're trying to check the contents of the array before you ever actually fill it up.
    2 - You're checking the contents of the array in the outer loop, instead of just breaking from it.
    3 - \1 isn't a valid escape sequence.
    4 - You assume all input is actually a digit, instead of just checking to be sure.


    That'll get you started.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM