Thread: Check if string is a floating point number or not !!! Please Help

  1. #1
    Registered User
    Join Date
    Aug 2012

    Check if string is a floating point number or not !!! Please Help

    Hello ,
    I'm a newbie in C and have been programming this for a while now .
    Task: Read a csv file and look for floating point numbers . If you see a floating point number, then change the floating point number to six significant figures after the decimal point.

    I have the code where the program reads the file, and seperates the strings from commas, I use the strtok function to tokenize between commas. Because my file is not in a proper array format or anything, its just goes on and its huge there are just some random commas in between.

    My problem is , I'm not able to find a proper solution to check if the seperated token(i.e., strings) is a floating point number or not.

    The file has words, binary numbers, integers, signed/unsigned floating point numbers.

    Below is the code that I've got through so far and below that is the example of how the file looks like.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <process.h>
    #include <conio.h>
    char *token;
     // Program that copies file frst_100l to a new file new_data.
       int main()
       char ch, data_in[1024], data_out[1024]; // initialize character for files
       char line[102400];
       char temp[30];
       char size;
       int i = 0;
        FILE *fp, *tp;  // file pointers
        printf("Enter name of file to copy\n"); // enter the name of the file for data
       gets(data_in); // get data to character pointer
       fp = fopen(data_in, "r"); // open file to read
       // if file has error then exit the debug
       if( fp == NULL )
          printf("Press any key to exit...\n");
      while(fgets(line,102400,fp) != NULL)
                while( token != NULL )
                  /* While there are tokens in "string" */
                  printf( " %s\n", token );
                  /* Get next token: */
                  token = strtok( NULL, "," );
                    if((token == 'a') && (*token <= 'z'))
       //Enter the name of the file for the data to copy to
       //printf("Enter name of file data should be changed to \n");
       gets(data_out); // get data_out pointer
       tp = fopen(data_out, "w"); // open the file to write
       // close if error in creating or writing the file
       if( tp == NULL ) 
          printf("Press any key to exit...\n");
       // while character is for data_out file is not equal to end of file
       // copy the data to new file 
       while( ( ch = fgetc(fp) ) != EOF )
          fputc(ch, tp);
       // print file copied successfully and close the debug window
       printf("File copied successfully.\n");

    Example of file :

    1,,,0,,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,,,,,, ,,,,,,,,,,,,,,,,23,3,0,3,1.2470109462738,1.2248870 1343536,,,,0,,0,1,23659,12976,32090,1.247010946273 8,112.743698120117,23,8869.6796875,,,,,,,,,,,,,,,, ,,,,,,,,98.8096694946289,,98.8096694946289,,.82850 6410121918,.986691415309906,,,,,,,,.79969382286071 8,,,,,,,,2.03000092506409,,2.0292010307312,,1.0346 8096256256,1.22431802749634,,,,.380130290985107,20 ,3,,,,20,0,,,,,,,,,,,,,,,,28.2810001373291,,27.870 7695007324,,.410234898328781,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.7565901279449 46,.736221790313721,.579420387744904,.560720205307 007,,,,,,,,,,,,,,,,,,,,,,2.07184100151062,,2.07184 100151062

    Thank you and please let me know


  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Melbourne Australia
    You shouldn't free(token) unless you dynamically allocated it.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2006
    Personally, I would take it char by char, through the file. (Because the file has a lot of odd stuff that is not floats).

    Then when you find a '.', I'd back up the index number until it reached either a comma, a space, or a newline char.

    Now put every char into a small char array, until you reach the next comma, space, or newline char. That's your float.

    Now read the string you just made, with sscanf() and store the float as a float.

    Round it off to the number of digits you want, using the standard formula.

    And print out the rounded off number. you can also round off a number directly using printf() format codes, but i don't believe your assignment would allow it.

    I'll put up a little example of this, later on. It won't be a full program, however.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Quote Originally Posted by adoosa View Post
    If you see a floating point number, then change the floating point number to six significant figures after the decimal point.
    I advise against mixing up your mathematical terms there. Significant figures specificaly has nothing to do with the decimal point. Either state "x significant figures", or state "x decimal places".

    You also need to be more specific as to what you consider to be a floating pointer number. 123 will happily parse as a floating point number, as will 4E5.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2011
    if all tokens are delimited by commas, i would tokenize on commas first. then maybe remove whitespace (write a function 'removeWhitespace') then separately write functions that recognize what type of token it is.functions such as 'isInteger', 'isWord', 'isFloat' etc. you have to define the rules that the types match. if a token has letters in it, its a word. if a token has only numbers and a '.' in it somewhere, its a floating point. if it has only numbers but no '.', its an integer. you also mention binary numbers. if you mean numbers containing only 0's and 1's, then it is hard to tell the difference between a decimal integer and a binary number. then once you know what type it is, sscanf it and output it as required.

    and before trying to put this all in a loop, write a test program that tests your individual 'is' functions with fixed data to get them working, then worry about the loop. my point is that you should break the whole thing down into its individual pieces and get them working first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number printf/string formating
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2009, 04:41 PM
  2. Floating point - check for divide-by-zero
    By pankaj401 in forum C Programming
    Replies: 7
    Last Post: 03-20-2008, 02:38 AM
  3. convert a floating point number to string
    By heeroyung in forum C Programming
    Replies: 1
    Last Post: 10-02-2005, 01:03 AM
  4. Convert a Floating Point Number to a String
    By maththeorylvr in forum C Programming
    Replies: 1
    Last Post: 04-21-2005, 04:40 PM
  5. how do you check for a floating point vlaue in c??
    By xenodvs1 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 06:52 AM

Tags for this Thread