Thread: reading digits from a text file

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    reading digits from a text file

    Hello,

    I'm learning c myself (not c++, I'm trying not to use objects)

    I have have a bunch of text files (created by a perl program)

    They contain a bunch of letters and random numbers from 0 to 9
    I'm only interested in the numbers and nothing else

    here's and example of the contents (i just made it up)

    5 7 2 4 6
    0 2 E D
    0 A D E
    6 D D
    7 D K
    5 F

    How do I go about loading just the numbers into an array?
    so I want an array that that has

    5 7 2 4 6 0 2 0 6 7 5

    Basically the above is an example, there can be more per line and more lines, but I just need the numbers.

    Any examples/help appricated

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    21
    Assuming that this is for a school assignment, I'll try to provide the proper kind of assistance.

    First of all, think of how large you should make your array when the program starts. Once it's made, it can't be changed unless you make a new one and copy contents over.

    To read the characters from the file, have a look at the fscanf manpage.

    There is a function that you will find very useful for this assignment in ctype.h.

    As far as how to easily navigate your array or file, have a look in your text book's index under "pointer arithmetic".

    Hope this helps,

    ~arker

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    Thanks for your input.

    lol not for a school assignment, bit to old for school lol. Just picked up a c programming book and giving it a shot

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read each character from the file, then use the isdigit() function in ctype.h to determine whether its a numeric char
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    Read each character from the file, using fgetc() then use the isdigit() function in ctype.h to determine whether its a numeric char
    ... or learn to compare the values by hand, testing if they are between '0' and '9'. I'd suggest for practice write your program both ways.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  3. Reading text file twice, can it be done once?
    By CaeZaR in forum C++ Programming
    Replies: 4
    Last Post: 02-04-2006, 04:18 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM