Thread: How to extract int from data?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    How to extract int from data?

    Hi,

    I need to extract integers from the following input file and put them into different input variables. Anyone can share some tips how to do it?

    The input file is something like that:
    Rec1-0($51) Rec2-0($25) 821
    Rec1-1($45) Rec2-1($85) 121
    Rec1-2($4) Rec2-2($5) 1421

    Meaning to say, for the 1st line, I need to input 0, 51, 0, 25 and 821 into separate int variables. For 2nd line, need to input 1, 45, 1, 85 and 121 into another set of int variables, and so on...

    Would appreciate you kind help...Thanks in advance!

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Can you read in information from files?

    If you can, will there only ever be two Rec's? I mean Rec1- Rec2-? If there is, you could read in 5 dud characters, then read in an int, then two dud characters, then an int, then a dud character, for Rec1-0($51) (Green are duds, red should be read as ints).

    Alternativel, what I'd do is read in as a string, find the first occurance of a '$', and the next ')', substring that to a temp, and stringstream to an int. Repeat for the Rec2, and then stringstream the last number. There have been some posts recently about string streams. Look in one with a title about atoi(), or itoa(), I can't remember which it is.
    Last edited by twomers; 09-01-2006 at 09:30 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    Yes, can read in from file.
    Say I input each line into a string, is there any way to extract the integers and put them into separate int?

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you can split the string up using a istringstream then when it comes time for the in, store it in an int.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> Say I input each line into a string, is there any way to extract the integers and put them into separate int?

    Look into the .find(), functions for strings, the .sub_str() function, and stringstreams.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well, if you know the exact format of the text file and you know that will never change, then it's easy. Ignore what you know isn't a number, when you get to the point that the number is next, read it into the appropriate variable.

    If you don't know the exact format, you'll have read the entire thing into a string, search the string for characters that are numbers or symbols in numbers ('0' to '9', '-','.'), mark that point in the string with an iterator, continue until it ends, mark that point and use a substring function to exact that. Then use a stringstream or something along those lines to convert it.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM