Say I have a file with the format:
key1/value1
key2/value2
key3/value3
....
Say I have an array to hold these values:
char *data[10][10]
How would I read this file and get key1, key2, and key3 into data[0][0], data[1][0], and data[2][0]. Then put value1, value2, and value3 into data[0][1], data[2][1], and data[3][1]. So actually I want to get the strings of key1-key3 individually, then test for the ‘/’ character then get the strings of value1-3. By the way, when I’m inputting these into the file, I am including the ‘\n’ character so you could test for that to test for the newline.
The best method is to read the data per line into a buffer, then parse the buffer. This can be expanded to reading in large blocks of data.
Use
fgetsfor reading the data into a buffer.Use
strchrto find the separator character.Example:
Note: The above code has not been compiled nor tested, but is for illustrative purposes only.