# Reading large number from file

This is a discussion on Reading large number from file within the C Programming forums, part of the General Programming Boards category; hi, Im trying to read a large number from a file, into a variable and then print it. This is ...

1. ## 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. just figured it out... i needed to use a float.

and change the &#37;d to %lf

BTW should i be using a float or another data type????

3. 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?

4. 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. 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..

6. 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.

7. 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. 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. thank you for your help, ill have a go at it.

10. 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 ("&#37;d", currentNum);
}

}

printf ("test");```

11. 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( "&#37;d", c );
}```
Quzah.

12. 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( "&#37;d", current );
}

}

printf ("test");```

13. 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. 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.

15. 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.

Page 1 of 3 123 Last