Question

I have a configuration file where some fields are mentioned in the following way:

;
Student Name; Enroll. No.; Std; Age

where first line tell about delimiter and second line shows fields delimited by the above delimiter. Fields are dynamic as it is a configuration file.

Delimiter could be ,/;/: Fields are not in fix number. Field names will change with scenario. Input file data to be formated according to field

I have to understand this configuration file using C code. Please guide me in this

Was it helpful?

Solution

I'll describe an approach algorithmically, with some C hints. You can make an attempt to implement this in detail in C:

  1. Read the first line of the configuration file into a string buffer and store the first character into a delimiter variable (Note: this assumes of course that the first character of this line is what you want).

  2. Read the second line of the configuration file into a string buffer and, using strtok and the delimiter saved in step 1 as the string delimiter, read each of the column names from that line, copying them into an array of column names (Note: you can have an array of char * with fixed maximum number of elements, and allocate each string memory dynamically as you go, copying the strings from pointers given by strtok). In this step, record how many columns you recorded, num_columns.

  3. Open and read the data file line by line. You can use strtok here and operate on each string item using whatever method needed. Since your configuration file doesn't indicate data types, you'll need to assume that they're all strings, unless you want to make assumptions based upon certain column names.

That's a basic approach. You can fill in the blanks on error checking and general housekeeping (closing files where appropriate, etc).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top