Getting numbers from a text file with C

Master-Nedero's Avatar
Newbie Member
This is a program that I have to make please somebody help me

The purpose of the task is to implement a program that reads the contents of a text
file (in.txt) and if it qualifies for an arithmetic expression set out below,
perform the specified arithmetic operation and record the results in a binary file (out.dat).
Arithmetic expression must meet the following conditions:
• is enrolled in a row;
• consists of two integer operands separated by the sign of arithmetic operation;
• arithmetic operations are permitted: "+" and "-";
• there is allowed an unlimited number of whitespace (space - '' and
char - '\ t') before and after the sign for simple operation;
• there is allowed an unlimited number of whitespace at the beginning and end of
arithmetic expression.
in.txt: "158 + 7"
out.txt: Contains binary written the number 165

Any Ideas guys ?
jimblumberg's Avatar
Ambitious contributor
How about starting with:

int main()
In other words please show a good faith effort of doing your assignment. If we supply you with source code at this point, what will you learn?


Last edited by jimblumberg; 19Oct2010 at 22:40..
shabbir like this
kumardashish's Avatar, Join Date: Nov 2008
Go4Expert Member
first when we store any thing in file (char,string or int) compiler first convert it in char after that store it in file. so u can't directly fetch the data from file and simply do some arithmetic operation.first you have to convert it into integer then perform arithmetic operation.
i have a clue. go through ASCII table there give the ASCII value of 0,1,2,3,4,5,6....
try to connect them i mean u have to create a equation to convert char to int.
OK i make it simple..
suppose you get a value 6 from file. be attention it is not int type it is char type and your program take ASCII value of 6 means it take 54 and you know that ASCII value of 0 is 48
just subtract these two ASCII and store it in int type variable.
int a;
char ch;
FILE *fp;
fp = fopen("filename","r");
a=ch - '0' ;
jimblumberg's Avatar
Ambitious contributor
Why not just use fscanf to retrieve the number, sign, number. Or use fgets to read in a line of text into a cstring then use sscanf to retrieve the number, sign, number sequence from the cstring?

Master-Nedero's Avatar
Newbie Member
Thanks for all the help
I made the program in a little difrent way but I am really thankful for your help