Brain Fart/Freeze - String to float question

This is a discussion on Brain Fart/Freeze - String to float question within the C Programming forums, part of the General Programming Boards category; Hola, I'm having a major brain fart here. I am reading in a file from (programName) <input.txt. And I am ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    111

    Brain Fart/Freeze - String to float question

    Hola,

    I'm having a major brain fart here.

    I am reading in a file from (programName) <input.txt.

    And I am using fgets() to grab the each line and I'm dealing with each line.

    There are some numbers on the line in the format of 01/01/2011,123.45,56.78,...,23.12

    I am trying to grab those numbers (ie 123.45) and convert them into a regular number, atoi() farts out when it hits the . and spits back 123.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
      parseFile();
    
      //more work
    
      return 0;
    }
    
    void parseFile()
    {
      while ( fgets(line, 256, stdin) != NULL )
      {
        //error checking/handling
        //dealing with other lines prior to the lines with the data
    
        grabData(line);
      }
    }
    
    void grabData(char *str)
    {
      char *delims[] = ",";
      char *result = NULL;
      
      result = strtok( str, delims );
    
      //String format going in is in the form of
      //date,price1,price2,...,priceN
      //sample lines
      // 1/31/2012,29.53,26.42,456.48
      // 1/30/2012,29.61,26.74,453.01
    
      while ( result != NULL )
      {
        result = strtok(NULL, delims);//grabs the first price
     
        //how to convert into a number??
        //tried scanf() but either I'm not getting it or still confused :S
        //Store the number into 'double stockTable[n][m]'
      }
    }
    An suggestions, or a better way in doing it?

    Thank You in advance!
    Last edited by Strahd; 02-09-2012 at 10:49 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Strahd View Post
    Hola,

    I'm having a major brain fart here.

    I am reading in a file from (programName) <input.txt.

    And I am using fgets() to grab the each line and I'm dealing with each line.

    There are some numbers on the line in the format of 01/01/2011,123.45,56.78,...,23.12

    I am trying to grab those numbers (ie 123.45) and convert them into a regular number, atoi() farts out when it hits the . and spits back 123...
    That's expected with atoi() for converting a string to integer.
    To convert a string to double lookup the manpage of strtod().

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Quote Originally Posted by itCbitC View Post
    That's expected with atoi() for converting a string to integer.
    To convert a string to double lookup the manpage of strtod().
    That did the trick, thank you kindly!

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    or there is atof()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional IF Brain Fart
    By paulogrady in forum C Programming
    Replies: 4
    Last Post: 04-12-2009, 11:07 AM
  2. brain fart
    By Gardul in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2005, 08:49 AM
  3. Brain Fart Problem
    By golfinguy4 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2002, 10:56 PM
  4. Brain fart
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-30-2001, 12:40 PM

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