Quick simple question

This is a discussion on Quick simple question within the C++ Programming forums, part of the General Programming Boards category; Ok, I feel like an idiot, but... How can i read in from a file with a string of numbers ...

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    Talking Quick simple question

    Ok, I feel like an idiot, but...

    How can i read in from a file with a string of numbers and characters, and put each into it's corresponding type?

    For example, let's say I have the following input file:
    Code:
    21 43 + 21 21 - - 92 *
    and I want to read in each number, the WHOLE number (i.e. 21, not 2, as the 1st num) into an int, but read in each non-space char in as a char. seems I can't use isdigit() as I won't get a whole number, just something below 9.
    I am Error. When all else fails, use fire.

    My Current Screenshot

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    getline() the whole line from the file. Make a function that gets the next token in your string (using space as your deliminater (look up strchr)). Make another function that checks to see if the token is an integer or not. If it is, put it into an integer variable, else put it in to a char or string value. repeat until all tokens have been read.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    37
    If you can get 1 digit at a time try this.


    Code:
    number = 0;
    input = read from file
    while (is_digit(input))
    {
       number = number * 10 + input;
       input = read from file;
    }

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    37
    Ohh remember '1' is not the same as 1. You may need to convert from ASCII character to digit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 12:59 PM
  2. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 07:59 AM
  3. Quick question, should be simple to answer.
    By kabuatama in forum C Programming
    Replies: 4
    Last Post: 01-21-2006, 02:42 PM
  4. Quick question if i may
    By badboy16z in forum C++ Programming
    Replies: 1
    Last Post: 10-20-2003, 10:41 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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