I have a function that reads a formatted file. It looks like this:
1;Name_of_the_author;The date when the quote was published;The author of the quote;The quote
2;Name_of_the_author_2;The date when the second quote was published;The author of the second quote;The second quote
So, the delimiter is ; . What I have to do is to check every sequence/token and to check if it’s correct. The problem however is that it doesn’t get all the tokens, just the first three, after the date it just breaks, it doesn’t move through… here’s the attached code function. Ignore the comments, it’s for a school project and the comments are in romanian.
int svnCheckDb()
{
FILE *file;
int k, p, i=2, m, j=0;
char mystring[1000000], *var, *var2, *string;
file = fopen("db.txt", "r"); //deschidem fisierul
if(file == NULL) {
return 0;
}
else {
//il putem accesa.
while(fgets(mystring, 1000000, file) ) {
if(j != 0)
{
//nu luam si prima linie cu descrierea repo-ului, prelucram doar citatele, j-ul numara randul pe care suntem
//separam cu strtok linia citita si verificam fiecare informatie in parte pentru a fi corecta
var = strtok(mystring, ";");
k=1;
/*
k numara string-urile citite din descrierea citatelor tocmai citita. Primul e numarul de ordine, al doilea e utilizatorul
care a adaugat citatul, al treilea reprezinta data adaugarii citatului, dupa care urmeaza citatul.
*/
while(var != NULL) {
printf("k is %d and var is %s \n", k, var);
switch(k)
{
case 1:
//numarul de ordine. Daca e 0, inseamna ca nu e numar, returnam false
i = atoi(var);
if(i == 0)
return 0;
break;
case 2:
//utilizatorul care a adaugat citatul. Daca e gol sau nu e format doar din caractere a-z A-Z, returnam false
for( m = 0; m < strlen(var); m++ )
if(!isalpha(var[m]))
return 0;
break;
case 3:
//data la care a fost adaugat citatul. Intrucat folosim formatul DD MM YY cu spatii intre ele, vom verifica daca e ok in fisier
string = var;
var2 = strtok(string, " ");
p=1; //folosim p sa vedem daca am ajuns la zi, luna sau an
while(var2 != NULL)
{
switch(p)
{
case 1:
//ziua
i = atoi(var2);
if(i == 0)
return 0;
else if(i > 31 || i < 1)
return 0;
break;
case 2:
//luna, care e formata din primele 3 caractere ale lunii si trebuie sa respecte formatul acesta
if( strlen(var2) == 3)
{
for( m = 0; m < strlen(var2); m++ )
if(!isalpha(var2[m]))
return 0;
}
else return 0;
break;
case 3:
//anul.
i = atoi(var2);
if(i == 0)
return 0;
break;
}
var2 = strtok(NULL, " ");
p++;
}
break;
case 4:
//cine a adaugat citatul, vom folosi functia searchAuthor dupa ce va fi gata.
for( m = 0; m < strlen(var); m++ )
if(!isalpha(var[m]))
return 0;
break;
case 5:
//citatul
if(strlen(var) == 0)
return 0;
printf("%d x \n", strlen(var));
}
var = strtok(NULL, ";"); //trecem la urmatorul sir de car separat de ;
k++;
}
}
j++; //trecem la urmatoarea linie
}
}
return 1;
}
And k gets only to 3, so it gets only the number, the author and the date. No quote and no author. So I can’t check them and see if it’s true
You can start by taking your first loop away and other variables too.
The first strtok has to be outside the loop that is going to help you divide each token, this has to be done in order to store the buffer you want to treat in the strtok function.
You can’t reuse the strtok function until you are certain that you do not want to divide your main data anymore, because if you reuse strtok before the end of the main treatment you are reseting the data used by the strtok function.
example:
If the sscanf function is allowed in your assignment and since you seem to know the exact format of your file, you may want to use it.
Also the getline function allows you to fetch line by line of your file, and you could treat each sentence at a time.