c++ - 调试 C 程序的断言错误

标签 c++ c pointers project

这是错误消息图片的链接:

http://www.flickr.com/photos/76298377@N02/6798897020/in/photostream

这是实际的编程问题。这是 3 号

http://books.google.com/books?id=bSy1hBCLNl8C&pg=PA335&lpg=PA335&dq=sales.dat+c%2B%2B&source=bl&ots=mmN9b4WzsN&sig=miAD8-u4ly8K1Mou9ZNHv90Nscc&hl=en&sa=X&ei=2wdQT_-4OtSCsgK-l5WyDg&ved=0CDcQ6AEwAg#v=onepage&q=sales.dat%20c%2B%2B&f=false

这是源代码

#include <iostream>
#include <string>
#include<stdio.h>

using namespace std;

#define numItems     8
#define numSalesP   10

// the product prices 
float prices  [numItems] = {345.0,  853.0, 471.0, 933.0, 721.0, 663.0, 507.0, 259.00};

// the product numbers
int   prodNum [numItems] = {7,      8,     9,     10,    11,    12,    13,    14};

// the salespersons IDs
int   salesP  [numSalesP] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// the output file pointers
FILE * filePtrs[numSalesP];

// sales totals for every salespersons
float totals  [numSalesP];

//get the product index from the prodNum array
int getProdIndex (int product) {
    int i;
    for (i=0; i< numItems; i++) {
        if (prodNum[i] == product) {
            return i;
        }
    }
    return -1;
}

// get a product price from the product index
float getProdPrice (int prodIndex) {
    return prices[prodIndex];
}

// open a salesperson output file
void openSalesPFiles () {
    int i;
    char fileName[16];;

    for (i=0; i<numSalesP; i++) {
        sprintf(fileName, "salespers%d.dat", i+1);
//DEBUG         cout << fileName << endl;
        filePtrs[i] = fopen(fileName, "r");
    }
}

// close Salespersons files
void closeSalesPFiles () {
    int i;
    for (i=0; i<numSalesP; i++) {
        fclose(filePtrs[i]);
    }
}

// get sales person index from its ID
int getSalesPIndex (int salesPerson) {
    int i;
    for (i=0; i< numSalesP; i++) {
        if (salesP[i] == salesPerson) {
            return i;
        }
    }
    return -1;
}



int main () {
    int i;                  // generic counter
    FILE * salesFile;       // the input file with all sales
    int salesPId;           // salesperson ID
    int salesPIndex;        // salesperson index in array
    int prodId;             // product ID
    int pIndex;             // product index in array
    int qty;                // quantity
    float total;            // total for one sale

    // open all salespersons output files
    openSalesPFiles();

    // open the input file
    salesFile = fopen("sales.dat", "r");

    // read all record in the input file
    while (!feof(salesFile)) {

        fscanf(salesFile, "%d %d %d", &salesPId, &prodId, &qty);
//DEBUG        cout << salesPId << " --- " << prodId << " --- " << qty << endl;

        // validate sales person
        salesPIndex = getSalesPIndex (salesPId);
        if (salesPIndex < 0) {
            cout << "Invalid Sales person ID " << salesPId << endl;
            continue;
        }

//DEBUG        cout << "Salesperson index : " << salesPIndex << endl;

        // validate product id
        pIndex = getProdIndex (prodId);
        if (pIndex < 0) {
            cout << "invalid product id : " << prodId << endl;
            fprintf(filePtrs[salesPIndex], "Invalid Product ID %d\n", prodId);
            continue;
        } 
        else {
            // compute the sale total
            total = qty * prices[pIndex];
//DEBUG            cout << "total : " << total << endl;;

            // add it to the totals for this salesperson
            totals[salesPIndex] += (qty * prices[pIndex]);

            // write the sale to the salesperson file
            fprintf(filePtrs[salesPIndex], "%d %d %2.2f\n", prodId, qty, total);
        }
    }

    // print totals in salespersons files
    for (i=0; i< numSalesP; i++) {
        fprintf(filePtrs[i], "Total Sales : %8.2f\n", totals[i]);
    }

    // close all files
    closeSalesPFiles();
    fclose(salesFile);


}

代码有什么问题会让我出现这样的错误?谢谢:S

最佳答案

断言来自名为feoferr.c的文件。这表明它与 feof 函数有关。该断言表示它期望 stream != NULL。断言失败,因此 stream 显然是一个空指针。由于 feof 采用文件流参数,因此可以安全地猜测断言消息提到的流就是文件流参数。您可以这样调用 feof:

// open the input file
salesFile = fopen("sales.dat", "r");

// read all record in the input file
while (!feof(salesFile)) {

所以也许 salesFile 是一个空指针。如您所知,当 fopen 无法打开文件时可能会发生这种情况。也许该文件不存在,或者您没有读取它的权限。

<小时/>

下次遇到错误时,请使用您面前的工具。你有一个调试器,当程序像这样失败时,它应该中断你的程序。它可以将您带到失败的行或最接近它的行。这应该会提示您,在您调用 fopen 后不久,问题就出现在某个地方。在那里开始调查。

稍后在代码中设置断点,并查看是否在程序失败之前到达它们。如果您没有做到这一点,那么当您发布问题时不要包含这些功能。不要用大量不相关的代码来筛选潜在的帮助者,以解决真正的问题。

当您遇到问题时,请确保您的函数返回您期望的值。如果您不知道他们会返回什么,请阅读文档并做一些实验。确保您理解您编写的所有代码。

关于c++ - 调试 C 程序的断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526240/

相关文章:

pointers - 如何在不指定类型的情况下修改函数输入参数?

c# - 差异 b/n 对象、引用、指针

c++ - 字符串相等时比较不相等

c++ - 二进制 '=' : no operator found which takes a right-hand operand of type "Button *"

c++ - 已修复 : C++ server/client program: "Connection refused"

c - 缓冲区溢出;避免溢出攻击

c - C 中的内存分段失败

c++ - 为 matlab R2011b 设置 c++ 编译器

c - 如何编译这个C程序?

C++ 指针加法被乘