Thread: Reading a serie of numbers from file

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question Reading a serie of numbers from file

    Hi!

    I need to read a series of numbers from a file. All in the same line, separated by commas.

    I'm trying to figure which of the 4 functions should I use ( fscanf(), fgets(), fgetc(), fread() ).

    Do I have to check char by char or is there a magic function/ combination of functions?

    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fread will read things in a binary fashion. Since it's human-readable with commas, you don't want that.

    fgetc will read character by character, which you can make that work for you but it's probably not the obvious solution.

    fgets reads in the whole line, and then you can break apart the line however you need to.

    fscanf reads "formatted input", so if your input lines follow a particular format (always) then that works.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    And to break a line a part I should use regular expressions? (I just know there are gonna be lots of numbers. I don't know how many or how many digits they have

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you writing code that can handle regular expressions, kotoko? C doesn't natively know regex and there should never have been anything that lead you to that misconception.

    Show us what you have even tried thus far.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    How can you distinguish one number from another? What is it that keeps the string of numbers from being one, long number? (HINT: Separator)

    Look up strtok, that's a function.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    strtol() is actually phenominal at this too, btw. In fact, I would argue that it is even better if you are given a string that simply only contains numbers if you use the second parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

Tags for this Thread