【问题标题】:Create a file and write struct with data member and the file created is empty创建一个文件并使用数据成员写入结构,创建的文件为空
【发布时间】:2021-05-16 20:13:54
【问题描述】:

我创建了一个程序,提示用户选择一个选项,如下所示:

  1. 创建文件并命名文件。
  2. 在文件中添加组件
  3. 显示文件中的项目
  4. 退出程序

所以我的问题是该文件已创建并存在于我的目录中,但它是空的,那么我在哪里遗漏了什么?另外你能检查一下2和3选项程序是否正常吗?

这是我的程序:

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

int main() { 
    int num, name;
    
start:
        
    printf("\n1. Create a file.");
    printf("\n2. Add a component to the list.");
    printf("\n3. Display the current list of component.");
    printf("\n4. Exit program.");
    printf("\n\nChoose either these four menu = ");
    scanf("%d", &num);
    fflush(stdin);

    switch (num) {
      case 1:
        printf("\n\nPlease enter file name: ");
        scanf("%d", &name);
        FILE *pf = NULL;
        char username[250];
        char userfile[255];
        
        printf("username: ");
        scanf("%s", username);
        sprintf(userfile, "%s.txt", username);
        fflush(stdin);
        goto start;
        break;

      case 2:
        pf = fopen(userfile, "w");
        if (!pf) {
            fprintf(stderr, "File opening failed!\n");
            return EXIT_FAILURE;
        }
        struct date {
            int day;
            int month;
            int year;
        };
        struct details {
            char name[20];
            int price;
            int code;
            int qty;
            struct date mfg;
        };
        struct details item[50];
        int n, i;
        
        printf("Enter number of items:");
        scanf("%d", &n);
        fflush(stdin);
        for (i = 0; i < n; i++) {
            fflush(stdin);
            printf("Item name: \n");
            scanf("%s", item[i].name);
            fflush(stdin);
            printf("Item code: \n");
            scanf("%d", &item[i].code);
            fflush(stdin);
            printf("Quantity: \n");
            scanf("%d", &item[i].qty);
            fflush(stdin);
            printf("price: \n");
            scanf("%d",  &item[i].price);
            fflush(stdin);
            printf("Manufacturing date(dd-mm-yyyy): \n");
            scanf("%d-%d-%d", &item[i].mfg.day,
                  &item[i].mfg.month, &item[i].mfg.year);
        }
        fclose(pf);
        goto start;
        break;
        
      case 3:
        pf = fopen(userfile, "r");
        if (!userfile) {
            fprintf(stderr, "File opening failed!\n");
            return EXIT_FAILURE;
        }
        {
            printf("             *****  INVENTORY ***** \n");
            printf("------------------------------------------------------------------\n");
            printf("S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE | MFG.DATE \n");
            printf("-------------------------------------------------------- ---------\n");
            for (i = 0; i < n; i++)
                fprintf("%d     %-15s        %-d          %-5d     %-5d %d/%d/%d \n",
                        i + 1, item[i].name, item[i].code, item[i].qty,
                        item[i].price, item[i].mfg.day, item[i].mfg.month,
                        item[i].mfg.year);
            printf("------------------------------------------------------------------\n");
        }
        fclose(pf);
        goto start;
        break;
    
      case 4:
        printf("Exit Program, Thank You, Sayonara");
        break; 
    }
    return 0;
}

【问题讨论】:

  • if (!userfile) { -> if (!pf) { 看起来您打开文件进行读取然后尝试写入它。这真是一团糟。
  • 关于:fflush(stdin); 这在 C 标准中被明确称为未定义行为
  • 关于:scanf("%s", item[i].name);%s,始终包含比输入字段长度小 1 的 MAX WIDTH 修饰符。 1 少,因为%s 总是在输入中附加一个 NUL 字节

标签: c


【解决方案1】:

您的代码存在多个问题:

  • 测试if (!userfile) 不正确:您应该改为测试if (!pf)
  • 你打开文件是为了阅读,你应该用"w"打开它来写,或者可能用"a"追加。
  • 您永远不会写入文件。您应该使用fprintf(pf, ...) 而不是printf
  • 数组userfileitem 和变量inswitch 语句中是局部的:当您转到start 外部switch 之外的标签时,它们的内容超出范围陈述。将所有这些定义移到 switch 语句之外。
  • fflush(stdin); 具有未定义的行为。您可以使用 (flush) 输入行的其余部分:int c; while ((c = getchar()) != EOF &amp;&amp; c != '\n') continue; 您可以定义一个函数 flush() 来执行此操作。

【讨论】:

  • 好的,谢谢您的信息,我可以知道,我应该在哪里更改 fprint(pf,....)?我假设在陈述案例 2 对吗?
  • @MuhammadRahimy:不清楚何时应该写入文件。选项 1 获取用户名,2 创建文件并读取项目,3 打印本地数组中的项目,4 退出。
  • 我使用 ffprint() ,他们现在写入文本文件。但问题是,在输入时它没有显示在屏幕上。所以我可以将屏幕的输出加倍并写入文本文件?示例 fprint() 及以下 printf()?
猜你喜欢
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2011-02-22
  • 2019-08-03
  • 2021-08-16
  • 1970-01-01
相关资源
最近更新 更多