# Reading in from a file

This is a discussion on Reading in from a file within the C Programming forums, part of the General Programming Boards category; Say my input is like this: (( 12345 * 10) - (895 / 5)) Currently, I am using fgets to ...

1. ## Reading in from a file

Say my input is like this:

(( 12345 * 10) - (895 / 5))

Currently, I am using fgets to read in equations one line at a time. One of the equations is like the one above.

Then entire line is read into a character array, then passed to another function. Only problem is, the number 12345 is seen as 1, 2, 3, 4, 5 (ie 5 separate numbers instead of 1).

How can I get it to see that as one big number??? I've tried using fscanf, but dont you have to know what the line will be ahead of time?

Any help would be great! (To yall who seen my last post, sorry about the snappage)...

2. Originally Posted by jcramer
Only problem is, the number 12345 is seen as 1, 2, 3, 4, 5 (ie 5 separate numbers instead of 1).

How can I get it to see that as one big number???
You may want to consider using string to integer library functions (e.g. atoi() or strtol())

or do it one number at a time e.g.
take the 1(i.e. take the '1' and turn it into an integer)
add to your total (total is now 1)
take the 2
times your total by 10 (total is now 10)
add the 2 to your total (total is now 12)
take the 3
times your total by 10 (total is now 120)
add the 3 to your total (total is now 123)
etc

3. >How can I get it to see that as one big number???
fgets is a good solution. Once you have the string in memory you can use parsing techniques and strtol (which saves the end of the conversion) to get your numbers.

Popular pages Recent additions