2017-05-22 6 views
0

Ich habe ein c-Programm geschrieben, die Daten von einer Datei in die nächste leitet, aber es war Endlosschleife.Unendliche Schleife durch Perror Scanf durch eine Leitung

Was ich bisher entdeckt habe. Die Endlosschleife wird in der Datei c1.c verursacht, wo perror (oder stderr) scanf überspringt. Wenn scanf funktioniert. Das Programm läuft endlos weiter und druckt den Perror aus, obwohl es hinter diesem Abschnitt ist!

Mein Code ist unten

controller.c

#include<stdio.h> 
#include<stdlib.h> 
#include<sys/wait.h> 
#include<unistd.h> 

int main(int ac, char**av) 
{ 
int pipez[2]; 
int piped[2]; 
int status; 
pid_t pid; 

if (pipe (pipez) == -1){ 
perror("could not make pipe"); 
return 1; 
} 

if ((pid = fork()) == -1){ 
perror("fork"); 
return 1; 
} 

if(pid == 0){ 
close(pipez[1]); 
dup2(pipez[0],0); 
close(pipez[0]); 
execvp("./c1",av); 
perror("demo); 
_exit(1); 
} 
else{ 
close(pipez[0]); 
dup2(pipez[1],1); 
close(pipez[1]); 
execvp("./c2",av); 
perror("demo"); 
exit(1); 
} 

waitpid(pid,&status,0); 
if(WIFEXITED(status)){ 
printf("[%d] TERMINATED (Status: %d)\n", pid, WEXITSTATUS(status)); 
} 

if(pipe (piped) == -1){ 
perror("could not make pipe"); 
return 1; 
} 

if((pid = fork()) == -1){ 
perror("fork"); 
return 1; 
} 

if (pid == 0){ 
close(piped[1]); 
dup2(piped[0],0); 
close(piped[0]); 
execvp("./c2", av); 
perror("demo"); 
_exit(1); 
} 
else{ 
close(piped[0]); 
dup2(piped(piped[1],1); 
close(piped[1]); 
execvp("./c3",av); 
perror("demo"); 
exit(1); 
} 

waitpid(pid, &status, 0); 
if (WIFEXITED(status)){ 
printf("[%d] TERMINATED (Status: %d)\n", pid, WEXITSTATUS(status)); 
} 

return 0; 
} 

c1.c

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define BUFSIZE 256 

//This program is causing infinite loop, tried fflush and fgets and scanf 
// It will run independently but will loop via the pipe 

int main(int a, char**av){ 
char store[BUFSIZE]; 
memset(store, '\0', sizeof(store)); 
while(strcmp(store, "exit!") != 0){ 

perror("Please enter next line of input (type 'exit!' to stop) \n"); //This repeats itself infinitely 
fgets(store, BUFSIZE, stdin); 
printf("%s",store); // This also repeats itself dependant on where i put 
//fflush or another printf. Repeated outputs occur in blocks 
} 

return 0; 
} 

c2.c

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define BUFSIZE 256 

char *strlwr(char *str); 

int main(int ac, char**av){ 
char store[BUFSIZE]; 

while(strcmp(store, "exit!") != 0){ 
scanf("%s", store); 
printf("%s", strlwr(store)); 
} 

return 0; 
} 

char *strlwr(char *str){ 
unsigned char *p = (unsigned char *)str; 

while(*p){ 
*p = tolower((unsigned char)*p); 
p++; 
} 

return str; 

} 

c3.c

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define BUFSIZE 256 

int main(int ac, char**av){ 
char store[BUFSIZE]; 
int n = 0; 

while(strcmp(store, "exit!") != 0{ 
scanf("%s",store); 
printf("Line %d: %s\n",n,store); 
n++; 

} 

return 0; 
} 
+0

'memset (Laden, '/ 0', sizeof (Speicher));' ----> 'memset (store" \ 0 ', sizeof (speichern)); ' – LPs

+0

Danke LPs, ich hatte das im Code zu beginnen, aber ich muss es in der Post vertippt haben. Ich habe es jetzt nochmal überarbeitet! – rowboat

+0

Beachten Sie, dass 'perror()' eine Fehlermeldung über den Standardfehler basierend auf dem Wert von 'errno' ausgeben soll - es handelt sich nicht um eine allgemeine Aufforderungsfunktion. –

Antwort

0

fgets verläßt '\n' char im store Puffer so, einfachste Lösung ist '\n' den verglichenen Zeichenfolge hinzuzufügen:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define BUFSIZE 256 

//This program is causing infinite loop, tried fflush and fgets and scanf 
// It will run independently but will loop via the pipe 

int main(int a, char**av) 
{ 
    char store[BUFSIZE]; 
    memset(store, '\0', sizeof(store)); 
    while (strcmp(store, "exit!\n") != 0) 
    { 

     perror("Please enter next line of input (type 'exit!' to stop) \n"); //This repeats itself infinitely 
     fgets(store, BUFSIZE, stdin); 
     printf("%s", store); // This also repeats itself dependant on where i put 
          // fflush or another printf. Repeated outputs occur in blocks 
    } 

    return 0; 
} 

Außerdem ist, wie Sie, init Wert sehen '/ 0' ist nicht akzeptabel; es muss '\ 0' sein. Ihr Compiler wahrscheinlich sagt Ihnen etwas wie

test.c: In function ‘main’: 
test.c:242:16: warning: multi-character character constant [-Wmultichar] 
    memset(store, '/0', sizeof(store)); 
       ^

BTW memset, ist in diesem Fall nutzlos, weil fgets macht die ganze Arbeit, aber wie auch immer, wenn Sie möchten, dass Sie auf nul Pufferset, erklären nur:

char store[BUFSIZE] = {0}; 

Beste Lösung fgets mit ist (natürlich) immer die newline verwalten links:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define BUFSIZE 256 

//This program is causing infinite loop, tried fflush and fgets and scanf 
// It will run independently but will loop via the pipe 

int main(void) 
{ 
    char store[BUFSIZE] = {0}; 
    char *pos; 
    while (strcmp(store, "exit!") != 0) 
    { 

     perror("Please enter next line of input (type 'exit!' to stop) \n"); //This repeats itself infinitely 
     fgets(store, BUFSIZE, stdin); 

     if ((pos = strchr(store, '\n')) != NULL) 
      *pos = '\0'; 
     else 
      fprintf(stderr, "String too long!\n"); 

     printf("%s", store); // This also repeats itself dependant on where i put 
          // fflush or another printf. Repeated outputs occur in blocks 
    } 

    return 0; 
} 
+0

@JonathanLeffler Du bist schneller als meine Rechtschreibprüfung;) Thx – LPs

+0

Hallo Leute, immer noch kein Glück. das fgets erlaubt mir jetzt eingabe einzugeben aber wenn ich tippe exit! Das Problem tritt erneut auf. Wenn ich "c2" "exit \ n" hinzufüge, hört das Problem auf, aber es fragt immer nach Eingabe und verlässt die Schleife nicht. Ich habe den vorgeschlagenen Code hinzugefügt. Vielleicht ist das ein Problem mit der Pfeife? – rowboat

+0

@rowboat, wie Sie [HIER] sehen können (http://ideone.com/qbLXcV) der Code, den ich gepostet hat funktioniert wie ein Charme – LPs