Thread: How does strtod manage to parse?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Question How does strtod manage to parse?

    I'm trying to understand how a well functioning piece of code actually does its work. In question is how the functions "strtod" and "strtol" manage to sequentially parse an input file or string of characters. I do understand that the functions in question do a conversion from characters to numbers; however, what I find confusing is how they manage to increment from one set of characters to the next in the input file. The file that is opened reads as follows, in part:

    1
    22050.0
    4.0
    /* and so on */


    The following code opens that file and begins sequentially pulling and converting blocks of characters into the appropriate global variables. What I don't undersatnd is how each sucessive "strtod" ot "strtol" knows to skip the previously read characters and pull the next sucessive block. If these functions do this on their own somehow, I haven't found any documentation as to this capability in the function manuals.

    Okay, here's the code in question (and it DOES work!):


    Code:
    int parseInputFile(const char *inputFile)
    {
        int i;
        FILE *fopen(), *fp;
        char line[128];
    
    
        /*  OPEN THE INPUT FILE  */
        if ((fp = fopen(inputFile, "r")) == NULL) {
    	fprintf(stderr, "Can't open input file \"%s\".\n", inputFile);
    	return (ERROR);
        }
    
    
        /*  GET Global_Variable_0 */
        if (fgets(line, 128, fp) == NULL) {
    	fprintf(stderr, "Can't read Global_Variable_0.\n");
    	return(ERROR);
        }
        else
    	Global_Variable_0  = strtol(line, NULL, 10);
    
        /*  GET Global_Variable_1 */
        if (fgets(line, 128, fp) == NULL) {
    	fprintf(stderr, "Can't read Global_Variable_1.\n");
    	return(ERROR);
        }
        else
    	Global_Variable_1 = strtod(line, NULL);
    
        /*  GET Global_Variable_2 */
        if (fgets(line, 128, fp) == NULL) {
    	fprintf(stderr, "Can't read Global_Variable_2.\n");
    	return(ERROR);
        }
        else
    	Global_Variable_2 = strtod(line, NULL);
    
    /*  THE PROGRAM CONTINUES WITH A SERIES OF STATEMENTS
     IDENTICAL THE ABOVE THREE CHUNKS OF CODE UNTIL ALL THE
     INPUT FILE CHARACTERS (GLOBAL VARIABLE VALUES) HAVE BEEN PARSED */
    
                                   
    
        /*  CLOSE THE INPUT FILE  */
        fclose(fp);
    
        /*  RETURN SUCCESS  */
        return (SUCCESS);
    }
    So, when it's done, the value "1" is assigned to Global_Variable_0, " 22050.0" to Global_Variable_1, "4.0" to Global_Variable_2, and so on. . .

    Any ideas?

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    Attached is the GNU version of strtol.c which is called by all of the strto.... functions. glibc.2.2.3

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The call to fgets reads one line at a time. The newline is not stripped away, but such a character that cannot be part of the target type will break out the call.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       const char text[] = "123x5";
       long john_silver = strtol(text, NULL, 10);
       printf("john_silver = %ld\n", john_silver);
       return 0;
    }
    
    /* my output
    john_silver = 123
    */
    The parameters that here are NULL can be a pointer that will be set the the character that causes the break.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    They aren't skipping anything, the array 'line' is being overwritten on each successive fgets call...

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Smile

    Okay, I think I understand it now: my problem was not with how strtod works, but with fgets. I didn't realize it reads one line at a time. The short man page I have on fgets doesn't explain that detail. I'll try to look into it better on the web.

    Thanks again. You folks are GREAT!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM