Thread: Read numbers from a text file

  1. #1
    Registered User
    Join Date
    Aug 2011

    Unhappy Read numbers from a text file

    Hello, I'm a big fan of this website and it has helped me a lot in the past years, but unfortunately this time I can't find an answer to my problem here. I hope someone can help me out!

    I'm trying to read numbers (integers) from a text file consisting of random text and numbers. The numbers in the text are marked by "quotes", but I just can't figure out how I have to extract them. It would also be sufficient if I knew how to compare these numbers with an array (integers) I've already created (in general I just need to know which numbers from the text can also be find in the array).

    I've tried extracting the numbers, but I can only get characters (f.e. if I extract the number '1', I get the ASCII value '49'). I've also tried searching the text (loading text in a buffer) and comparing each character with the values from the array, but nothing...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    If the numbers are surrounded by quotes, finding them should be easy. Then you can collect them into a string and use strtol() to convert the number to an integer.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    Possible options(depending on file format since I am not sure):

    1. fscanf().
    2. Using an fgets/sscanf pair.
    3. Working with the solution you already have in place, you can convert a string to an integer with atoi
    4. If you just have a char, say '1' then if you do this: number = charNum - '0'. This will give you the integer equivalent of your character number.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Join Date
    Aug 2010
    Ontario Canada
    If you have a text file like this....
    fred "1003" is a very crazy "10032" caveman
    ... and the quotes rule always holds you should be able to use fscanf("\"%d\"",&myint); to extract the numbers.

    Look up fscanf() in your C library documentation (which should be supplied with your compiler).

  5. #5
    Registered User
    Join Date
    Aug 2011
    Thanks for quick reply!

    I'll look into the fscanf solution since the rule does indeed always hold.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Was indeed already very closed. Found the solution about one hour ago. Thanks for support! Wouldn't have found it without the tips!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 03:26 PM
  2. read text file, convert character numbers to int?
    By hyaku_ in forum C Programming
    Replies: 6
    Last Post: 11-06-2006, 05:04 PM
  3. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 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