Δημοσιεύτηκε: 26 Μάιος 2011, 16:05
Εννοείς πρέπει να απορριφθεί το υπόλοιπο standard input;
Το βρήκα στο http://www.gnu.org/software/libc/manual ... cter-Input
Ερώτηση: Γιατί δουλεύει το δεύτερο getchar() τότε; Το δοκίμασα και αφαιρεί όλους τους επακόλουθους χαρακτήρες. -- Άκυρο δε δουλεύει.
- Κώδικας: Επιλογή όλων
while (c != '\n' && c != EOF) { c = fgetc(stdin); }
Το βρήκα στο http://www.gnu.org/software/libc/manual ... cter-Input
- Κώδικας: Επιλογή όλων
/********************************************************************************
Άνοιγμα ενός αρχείου txt που επιλέγει ο χρήστης και εκτύπωση των αποτελεσμάτων.
Για να αρχίσει να εκτελείτε το πρόγραμμα θα πρέπει ο χρήστης να το εκτελέσει,
δίνοντας μαζί και τo αρχείο που θέλει να διαβάσει!
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
FILE *infile;
char ch;
int c, var, var2;
printf("\n");
infile = fopen(argv[1],"r");
if (infile == NULL) {
printf("Error in file open. The %s file does not exist!\nPlease try again typing: ./a.out <filename>.\n", argv[1]);
exit(1);
}
else {
printf("File %s has opened!\n", argv[1]);
printf("If you want to continue and see the content type [y/Y]:");
//scanf("%c", &var);
c = getchar();
var = c;
while (c != '\n' && c != EOF) { c = fgetc(stdin); }
}
if (isupper(var)) { var = tolower(var); }
if (var == 'y') {
while((ch = fgetc(infile)) != EOF) {
printf("%c", ch);
}
}
else {
printf("Program exited correctly!\nThank you for using it!;)\n");
exit(0);
}
printf("If you want to clear the screen type [y/Y]:");
//scanf("%c", &var2);
c = getchar();
var2 = c;
while (c != '\n' && c != EOF) { c = fgetc(stdin); }
if (isupper(var2)) { var2 = tolower(var2); }
if (var2 == 'y') {
printf("\033[H\033[J");
}
return 0;
}
Ερώτηση: Γιατί δουλεύει το δεύτερο getchar() τότε; Το δοκίμασα και αφαιρεί όλους τους επακόλουθους χαρακτήρες. -- Άκυρο δε δουλεύει.