Thread: getting 1000 chars at a time from stdin

  1. #16
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    ok.. im assured that the input will always be digits, and im not sure how to break out of both loops from the inner one. But my main problem is still there, when i enter a large number, the final digit isnt printed.

    please help

    Code:
    #include <stdio.h>
    
    #define maxSize 10001
    
    int main (int argc , char** argv) {
    
    	int counter = 0;
    	char myArray[maxSize];
    	
    	do {
    
    		if (fgets(myArray, maxSize, stdin) == NULL)
    			break;
    		
    		for (counter=0; counter<maxSize; counter++) {
    			
    			if (myArray[counter] != '\n' && myArray[counter] != '\0') {
    				printf("&#37;d", myArray[counter]-'0');
    			}
    			else
    				break;
    		}
    
    	}
    	while (myArray[counter] != '\n' && myArray[counter] != '\0');
    	
    	return 0;
    }

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Waxy, I believe you're focusing on the details here, rather than the right logic design.

    I have another idea for this:

    Use c=getchar (which actually returns to c an unsigned int), to get all the digits. If there's a million digits, that's OK, because after being checked that each one IS a digit, then they get written out to a file. The function isdigit() is made for this, btw.

    10 million digits? You can take as many as your HD will have space for. And while you're taking in digits, you count how many you've taken in. When the count reaches 1000 ((number % 1000 == 0) && (number > 0)). then your program will write out an end of string char - '\0'.

    When you get all the digits into the digits_in file, then close it.

    Now re-open the digits_in file, and use fgets() to read in each of the 1000 digit strings, which will be no problem since you've put the end of string chars into the file.

    (you could also just count them in from the file, digit by digit. No fun, but computers love that counting gig!).

    You'll have substantial problem handling this logic, in "one pass", correctly. Nothing I've seen so far will do what you want. At best, they'll take in 1,000 digits and exit the program.

    That's not what you said you wanted.

    Adak

  3. #18
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    so if the number entered was 1000000.. etc there is no way to just get 1000 or even 500 digits at a time into a buffer until ive looped through them all? apart from your file way.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by waxydock View Post
    so if the number entered was 1000000.. etc there is no way to just get 1000 or even 500 digits at a time into a buffer until ive looped through them all? apart from your file way.
    Never say never!

    Easier, certainly. The first pass does the checking, sets everything up to be read as a string, which printf() will very nicely change into regular digits just by using %d, instead of %c.

    To do it in one pass you'd need a loop (including a counter), to check each char (is it a digit or what?), then you'd need some memory to hold this (maybe), outrageously huge number.

    Array's won't always be able to handle a number with a million digits (and is the million figure really the upper limit?). I wouldn't trust a linked list with a million nodes, either. Sure, each node could handle several digits, but now you're introducing another complexity to the code stew. Anything that handles the digits as something in "chunks", other than as a "stream", is going to have to handle putting the number back together again, too.

    The writing and reading from the HD would be quite fast (done in large bunches naturally by the HD controller), so you wouldn't be losing much time. The HD could be working while your input of further numbers, was still going on.

    Can you think of something better?

    Adak

  5. #20
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    i thought my was good good.. but i think i misunderstood on how to do this. I thought that this line would keep getting 1000 digits at a time from the input. Am i wrong?

    Code:
    if (fgets(myArray, maxSize, stdin) == NULL)

  6. #21
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    This is the sudo code they gave in the question...

    Code:
    let the length of a buffer = x
    
    read digits as many as x from standard input
    
    while ( the buffer contains some digits ) do {
    
       //whatever
    
       read next x digits from standard input
    }
    The teacher makes it so simple, he said we should use fgetc or fgets .. those type of functions .. thats why i don't think i need to do that file stuff you mentioned..

    anyone have any ideas on how to implement this sudo code?

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by waxydock View Post
    This is the sudo code they gave in the question...

    Code:
    let the length of a buffer = x
    
    read digits as many as x from standard input
    
    while ( the buffer contains some digits ) do {
    
       //whatever
    
       read next x digits from standard input
    }
    The teacher makes it so simple, he said we should use fgetc or fgets .. those type of functions .. thats why i don't think i need to do that file stuff you mentioned..

    anyone have any ideas on how to implement this sudo code?
    It's elementary to do the above, but NOT (imo), for a million digits. Contrary to our politicians in Washington, a million (bucks in their case, digits in ours ), is really respected as a VERY large number.

    Did your teacher mention what kind of data structure he thought would be suited to hold a million digits?

    There are ways to pack bytes with digits, but it's WAY above your skill in C, and I've done it like once, years ago. Hard drive is there spinning anyway, and has plenty of storage these days.

    Adak

  8. #23
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    but you never store the million digits right? you just store 1000 at a time, or am i wrong?

    also can an int array hold 1000000 + digits?

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since you're not actually using numbers, and are just reading strings anyway, use an array of char since all you're storing is one digit in each one anyway.


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

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're wrong!

    I thought the goal was to be able to store all the million digits - or more. Not just a thousand, but a thousand digits making up one string of char's, then get the next thousand, etc.

    Whatever the plan, they should be stored as char's, to save space, as Quzah and others, have suggested.

    Try getting as large a char array as you can on your system. Try using malloc to get the memory you need dynamically, and try getting it just from the stack (without using malloc). Some newer compiler's will allow you to declare huge memory structures - other's (like my old standby), can't do that. If there's one thing to learn from this, I believe it is simply that no matter HOW big a memory your system has, it might not be enough, by itself.

    Adak
    Last edited by Adak; 04-19-2007 at 12:26 AM.

  11. #26
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    no.. my plan was to just get 1000 digits at a time.. there is no need to store everything.. i just need to proccess the 1000 digits, then get the next lot of 1000 (ignoring the prev. 1000 that i had before)

    how do i do this? please help cause i need this urgently.
    a simple code example would be great

    Thank you
    Last edited by waxydock; 04-19-2007 at 07:06 AM.

  12. #27
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Then the logic would be the same as Adak mentionned without writing everything to a file. You store the digits you read in a 1000 length integer array. When you've reached 1000 read, or reached an EOF or newline, then you stop, and you process whatever you need to do with that 1000 length array.
    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

  13. #28
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    then how do i get the next 1000 from the same input

    thats what i was trying to do here

    Code:
    #include <stdio.h>
    
    #define maxSize 10001
    
    int main (int argc , char** argv) {
    
    	int counter = 0;
    	char myArray[maxSize];
    	
    	do {
    
    		if (fgets(myArray, maxSize, stdin) == NULL)
    			break;
    		
    		for (counter=0; counter<maxSize; counter++) {
    			
    			if (myArray[counter] != '\n' && myArray[counter] != '\0') {
    				printf("&#37;d", myArray[counter]-'0');
    			}
    			else
    				break;
    		}
    
    	}
    	while (myArray[counter] != '\n' && myArray[counter] != '\0');
    	
    	return 0;
    }
    if someone knows how to do what im after, please give some code, ive spent days on this, i still need to write alot more code but cant without this.
    Last edited by waxydock; 04-19-2007 at 07:41 AM.

  14. #29
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    It's not like the solution has already been mentioned several times in this thread. Each call to fgets(..., 1001, stdin) reads up to 1000 digits (+ 1 for the \0), only the last call will read less than 1000 digits (+ the final fgets call that reads nothing and will exit the loop)

  15. #30
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    isnt that what my code is doing?? please check it for me and see if thats what you mean.

    Code:
    #include <stdio.h>
    
    #define maxSize 10001
    
    int main (int argc , char** argv) {
    
    	int counter = 0;
    	char myArray[maxSize];
    	
    	do {
    
    		if (fgets(myArray, maxSize, stdin) == NULL)
    			break;
    		
    		for (counter=0; counter<maxSize; counter++) {
    			
    			if (myArray[counter] != '\n' && myArray[counter] != '\0') {
    				printf("&#37;d", myArray[counter]-'0');
    			}
    			else
    				break;
    		}
    
    	}
    	while (myArray[counter] != '\n' && myArray[counter] != '\0');
    	
    	return 0;
    }
    This is really urgent, if someone could please tell me where to fix this .. it would be great.
    Last edited by waxydock; 04-19-2007 at 08:55 AM.

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