problem reading mixed data...

This is a discussion on problem reading mixed data... within the C Programming forums, part of the General Programming Boards category; I can't figure out how to read mixed integer and float data from a text file. I would like to ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    6

    problem reading mixed data...

    I can't figure out how to read mixed integer and float data from a text file. I would like to use sscanf if possible but can't figure out how to read in the numerical data.

    An example of the file looks like this:

    Billy 6.0 8.0
    Bob 20 40
    Maynard 0.69 0.98
    Bubba 0 10
    Bunch 1 7


    I would like to be able to write the numerical data out to a file in floating point format and ignore the names.

    Thanks in advance!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Read it all in as float values. You can use sscanf to safely read and convert "60" into a float just fine.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Do you strictly need to read in ints as ints?
    Code:
    scanf("%s%f%f", name, &value1, &value2);
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    fgets(temp,strlen(temp),stdin);
    
        sscanf(temp,"%s %f %f",name,&num1,&num2);

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. reading data from disk into array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 03:19 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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