c - 带有结构的for循环导致崩溃

标签 c struct

enter image description here所以这里基本上我有两个循环,它们基本上做同样的事情,除了它们 fscanf 到不同的目录。 第二个应该 fscanf 到一个结构,一个导致程序崩溃。 这是为什么????? 导致程序崩溃的代码是程序中的最后一个for循环。

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAXLEN 100

int main()
{
    char filename1[MAXLEN];
    char filename2[MAXLEN];
    char filename3[MAXLEN];
    char filename4[MAXLEN];


    char itemname[MAXLEN];
    printf("Enter the input file: ");
    scanf("%s", filename1);
    strcpy(filename3,filename1);
    strcat(filename3,"Output.txt");
    strcpy(filename4,filename1);
    strcat(filename4,"Log.txt");
    strcpy(filename2, filename1);
    strcat(filename2, "Customers.txt");
    strcat(filename1, ".txt");
    printf("%s will be used",filename1);

    FILE *inputfile1 = NULL;
    FILE *inputfile2 = NULL;
    FILE *outputfile = NULL;
    FILE *logfile = NULL;

    inputfile1 = fopen(filename1, "r");
    inputfile2 = fopen(filename2, "r");
    outputfile = fopen(filename3, "w");
    logfile = fopen(filename4, "w"); 

    int numberofitems =0;
    while (fscanf(inputfile1,"%s",itemname)==1){
        numberofitems++;
    }
    rewind(inputfile1);
    numberofitems /= 4;

    struct storestock{ 
    char itemnames[numberofitems][MAXLEN];
    int isdecimal[numberofitems];
    double stock[numberofitems];
    double price[numberofitems];
    };

    typedef struct storestock store;

    store inventory;
    int i;
    for (i=0; i < numberofitems; i++)
    {
    fscanf(inputfile1,"%s %d %lf %lf",inventory.itemnames[i],&(inventory.isdecimal[i]),
    &(inventory.stock[i]),&(inventory.price[i]));
    printf("\n %dst item %s %d %lf %lf", i+1,inventory.itemnames[i],inventory.isdecimal[i]
    ,inventory.stock[i],inventory.price[i] );
    }

    struct customers{
        char customername[MAXLEN];
        char wanteditems[10][MAXLEN];
        double amountwanted[10];
    };


    int j,k,l;
    int numberofcustomers = 0;
    int itemnumber=0;
    double itemamount;
    char string[MAXLEN];
    for (j=0;j<100;j++){
        if (fscanf(inputfile2,"%s %lf", string,&itemamount)==1){
            numberofcustomers++;
            printf("\n%s",string);
    }}
    printf("%d", numberofcustomers);

    struct customers mycustomers[numberofcustomers];
    rewind(inputfile2);

    **for (k=0;k<100;k++){
        if (fscanf(inputfile2,"%s %lf", mycustomers[k].customername,&itemamount)==1){
            printf("\n%s", mycustomers[k].customername);}
            }**
    getch();
    return 0;       
}

最佳答案

这段代码是非法的:

struct storestock{ 
    char itemnames[numberofitems][MAXLEN];

结构中数组的维度必须是常量表达式(灵活数组成员除外,这不是)。

您需要重新设计代码才能避免这样做。很难看出您的编译器是如何通过这一行的。

更好的方法是让 struct storestock 实际上只有一个项目,然后你有一个这样的结构数组(可以有大小 项目数)。与您对 struct customers 所做的类似。


代码的第一部分,在 FILE * 行之前,对缓冲区执行大量写入,但没有检查大小。这可能会导致缓冲区溢出,从而导致不可预测的行为。最好用经过长度检查的 scanf 替换所有这些废话,然后使用 snprintf 而不是 strcpystrcat.

关于c - 带有结构的for循环导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237886/

相关文章:

c - 我的老师做了这个功能,但我不明白它有什么用

c - 使用 malloc 为其分配内存后如何加载结构?

改变全局变量的值,C

c - 如何在循环写入时动态更改文件名

用于在 Intel Core 2 Duo 上对齐的 C 代码

go - 从 SQL QueryRow 结果动态生成结构字段

c - 从偏移处的文件描述符读取/写入

c - 为什么这段代码在没有 volatile 声明的情况下会失败?

c - 类型定义不完整 (struct)

C - 将 "h_addr_list"数组转换为类型 "struct in_addr **"