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?