Did you hear something waxydock?
I thought being told the same thing at least twice by two different people would make it stick, obviously not. Perhaps a switch to BASIC is in order ? :P
Printable View
Did you hear something waxydock?
I thought being told the same thing at least twice by two different people would make it stick, obviously not. Perhaps a switch to BASIC is in order ? :P
"You can lead a horse to water, but you can't make him think"
No, but you CAN make him drink the water.
No pun intended...
ok, you guys are telling me to do something like this? but now how do i get each digit in the file? because current isnt the digit, it is the char turned into an int which give weird numbers..
Code:#include <stdlib.h>
#include <stdio.h>
int main (int argc , char** argv) {
FILE *inFile;
int current;
if (!(inFile = fopen("10001.tst","r")))
fprintf(stderr, "10001.tst cannot be opened!\n");
else {
while( (current = fgetc( inFile )) != EOF && current != '\n' ) {
printf( "%d", current);
}
}
}
> printf( "%d", current);
Use %c to print a character (I'm guessing you're getting lots of numbers from 48 to 56)?
To print the numeric value of a decimal digit, try
printf( "%d", current - '0' );
in fact my file just stores one big number... im just trying to get each digit and sum them. How can i do this, because the int isnt the value i want. I tried below but this is the wrong sum right?
Code:#include <stdlib.h>
#include <stdio.h>
int main (int argc , char** argv) {
FILE *inFile;
int current;
int sum = 0;
if (!(inFile = fopen("10001.tst","r")))
fprintf(stderr, "10001.tst cannot be opened!\n");
else {
while( (current = fgetc( inFile )) != EOF && current != '\n' ) {
sum = sum + current;
printf( "%c", current);
}
}
}
Change to something like:Code:sum = sum + current;
I believe you were told to do this already.Code:sum = sum + current - '0';
Anyway the reason is that if you subtract '0' (which is ASCII 48) from a char digit, you'll get its actual numerical value (ie. '0' will be 0, '1' will be 1, '2' will be 2... etc.).
http://img412.imageshack.us/img412/9...mputersjd3.gif
That's all I've got to say about this thread.
Quzah.