Reputation: 407
I'm using a simple reading in my program using fgets and some break conditions, and for some reason when I compile and run the program with ./program < input.txt it presents a Seg Fault.
But when I run the program with ./program and paste the same content that the input file has, it runs normally.
This is the code I'am using:
void readInput()
{
char lote[20], str[505];
char *msg;
int par, ord, k;
scanf("%d", &k);
while(1)
{
__fpurge(stdin);
fgets(lote, 20, stdin);
if (strcmp(lote, "-1\n") == 0)
break;
while(1)
{
__fpurge(stdin);
fgets(str, 505, stdin);
if(strcmp(str, "Fim\n") == 0)
break;
par = atoi(strtok(str, ";"));
ord = atoi(strtok(NULL, ";"));
msg = strtok(NULL, "\n");
printf("Par = %d/Ordem = %d/Mensagem=%s\n", par, ord, msg);
}
}
}
Edit: This is the input file:
10
Lote 1
3;2;Estou fazendo agora
0;1;Olá, tudo bem com você?
1;2;Não
1;1;Você vem jantar hoje?
0;2;Tudo sim e você?
1;3;Vou ter que trabalhar até mais tarde
2;4;se você lembrar
3;1;Conseguiu fazer o TP?
3;3;Esta muito fácil
Fim
Lote 2
2;2;Vou
2;3;leva na aula amanha
0;3;Tudo bem tbm
4;2;Parabéns! Que dia vamos comemorar?
2;1;Vai precisar da grana?
4;1;Passei no vestibular!
3;4;Esta mesmo
Fim
-1
Upvotes: 0
Views: 53
Reputation:
I think @dbush already explained the problem quite well, but just for completeness, here's some perfectly working code with just a minor variation of the original:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char lote[20], str[505];
char *msg;
int par, ord, k;
fgets(lote, 20, stdin);
k = atoi(lote);
while(1)
{
fgets(lote, 20, stdin);
if (strcmp(lote, "-1\n") == 0)
break;
while(1)
{
fgets(str, 505, stdin);
if(strcmp(str, "Fim\n") == 0)
break;
par = atoi(strtok(str, ";"));
ord = atoi(strtok(NULL, ";"));
msg = strtok(NULL, "\n");
printf("Par = %d/Ordem = %d/Mensagem=%s\n", par, ord, msg);
}
}
}
Test run:
> ./lote <lote-input.txt
Par = 3/Ordem = 2/Mensagem=Estou fazendo agora
Par = 0/Ordem = 1/Mensagem=Olá, tudo bem com você?
Par = 1/Ordem = 2/Mensagem=Não
Par = 1/Ordem = 1/Mensagem=Você vem jantar hoje?
Par = 0/Ordem = 2/Mensagem=Tudo sim e você?
Par = 1/Ordem = 3/Mensagem=Vou ter que trabalhar até mais tarde
Par = 2/Ordem = 4/Mensagem=se você lembrar
Par = 3/Ordem = 1/Mensagem=Conseguiu fazer o TP?
Par = 3/Ordem = 3/Mensagem=Esta muito fácil
Par = 2/Ordem = 2/Mensagem=Vou
Par = 2/Ordem = 3/Mensagem=leva na aula amanha
Par = 0/Ordem = 3/Mensagem=Tudo bem tbm
Par = 4/Ordem = 2/Mensagem=Parabéns! Que dia vamos comemorar?
Par = 2/Ordem = 1/Mensagem=Vai precisar da grana?
Par = 4/Ordem = 1/Mensagem=Passei no vestibular!
Par = 3/Ordem = 4/Mensagem=Esta mesmo
Just adding: this code heavily relies on well-formed input. For production quality code, always assume you could get fed garbage (so, for example, while(1)
might be a bad idea, at least if you don't have a break
for error detected)
Upvotes: 0
Reputation: 223709
It looks like __fpurge
is the culprit here. It's flushing the entire input buffer. The reason it's working by pasting the lines into the console is that only one line at a time is read, while using an input redirection feeds the whole thing in at once.
After the scanf
, do a single call to getchar
to absorb the newline that's left in the buffer. Then get rid of the __fpurge
calls, and it should work.
Upvotes: 1