如何在c中读取输入后清除缓冲区?

时间:2017-12-07 18:03:30

标签: c pointers struct char

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

void generarDimension(int, void **, int);
void reallocarDimensiones(int, void **, int);
void preguntarValor(int *);
char *generarCadena(char *);

typedef struct listaAlu{//this is a list of students
    char cedula[7];//this means id
    char *nombreApellido;//this means name and lastname
    short curso;//this means the year of the student in college/school
    unsigned short anho;//and this means year-E.g: 2017
};

int main(void){
    struct listaAlu *alu = NULL;
    int salir=1;
    char c;
    unsigned int cont = 0;
    unsigned int i;
    size_t size = 0;
    struct listaAlu *temp = NULL;
    while(salir!=0){//Condition until user press exit option
        printf("Agregar Alumno?Ingrese 1 para Agregar, y 0 para salir\n");//This means "Add student?Press 1 to add, and 0 to exit
        preguntarValor(&salir);//this verifies user input
        if(salir==1){//if user adds then it asks info of the student
            cont++;
            if ( NULL == ( temp = realloc ( alu, size + 2))) {//realloc for each character input and '\0'
                fprintf ( stderr, "realloc problem\n");
            }//this is for increasing the number of students
            alu = temp;
            printf("Ingrese Cedula:\n");//This means "Enter id"
            scanf("%s", &(alu+(size))->cedula);
            printf("Ingrese Nombre y Apellido:\n");//This means "Enter name and lastname"
            (alu+(size))->nombreApellido = generarCadena((alu+(size))->nombreApellido);
            printf("Ingrese Curso:\n");//This means enter college/school year
            scanf("%hd", &(alu+(size))->curso);
            printf("Ingrese Anho:\n");//and this means "Enter year"
            scanf("%hd", &(alu+(size))->anho);
            size++;
        }
    }
    for(i=0;i<cont;i++){
        printf("\tAlumno %d\n", i+1);
        printf("Nombre:\t%s\n", (alu+i)->nombreApellido);
        printf("Cedula:\t%s\n", (alu+i)->cedula);
        printf("Curso:\t%d\n", (alu+i)->curso);
        printf("Anho:\t%d\n", (alu+i)->anho);
    }//This prints Everything the user entered
    free(alu);
    return 0;
}//Sorry for writing my program entirely on the main >.<

void generarDimension(int bloques, void **ptr, int tamanho){
    void **ptrAux;
    *ptrAux = malloc(bloques * tamanho);
    if(*ptrAux==NULL){
        printf("No se pudo almacenar memoria\n");
    }else{
            *ptr = *ptrAux;
            free(ptrAux);
        }
}//this I didn't use, but it is for generating an array through a pointer
void reallocarDimensiones(int bloques, void **ptr, int tamanho){
    void **ptrAux;
    *ptrAux = realloc(*ptr,bloques * tamanho);
    if(*ptrAux==NULL){
        printf("No se pudo almacenar memoria\n");
    }else{
            *ptr = *ptrAux;
            free(ptrAux);
        }
}//This I didn't either, but it is for reallocating any given pointer

void preguntarValor(int *n){
    long entero, lector;
    *n=-2;
    do{
        printf("\nSi ingresa otro numero o caracter, vuelva a ingresar opcion\n");
        while(!scanf("%d", &entero)){
            while((lector=getchar()) !='\n' && lector !=EOF );
        }
        *n=entero;
    }while(*n>1 || *n<0);//You can change this parameters according to the numbers you want
}//This subrutine scans only integers between 1 and 0

char *generarCadena(char *scaneado){
    char *temp = NULL;
    int in = 0;
    size_t size = 0;

    while ( '\n' != ( in = getchar ( ))) {//loop until newline
        if ( NULL == ( temp = realloc ( scaneado, size + 2))) {//realloc for each character input and '\0'
            fprintf ( stderr, "realloc problem\n");
            return scaneado;
        }
        scaneado = temp;
        scaneado[size] = in;//set input
        scaneado[size + 1] = '\0';//terminate
        size++;
    }
    return scaneado;
}//This subrutine is for geting an input of chars of any given length

这是一个存储学院/学校学生名单的程序,我想知道如何改进这段代码,但是在我要求用户输入字符串的部分,在第二部分我无法输入任何东西。如果我尝试输入超过2名学生,我的代码不会打印我输入的学生的所有信息

1 个答案:

答案 0 :(得分:0)

struct listaAlu *temp = NULL;
temp = realloc ( alu, size + 2);//<- wrong size

realloc(alu, size + 2)将在第一次运行中仅分配2个字节。但是你还需要更多。所需的总大小是数组中元素的数量乘以结构的大小。那是sizeof(struct listaAlu),大约15个字节。

realloc( alu, sizeof(struct listaAlu) * (size + 1) );

您不需要将尺寸增加2

确保检查正确位置的输入。 <{1}}失败时清除输入。

要获取名字和姓氏,您可以使用scanffgets

strdupgenerarDimension存在重大问题。你可能想把它们留下来。

reallocarDimensiones

<小时/> 的 修改

struct listaAlu{ char cedula[7];//this means id char *nombreApellido;//this means name and lastname short curso;//this means the year of the student in college/school unsigned short anho;//and this means year-E.g: 2017 }; void clear() { int c; while((c = getchar()) != '\n' && c != EOF); } int main(void) { struct listaAlu *alu = NULL; size_t size = 0; while(1) { alu = realloc( alu, sizeof(struct listaAlu) * (size + 1) ); if (!alu) { fprintf(stderr, "realloc problem\n"); break; } char buf[256] = { 0 }; printf("Enter id:\n"); fgets(buf, sizeof(buf), stdin); buf[strcspn(buf, "\r\n")] = 0; buf[sizeof(alu[size].cedula) - 1] = 0; strcpy(alu[size].cedula, buf); printf("Enter name and lastname:\n"); fgets(buf, sizeof(buf), stdin); buf[strcspn(buf, "\r\n")] = 0; alu[size].nombreApellido = _strdup(buf); for(;;) { printf("Enter college/school year:\n"); if (scanf("%hd", &alu[size].curso) == 1) break; printf("input error\n"); clear(); } for(;;) { printf("Enter year:\n"); if (scanf("%hd", &alu[size].anho) == 1) break; printf("input error\n"); clear(); } size++; printf("Press 1 to continue\n"); char input; scanf(" %c", &input); if(input != '1') break; clear(); } for(size_t i = 0; i < size; i++) { printf("Alumno %d\n", i); printf("Nombre:\t%s\n", alu[i].nombreApellido); printf("Cedula:\t%s\n", alu[i].cedula); printf("Curso:\t%hd\n", alu[i].curso); printf("Anho:\t%hd\n", alu[i].anho); } for(size_t i = 0; i < size; i++) free(alu[i].nombreApellido); free(alu); return 0; } 不需要包含在不同的函数中。但出于有趣的目的,您可以执行以下操作:

realloc

void* my_realloc1(void *ptr, int size) { ptr = realloc(ptr, size); if (!ptr) printf("error\n"); return ptr; } //usage: alu = my_realloc1(alu, sizeof(struct listaAlu) * (size + 1)); if (!alu) printf("error\n"); 可以在这里工作,但无法获得任何结果。这只是额外的代码行。直接使用my_realloc1并执行必要的错误处理更容易,更清晰。

另一种方法是传递引用:

realloc

这将再次起作用,但它只会添加更多代码。

函数void my_realloc2(void **ptr, int size) { *ptr = realloc(*ptr, size); } //usage: (*** Note the address `&` operator ***) my_realloc2(&alu, sizeof(struct listaAlu) * (size + 1)); if (!alu) printf("error\n"); 似乎没问题。您可以简化如下:

generarCadena

并使用char *my_getline() { char *buf = NULL; int in = 0; size_t size = 0; while('\n' != (in = getchar())) { size++; buf = realloc(buf, size); buf[size - 1] = (char)in; } size++; buf = realloc(buf, size); buf[size - 1] = 0; return buf; } //usage: printf("Enter name and lastname:\n"); alu[size].nombreApellido = my_getline(); 来读取6个字符(因为scanf的大小为7)

cedula

为安全起见,请确保输入为空终止,并清除缓冲区。

或者您可以像我在示例中使用的那样使用printf("Enter 6 characters:\n"); scanf("%6s", alu[size].cedula); alu[size].cedula[6] = '\0'; clear(); 。这最多可读取256个字符:

fgets

char buf[256]; fgets(buf, sizeof(buf), stdin); 有时更容易,因为您不需要清除缓冲区。但fgets包含行尾字符buf,在这种情况下您必须删除\n