函数返回空时程序崩溃

时间:2015-10-11 18:25:30

标签: c function crash return

我想检查我的预定义数据是否在我拥有的.dat文件中。如果是这样,我打印它们。如果没有,我的程序崩溃,因为我写的函数没有任何回报。

我该如何解决这个问题?如何检查是否没有匹配并说“不匹配”并阻止我的程序崩溃?

struct objectList用于我的预定义数据。 struct database用于.dat文件中的数据。

struct objectList
{
    double oArea;
    double oLength;
    double oFormFactor;
};

struct database
{
    char name[15];
    double dArea;
    double dLength;
    double dFormFactor;
};

如果只有“area”,“length”和“formFactor”匹配,则此函数读取文件并返回值。

struct database myReadFile(FILE *rPtr, double area, double length, double formFactor)
{
    struct database fromDb = { "", 0.0, 0.0, 0.0 };

    if ((rPtr = fopen("objects.dat", "r")) == NULL)
        printf("Couldn't open the file to read.\n");
    else
    {
        fscanf(rPtr, "%s%lf%lf%lf", fromDb.name, &fromDb.dArea, &fromDb.dLength, &fromDb.dFormFactor);

        while (!feof(rPtr))
        {
            if (fromDb.dArea == area)
                if (fromDb.dLength == length)
                    if (fromDb.dFormFactor == formFactor)
                    {
                        printf("We have found the %s\n", fromDb.name);
                        return fromDb;
                    }

            fscanf(rPtr, "%s%lf%lf%lf", fromDb.name, &fromDb.dArea, &fromDb.dLength, &fromDb.dFormFactor);
        }
    }
    fclose(rPtr);
}

此功能打印值。

void getValues(FILE *gPtr, double area, double length, double formFactor)
{
    struct database objectValues = { "", 0.0, 0.0, 0.0 };

    objectValues = myReadFile(gPtr, area, length, formFactor);

    printf("Object name: %s\n", objectValues.name);
    printf("Object area: %.2lf\n", objectValues.dArea);
    printf("Object length: %.2lf\n", objectValues.dLength);
    printf("Object formFactor: %.2lf\n", objectValues.dFormFactor);
}

主:

int main()
{
    struct objectList myObjectList[4];
    myObjectList[0] = { 9214.00, 417.24, 22.08 };       // Coin
    myObjectList[1] = { 54375.00, 1087.94, 49.97 };     // mp3
    myObjectList[2] = { 12785.00, 550.70, 23.22 };      // toy
    myObjectList[3] = { 100.01, 200.02, 300.03 };       // Random stuff

    FILE *cfPtr;

    if ((cfPtr = fopen("objects.dat", "a +")) == NULL)
        printf("Couldn't open the file\n");
    else
        fclose(cfPtr);

    int i = 2;
    getValues(cfPtr, myObjectList[i].oArea, myObjectList[i].oLength, myObjectList[i].oFormFactor);
    return 0;
}

我的.dat文件有“玩具”,“硬币”,“mp3”但不是“随机的东西”。所以当i = 3时,我的程序崩溃了。

1 个答案:

答案 0 :(得分:1)

我看到你的代码的主要问题是阅读循环,它主要有两个问题

  1. 阅读:Why is while (!feof(rPtr)) always wrong

  2. 您必须检查scanf()的返回值,并防止scanf()溢出目标缓冲区,因此循环条件应为

    while (fscanf(rPtr, "%14s%lf%lf%lf", fromDb.name, &fromDb.dArea, 
               &fromDb.dLength, &fromDb.dFormFactor) == 4)
    
  3. 这个怎么样

    int
    myReadFile(struct database *db, double area, double length, double formFactor)
    {
        FILE *rPtr;
        if ((rPtr = fopen("objects.dat", "r")) == NULL)
            printf("Couldn't open the file to read.\n");
        else
        {
            while (fscanf(rPtr, "%14s%lf%lf%lf", db->name, &db->dArea, 
                &db->dLength, &db->dFormFactor) == 4)
            {
                if ((db->dArea != area) || (db->dLength != length) || 
                    (db->dFormFactor != formFactor))
                {
                    continue;
                }
                fclose(rPtr);
                return 0;
            }
        }
        fclose(rPtr);
        return -1;
    }
    

    并在主要功能

    struct database result;
    if (getValues(&result, myObjectList[i].oArea, 
        myObjectList[i].oLength, myObjectList[i].oFormFactor) == 0)
    {
        fprintf(stderr, "%s\n", result.name);
    }