Capturing float value from file

This is a discussion on Capturing float value from file within the C Programming forums, part of the General Programming Boards category; Within the customers text file it says : Account Balance = 4000.00 Code: int main() { FILE * fp; char ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    31

    Capturing float value from file

    Within the customers text file it says :
    Account Balance = 4000.00
    Code:
    int main() {
        FILE * fp;
        char one[80];
        double theone;
        
        fp = fopen("customers.txt", "r");
        fgets(one, 35, fp);
        sscanf(one, "Account Balance = %f", &theone);
        printf("%f", theone);
        fclose(fp);
        return 0;
    }
    printf returns garbage numbers followed by lots of zeroes
    Even when I use fscanf, there are also problems...

    What's wrong with the code?

    Pier.
    [/code]

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Just solved the problem using %lf instead of %f....
    what the hell is the difference between using %lf and %f when reading from a file?!?!
    argh... I spent two hours on this

    Pier.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Sorry another similar question related to this

    How do I read in a string
    "DOB = 10-10-1970"
    from a file and assign it to char arrays dob1, dob2 and dob3...?

    this is my code
    Code:
    fgets(buffer, 40, fp);
    sscanf(buffer, "DOB = %2c-%2c-%4c\n", custList[i].dob1, custList[i].dob2, custList[i].dob3);
    It doesn't seem to work when I print them out
    it gives
    101019790-3233
    -10-19790-3233
    0-19790-3233

    when I puts the three variables...

    This is the text file :
    Code:
    Account Id = 123
    Name = Matt Damon
    Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
    DOB = 10-10-1970 
    Phone Number = 790-3233
    Account Balance = 405600.00
    Pier.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    upz

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>It doesn't seem to work when I print them out
    It works fine for me when I nul terminate the strings, all you're doing is assigning characters, you don't treat the arrays as strings at all.

    >>upz
    People here don't like thread bumping, it's rude and could get your thread deleted from what I can tell. ;-)
    *Cela*

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    What do you mean by null terminating the strings ? What's the syntax like...
    Sorry I posted the question again, but I don't seem to be able to delete the thread...

    Pier.

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>What do you mean by null terminating the strings ? What's the syntax like...
    Just add a '\0' character after the last valid character in the array. Be sure that there's enough room for it though. :-)
    Code:
    custList[i].dob1[2] = '\0';
    *Cela*

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. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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