Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                                                                                            
                
        
帮我解决这个编码...它总是说有错误:-

它说fwrite出现问题了。

// #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

    struct prod
    {
        char ProductCode [5];
        int Expired_year;
        char Product_country [25];
    }product;
    struct prod product;

void main()
{

    char x ;
    FILE* data;
    data = fopen("product.dat","wb");

    while(x != 'N')
    {
        printf("Enter product code :");
        scanf("%s", product.ProductCode);
        printf("Enter expired year of the product :");
        scanf("%d", &product.Expired_year);
        fflush(stdin);
        printf("Enter product country :");
        scanf("%[^\n]", product.Product_country);
        fflush(stdin);

        fwrite(&prod, sizeof(product), 1, data);

        printf("\nPlease enter anykey to continue or 'N' to stop: ");
        scanf("%c", &x);
        fflush(stdin);
        printf("\n");
    }
    fclose(data);

}

最佳答案

fwrite(&prod, sizeof(product), 1, data);


prod不是引用struct实例,而是结构的名称,应该是

fwrite(&product, sizeof(product), 1, data);

08-27 03:41