Thread: Reading large number from file

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

    Reading large number from file

    hi,

    Im trying to read a large number from a file, into a variable and then print it.

    This is what i have tried, but i am getting a seg. fault. (ive attached the file as well)

    Code:
    long current;
    FILE *inFile;
    
    if (!(inFile = fopen("tests/100.tst","r")))
    	fprintf(stderr, "tests/100.tst cannot be opened!\n");
    	
    else {
    	fscanf(inFile, "%d", &current);
    	printf ("\n%d\n", current);	
    }

  2. #2
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    just figured it out... i needed to use a float.

    and change the %d to %lf

    BTW should i be using a float or another data type????
    Last edited by waxydock; 04-13-2007 at 04:18 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Will a float handle 100 digits?

    They have big number libraries for people who need to work with numbers larger than C's standard files will handle.

    You can also roll your own big number functions.

    When you print out your float, does it equal the 100 digit number?

    Adak

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you go the float route, use a double instead of a float. But, yes, a large number routine might be necessary depending on what you are doing with it.

  5. #5
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    I see... the max digits i need to store would be 9999999 digits long.

    so should i make an int array 9999999 elements big? ie. int num[9999999]

    The program i am writing is to get the number from the file, then sum each digit of that number together.

    ie. if the number in the file was 12345, i should return 1+2+3+4+5 which is 15..

    Whats the best way to go about this
    Last edited by waxydock; 04-13-2007 at 05:14 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well even doubles usually have only 15 decimal digits of precision, so where the other 85 digits go is left as an exercise for the reader.
    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.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Are you trying to add the individual digits or read something into a very large number? That is, are you reading numbers from 0 to 9 and adding these, or are you trying to read a very large number into a 256-bit value (or something big like that)? If you're merely adding digits, a "char" variable type is all you need for reading each digit and an "int" or "long" variable type for the totaled output. If it was 9,999,999 digits being added, you'd reach 89,999,991 at the maximum, well below the near 4.3 billion maximum for an int (unsigned that is, half that if signed). If it's large numbers, that's outside my realm of knowledge.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I agree with the last post. Just keep reading the digits from the file as chars, adjust each one to an int, and add it to a variable named total or sum or something else descriptive.

    Obviously all of this should be done in a loop.

  9. #9
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    thank you for your help, ill have a go at it.

  10. #10
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    I tried the following code, i dont think it works, and it just stalls my program and doesnt continue to the next line of code. So ie. it doesnt print out "test"...Any suggestions?

    Code:
    if (!(inFile = fopen("tests/10001.tst","r")))
    		fprintf(stderr, "tests/10001.tst cannot be opened!\n");
    	
    	else {
    
    		current=fgetc(inFile);
    
    		while((current != '\n') && (current != EOF)) {
    		
    			current=fgetc(inFile);
    			currentNum = (int)current;
    			printf ("%d", currentNum);
    		}
    		
    	}
    
    
    	printf ("test");
    Last edited by waxydock; 04-13-2007 at 10:43 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to cast current, because it's already an int, or it should be, since that's what fgetc returns. Your loop also ignores (discards) the first character read without doing anything with it.
    Code:
    while( (c = fgetc( fp )) != EOF && c != '\n' )
    {
        printf( "%d", c );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    hmm.. my program is still freezing.. its not printing test

    Code:
    	if (!(inFile = fopen("tests/10001.tst","r")))
    		fprintf(stderr, "tests/10001.tst cannot be opened!\n");
    	
    	else {
    
    		current=fgetc(inFile);
    
    		while( (current = fgetc( inFile )) != EOF && current != '\n' ) {
    	
        		printf( "%d", current );
    		}
    		
    	}
    
    	printf ("test");

  13. #13
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    All I can think of doing is using fread to read a single byte (if it reads the digit 4, it'd read as the number 52 (after the ASCII position). Just subtract 48 from this. This is probably a poor method, but it works. In a loop, fread a single byte to a temporary variable, subtract 48 to convert to the actual numerical value, then add this value to your total (likely typecasting if needed). Keep this repeating until the entire file is read as needed. I'm pretty sure there's a better way of doing it, but this is the only way I know of (I'm still fairly new to C).

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try posting the whole thing, or making a small example and test just the loop that way. Why do you still have that first call to fgetc there? Does it actually print the numbers in the file? You do know that if it reads a '1', it's not going to print '1' when you print it the way you currently are, right? You also might want to considering flushing your output stream, since you never do that in your snippet. (IE: You have no newline, nor a call to fflush.)


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

  15. #15
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    Ive attached a small example.. its not outputting the same as whats in the 10001 file. And im not sure what you mean by "You do know that if it reads a '1', it's not going to print '1' when you print it the way you currently are,"

    Why wont it? And how do i make it do that.


    What im trying to do is get each digit, and print it. Later on i will be adding the digits up to get the total, so i need to change the char to an int. But when i print it, it comes out weird, ie. not the same as the input file.
    Check attachment for files.
    Last edited by waxydock; 04-14-2007 at 12:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Reading integers from a File into an Array
    By bobby19 in forum C Programming
    Replies: 13
    Last Post: 10-09-2006, 01:36 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM