程序可以写入文本文件,但在尝试回读时崩溃

时间:2018-04-09 20:31:54

标签: c file text

程序运行并且可以成功写入文本文件,但是当我尝试从文本文件中读回时,我得到:

  

"文本文件Homework.exe中的0x0F8C7071(ucrtbased.dll)抛出异常:0xC0000005:访问冲突写入位置0xFFFFFFCC。"

这将我带到stdio.h的第1092行。

应该读取每一行的函数内的代码是:   产品温度;

char name[MAX_STRING_LEN];
char type;
double price;
int qty;

int moreData = fscanf_s(stream, "%s", temp.name, MAX_STRING_LEN - 1);

if (moreData != EOF)
{

    fscanf_s(stream, "%c", temp.type);
    fscanf_s(stream, "%lf", &temp.price);
    fscanf_s(stream, "%d", &temp.qty);

    // echo what just got read (from the file) to the console.
    printf_s("%s %c %.2f %d was read \n\n", temp.name, temp.type, temp.price, temp.qty);
}
else
{
    printf("Read past end of file.\n\n");
}
*pro = temp;
return moreData;

调用该函数的函数是:

FILE *stream;
Product p;
*numProducts = 0;

int err = fopen_s(&stream, fileName, "r");
if (err)
    printf_s("File %s was not opened for reading. \n", fileName);
else {

    printf("\n\nReading Product data from file... \n");

    // Read data back from file:  
    int moreData = readLine(stream, &p);
    while (moreData != EOF) {
        printf("Number of products was %d ", *numProducts);
        reportLine(p);
        list[*numProducts] = p;
        *numProducts = *numProducts + 1;
        printf("It is now incremented to %d \n", *numProducts);
        moreData = readLine(stream, &p);
    }

    fclose(stream);
}

0 个答案:

没有答案