1

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.

2 Answers2

2

consider fgets for all input.
strpbrk can be used to see if the line contains a |.
sscanf can parse the line to see if there are two values.

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:");
    fgets ( teste, sizeof teste, stdin);
    teste[strcspn ( teste, "\n")] = '\0';//remove newline

    pfile = fopen("registos.txt", "r");

    while( fgets ( line, sizeof line, pfile))
    {
        if( strpbrk ( line, "|"))//the line contains a |
        {
            //sscanf for two items
            result = sscanf ( line, "%20[^|]|%20s", procuto_nome, produto_quantidade);

            if(result == 2) {
                if((strcmp(produto_nome,teste) == 0))
                {
                    //DO THE REST OF THE WORK HERE
                    printf("HERE");
                }
                else
                {
                    printf("ERROR");
                }
            }
            else {
                printf ( "ERROR line did not contain two values\n");
            }
        }
        else {
            printf ( "ERROR line does not contain |\n");
        }
    }
    fclose(pfile);
}
xing
  • 2,125
  • 2
  • 14
  • 10
1

Your condition of checking blank line is not correctly coded.

Change

line != "\n"

to

line[0] != '\n'

Because this condition was not correct, it was always getting satisfied. Later inside the if block strtok was returning null because there was no pipe symbol in the empty line. So effectively, you were passing null to the strcpy function which was crashing your program.

VHS
  • 9,534
  • 3
  • 19
  • 43