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. #1
    Registered User jcramer's Avatar
    Join Date
    Oct 2003
    Posts
    23

    Question 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)...
    Viva la Tutorial
    What is the meaning of life? 42 of course!

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Quote 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
    Last edited by major_blagger; 03-29-2004 at 09:07 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21