I am trying to read a line of text from file(example below). Every line of text has: title of the book(starts with capital letter, can be multiple words), number of the book, country origin of the book(starts with capital letter) and catalog of the book(starts with capital letter, can be multiple words). Every line ends with a "\n" and the next line starts. I want to read the line and seperate title, number, country and name. How to do it properly?
I tried to read char by char, and when char I am currently reading is either a number or a Capital letter and previous char was a "space", flag should go up to signalize I should move from for example title to number, number to country etc. When i read a "\n" all flags should go down and the process should start again
void openfile(char* filedestination)
{
FILE *file = fopen(filedestination, "r+");
int help[100];
int c;
int name[100];
char title[50];
int country[50];
int number[6];
int i = 0, flagt = 0 , flagn = 0 , flagc = 0, flagname = 0;
if (file == NULL)
{
printf("Opening error");
exit(-1);
}
while((c = fgetc(file))!=EOF)
{
help[i] = c;
if (flagt == 0) {
for (int j = 0; j < i; j++) {
name[j] = help[j];
if (c > 47 && c < 91 && help[i - 1] == 32) {
flagt = 1;
j = 100;
}
}
}
if (flagc == 0)
{
int j = 0;
while (flagc == 0) {
number[j] = help[i];
if (c > 47 && c < 91 && help[i - 1] == 32) {
flagc = 1;
}
j++;
i++;
}
}
i++;
printf("\n");
for (int j = 0; j < i; j++) {
printf("%c", name[j]);
}
for (int j = 0; j < i; j++) {
printf("%c", country[j]);
}
fclose(file);
}
}
Example line:
Mountains 10002 France Wonders of nature
Photonic crystals 10003 Germany Science
So I should get title = Mountains, number = 10002, country = France, name = Wonders of nature. Then I will use it in another function, so I can overwrite name after I read it from the first line, because I won't need it.