954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading two numbers From a file and add them! Help me!

I am newbie in C programming and I have a easy assigment but I coul not do that. I need to read two number from a file, add them, print them that is all. Here is the my code.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	FILE *fp;
	fp = fopen("erogol.k","r");
	char a =fgetc(fp);	
	int aInt = atoi(&a);
	char b =fgetc(fp);
	int bInt = atoi(&b);
	printf("%d \n",bInt+aInt);
	return EXIT_SUCCESS;
}

My File has "9 4" and I need to take each number 9 and 4 them sum them but the program does not work.

Can you help me?

erogol
Newbie Poster
24 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

both number are in the same string, this is what you do

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[0]);
        bInt = atoi(ch[1]);
	printf("%d \n",bInt+aInt);

	return EXIT_SUCCESS;
}
dnambembe
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
aInt = atoi(ch[0]);
bInt = atoi(ch[1]);

Except that there's a space at position 1.

jonsca
Quantitative Phrenologist
Team Colleague
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
Moderator
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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: