How to read numbers out of a file in c

This is a discussion on How to read numbers out of a file in c within the C Programming forums, part of the General Programming Boards category; Hi, I have a file that is a mixture of integers and words all strung together with spaces. It is ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    How to read numbers out of a file in c

    Hi,
    I have a file that is a mixture of integers and words all strung together with spaces. It is a score for a bowling game it looks like "8 split 1 9 1 spare 7 2 10 strike 8 2 spare 6 split 2 7 3 spare 10 strike 10 strike 10 turkey 8 1" I am trying to figure out how to read the just the numbers out of the file and then rewrite them into another file.

    I started with opening the original file and using sscanf to store the values in an array. Will this work with the way the file is formatted right now?

    Thanks

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You could read it "word by word" (whether that word is something like "split" or something like "8") using scanf with the %s format and then test the first character to see if it's between '0' and '9' inclusive. If it is, convert it to a number with atoi(), otherwise ignore it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,049
    Better use strtok(), since the position of numbers and strings in the input file is arbitrary.
    After reading each token from the file, verify with isdigit() if it is a number or not.
    If the token is a number, then it gets written to another file or thrown away.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't think strtok is necessary. You can read the data straight from the file with fscanf using the %s format since it skips spaces automatically. Something like:
    Code:
        char token[100];
        // ...
        while (fscanf(f, "%s", token) == 1) {
            if (isdigit(token[0])) {
                int n = atoi(token);
                printf("Number: %d\n", n);
            }else
                printf("Word: %s\n", token);
        }
    Or if you had the data in a string, you could use sscanf the same way.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read in numbers from file
    By ElNino in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 01:47 PM
  2. Read numbers from a text file
    By fred_maes in forum C Programming
    Replies: 5
    Last Post: 08-13-2011, 07:43 PM
  3. Read numbers from file to std::vector
    By Petike in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2010, 04:33 AM
  4. read numbers from a file
    By haroonie in forum C Programming
    Replies: 5
    Last Post: 03-27-2005, 11:30 PM
  5. Read words and numbers from a file
    By atari400 in forum C Programming
    Replies: 5
    Last Post: 11-04-2004, 04:55 PM

Tags for this Thread


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