Your going about this the wrong way. Try using fgets() instead of fgetc().
#include <stdio.h>
#include <stdlib.h>
#define STR_SIZE 10
int main(void)
{
FILE *fp;
char ch[STR_SIZE];
int aInt;
int bInt;
fp = fopen("erogol.k","r");
fgets(ch, STR_SIZE, fp);
aInt = atoi(ch);
fgets(ch, STR_SIZE, fp);
bInt = atoi(ch);
printf("%d \n",bInt+aInt);
return EXIT_SUCCESS;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Both numbers are on the same line, so you only need one fgets call, I think. Then you can get the digits from the string or use sscanf (but that's probably overkill).
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
aInt = atoi(ch[0]);
bInt = atoi(ch[1]);
Except that there's a space at position 1.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
If the numbers are on the same line..
#include <stdio.h>
#include <stdlib.h>
#define STR_SIZE 10
int main(void)
{
FILE *fp;
char ch[STR_SIZE];
int aInt;
int bInt;
fp = fopen("erogol.k","r");
fscanf(fp, "%s", ch);
aInt = atoi(ch);
fscanf(fp, "%s", ch);
bInt = atoi(ch);
printf("%d \n",bInt+aInt);
return EXIT_SUCCESS;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
C'mon, guys, think!
Any guesses what fscanf(fp, <strong>"%d"</strong>, &bInt); might do?
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
We don't have to think...your here.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387