簡體   English   中英

使用'fgets'讀取char只是在運行時跳過

[英]Using 'fgets' to read in a char is just skipping when running

問題是在read_in函數中,當我使用fgets讀取樂隊/歌手的用戶輸入時,它將跳過此行。 我不知道為什么會這樣? 之前我確實使用過scanf,但是問題是,如果在輸入帶空格的樂隊名稱(例如“ foo fighters”)時這樣做,它將跳過參考行的下一個打印名稱。 有人知道怎么修這個東西嗎?

#include <stdio.h>
    #include <conio.h>
    #define MAX 1000`enter code here`

    int exit = 0;                                                                       // Declaring a global variable 

    struct data {                                                                       // using struct to put the data in an arrays
        char band [48], cd [48], ref [16];
        float cost;
        int year;
    }   cata [MAX];                                                                     // declaring a struct variable

    int read_in ( void )
    {
        int i = 0;
        printf("\n");

        while ( exit != 2 )                                                             // while exit != 2 then ask the user to enter the information
        {
            printf("\nPlease Enter the Name of Band/Singer: ");
            fgets(cata[i].band, 48, stdin);

            printf("\nPlease Enter the Name of CD: ");
            scanf("%s", &cata[i].cd);

            printf("\nPlease Enter the Name of the Reference: ");
            scanf("%s", &cata[i].ref);

            printf("\nPlease Enter the cost: ");
            scanf("%f", &cata[i].cost);

            printf("\nPlease Enter the Year of Release: ");
            scanf("%d", &cata[i].year);

            printf("\nWrite another CD: [1] \nMain Menu: [2]\nYour choice: ");          // Asking him a choice either write another one or go back to menu
            scanf("%d", &exit); 

            i++;
        }

        exit = 0;
        return i;
    }

    void print_out (void)
    {
        FILE *file_cata;
        int i = 0;

        printf("\n");

        while ( exit != 2 )                                             
        {
            file_cata = fopen ("catalogue.txt", "r");                               // open the file and read

            if (file_cata == NULL)
            {
                printf("Error: can't open files.\n");
            }
            else
            {
                while (!feof (file_cata))
                {
                    fgets(cata[i].band, MAX , file_cata);                           // it will scanf the file and get the string and then print it out on screen
                    printf("%s", cata[i].band);
                    fgets(cata[i].cd, MAX, file_cata);
                    printf("%s", cata[i].cd);
                    fgets(cata[i].ref, MAX, file_cata);
                    printf("%s", cata[i].ref);
                    fscanf(file_cata, "%.2f" , cata[i].cost);                       
                    fscanf(file_cata, "%d", cata[i].year);
                    i++;
                }
            }
            fclose (file_cata);                                                     // close file
            printf("Read it again: [1] \nMain Menu: [2]\nYour choice: ");
            scanf("%d", &exit);
        }
        exit = 0;
    }

    void save ( int num )
    {
        FILE *file_cata;
        int i = 0;

        printf("\n");

        while ( exit != 2 )
        {

            file_cata = fopen ("catalogue.txt", "a");                                   // file append, so it will write at the end of the file

            if (file_cata == NULL)
            {
                printf("Error: can't open files. \n");
            }
            else
            {
                while (i != num)
                {
                    fprintf( file_cata, "\n");
                    fprintf( file_cata, "The name of Band/Singer: %s \n", cata[i].band);
                    fprintf( file_cata, "The name of CD: %s \n", cata[i].cd);
                    fprintf( file_cata, "The name of Reference: %s\n", cata[i].ref);
                    fprintf( file_cata, "The Cost: %.2f \n", cata[i].cost);
                    fprintf( file_cata, "The Year of Release: %d \n", cata[i].year);
                    i++;
                }
            }
            fprintf( file_cata, "\n");
            fclose (file_cata);

            printf("Your data has been saved to the catalogue.\n\n");
            printf("Save it again: [1] \nMain Menu: [2]\nYour Choice: ");
            scanf("%d", &exit);
        }
        exit = 0;
    }

    void main ()
    {
        int num1 = 0;
        int option = 0;

        while ( option != 4 )
        {
            printf("The following options are availlable: \n");
            printf("Read in data [1] \n");
            printf("Print out catalogue to screen [2] \n");
            printf("Save data to file [3] \n");
            printf("Exit Program [4] \n");
            printf("Enter your choice now: ");
            scanf( "%d", &option);

            if (option == 1)
                num1 = read_in ();

            if (option == 2)
                print_out ();

            if (option == 3)
                save(num1);

            printf("\n");
        }

        printf("\n*** Your program will end when you press ANY button. ***\n");
        _getch();
    }

scanf將換行符保留在輸入緩沖區中。 一個很好的答案的相關問題(提示:不要使用scanf ;始終使用fgets ,如果必須以這種方式轉換的話還要使用sscanf 。): 替換fflush(stdin)

這將刷新輸入緩沖區,您應該在嘗試讀取另一行(從上面的答案中竊取)之前執行此操作:

while((c = getchar()) != '\n' && c != EOF)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM