【问题标题】:My c program keeps crashing on as it's starting我的 c 程序在启动时不断崩溃
【发布时间】:2020-06-03 19:16:02
【问题描述】:

我正在为一个简单的库制作一个 c 程序,但由于某种原因,该程序在启动时不断崩溃。就像显示了一个菜单但它甚至没有出现它只是崩溃了。谁能帮帮我?

struct library
{
    char name[50];
    int id;
    int qty;
}books[50],copy[50],delet[50],sort[50];
int i=0;
FILE *mybooks;

int main()
{
    int choice; char ans;
    int id;
    int qty;
    int s,o=0,j=0;
    char name[50];
    mybooks=fopen("D:\\mybooks.txt","r");
    if (mybooks == NULL) printf("Error. File not found.");
    else
    {
        while(!feof(mybooks))
        {
            fscanf(mybooks,"%[^\n] %d %d",books[i].name,&books[i].id,&books[i].qty);
            strcpy(copy[i].name,books[i].name);
            copy[i].id=books[i].id;
            copy[i].qty=books[i].qty;
            i++;
        }
        fclose(mybooks);
    }
    printf("Welcome to the Library.\n");
    do
    {

        printf("Please choose an option:\n");
        printf("1.Insert a book\n");
        printf("2.Delete a book\n");
        printf("3.Search a book by ID\n");
        printf("4.Search a book by name\n");
        printf("5.Display all books (sorted by name)\n");
        printf("6.Display all books (unsorted)\n");
        scanf("%d",&choice);
        switch (choice){
        case 1:
            printf("You will need to enter a name, ID, and quantity of the book.\n");
            printf("please enter book name:");
            fflush(stdin);
            fgets(name,sizeof name,stdin);
            printf("please enter book ID:");
            scanf("%d",&id);
            printf("please enter book quantity:");
            scanf("%d",&qty);
            InsertBook(name,id,qty);
            printf("your book has been added successfully\n");
            break;
        case 2:
            printf("Please enter book ID:");
            scanf("%d",&id);
            DeleteBook(id);
            printf("book successfully deleted.\n");
            break;
        case 3:
            printf("Please enter ID of Book:");
            scanf("%d",&id);
            s=LinearSearch(id,j);
            if (s>=0)
            {
                printf("Book Found.\n");
                printf("Name:%s",books[s].name);
                printf("ID:%d\n",books[s].id);
                printf("Quantity:%d\n",books[s].qty);
            }
            else
                printf("Sorry, the book doesn't exist.\n");
            break;
        case 4:
            printf("Please enter name of book:");
            fflush(stdin);
            gets(name);
            sorting();
            s=BinarySearch(name,0,i);
            printf("Book Found.\n");
            printf("ID:%d\n",sort[s].id);
            printf("Quantity:%d\n",sort[s].qty);
            break;
        case 5:
            sorting();
            while (o<i);
            {
                printf("%s\n",sort[o].name);
                o++;
            }
            printf("\n");
            break;
        case 6:
            while(o<i)
            {
                printf("%s",books[o].name);
                o++;
            }
            break;
        default:
            printf("Invalid Choice. Please try again.\n");
            break;
        }
        printf("do you want to choose another option?(y/n)  ");
        scanf(" %c",&ans);
    }while(ans == 'y');
}

(我正在为库使用函数,但我认为它们不会引起任何问题,因为我还没有调用它们。) 编辑问题以添加结构

【问题讨论】:

  • 您忽略scanf() 和朋友的返回值,风险自负。它通常对调试很有帮助。如果失败,请报告所有返回值。注意:返回值,而不是扫描值。
  • 您在读取文件数据时没有检查数组边界。即使您认为您已经完全为未显示的books[] 定义了正确的数组大小,由于feof 的使用不正确,还会额外增加一条记录。此外,还有其他基本检查未进行。 %[^\n] 将在接下来的两个 %d 数据项之后读取它们是否在同一行上,并且您不会知道,因为您不检查 fscanf 的返回值 and 它可能会破坏不受限制的字符串数组大小:%[^\n] 应该是 %49[^\n] 或者比数组长度小 1。
  • 另外,never use gets(),它已被弃用,因为它可能导致缓冲区溢出。

标签: c crash


【解决方案1】:

在对输入文件执行任何 i/o 之前,您已经测试了文件结束条件。然后,当您使用fscanf() 执行 i/o 时,您没有测试结果以查看变量是否被成功读取。

发生的情况是 scanf() 可能失败并且您没有到达循环的末尾,即 EOF 条件。你被困在那里,直到你的一项任务(strcpy(copy[i].name,books[i].name);copy[i].id=books[i].id;copy[i].qty=books[i].qty;)最终导致溢出。

要验证这一点,请在调试器中运行代码。

使用fscanf() 非常棘手,请务必彻底测试。

【讨论】:

    猜你喜欢
    • 2014-04-28
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多