I recently started working with files and i'm experiencing an error. I have a txt. file with the following strings:
a|10
b|5
My problem is when im reading the blank line, somehow it crashes even when i have the condition in the code. When debugging i can see that the line receives a "\n" but program doesnt recognize it in the condition and it crashes.
void delete_prod(void)
{
FILE *pfile;
char line[21];
char *buffer;
char *ptr;
char produto_nome[21];
char produto_quantidade[21];
char quantidade_nova[21];
char teste[21];
char barra_linha[]="\n";
buffer = (char *) malloc(1000*sizeof(char));
memset(buffer, 0, 1000*sizeof(char));
ptr = buffer;
printf("material:");
scanf("%s",teste);
pfile = fopen("registos.txt", "r");
while(!feof(pfile))
{
int i=0;
for(i; i<21;i++)
{
line[i] = 0;
}
fgets(line, 21, pfile);
if(line != NULL || line != "\n")
{
strcpy(produto_nome, strtok(line ,"|"));
strcpy(produto_quantidade, strtok(NULL, "|"));
if((strcmp(produto_nome,teste) == 0))
{
//DO THE REST OF THE WORK HERE
printf("HERE");
}
else
{
printf("ERROR");
}
}
}
fclose(pfile);
}
Have been researching here but didnt find anything that fixes my problem. Thanks in advance and i hope i made myself clear explaining the problem.