I don't think there is an all-encompassing tutorial. File input is varied and new formats are being invented all the time. You are best served learning the ins-and-outs of functions in string.h and complex scanners like sscanf() or fscanf(), because sometimes they are sufficient. The complexity of the format and your willingness to tolerate format errors, will dictate how much effort you should really put forth.
For XML or other structured document formats, like HTML or JSON, a library is essential.
In addition to learning the input functions, you could also learn about finite state machines. (See here or search the forum for the term.) Many great tasks that involved parsing input can be solved with one, and it is often a very clean, small amount of code.
For this specific task, a quick hack might be good enough:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char fileName[] = "sample.txt";
const char content[] =
"<>Is</> <>there</> <>any</> <>tutorial</> <>on</> <>how</> <>can</> <>I</> <>learn</> <>more</> "
"<>on</> <>how</> <>to</> <>creat</> <>a</> <>file</> <>parsing</> <>program?</>\n";
int main()
{
FILE *file = NULL;
char buffer[2048] = "";
char words[60][40] = { '\0' };
char *token = NULL;
int k = 0, i = 0;
const int N = sizeof words / sizeof words[0];
/* Create a sample to show a simple parse. */
file = fopen(fileName, "w+");
if (file == NULL)
{
perror(fileName);
return EXIT_FAILURE;
}
fputs(content, file);
rewind(file); /* prepare sample for reading. */
fgets(buffer, sizeof buffer, file);
/* Start using strtok.
* Note that strtok has various problems, and that there are alternatives.
* http://web.archive.org/web/20080612190205/http://www.daniweb.com/code/snippet318.html
*/
for (token = strtok(buffer, "</>"); token != NULL && k < N; token = strtok(NULL, "</>"))
{
strncpy(words[k++], token, sizeof words[0]);
}
for (i = 0; i < k; i++)
{
printf("%s ", words[i]);
}
putchar('\n');
fclose(file);
return EXIT_SUCCESS;
}
/* results:
Is there any tutorial on how can I learn more on how to creat a file parsing program?
*/
Keep in mind that this is very quick and dirty. You can, and probably should code more defensively in real applications.