调用函数时进程终止

Process terminates when function is called

本文关键字:终止 进程 函数 调用      更新时间:2023-10-16

我试图写一个函数,将读取和打印文件的内容。我将文件名作为函数的参数。我使用FILE *testfile创建一个文件句柄,然后使用fread读取该文件。block_t是一个结构体,nreserved是块的保留段。每个块都有记录。我认为没有必要告诉你block_t是如何创建的。

我的问题是,即使函数运行,我可以在控制台中看到我想看到进程终止的结果。即使我注释掉if else部分,也会发生这种情况。我得到这个消息Process terminated with status -1073741510

下面是我的代码:
#include "dbtproj.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
void showEntriesOfBlock(char *filename){
    FILE *testfile;
    block_t block;
    int nreserved;

    //open file and print contents
    testfile = fopen(filename,"r");
    if(testfile==NULL)
        cout << "Error";
    else{
        while(!feof(testfile)){
            fread(&block, 1, sizeof(block_t), testfile);
            nreserved = block.nreserved;
    //print block contents
    for (int i=0; i<nreserved; ++i) {
        printf("this is block id: %d, record id: %d, num: %d, str: %sn",
                block.blockid, block.entries[i].recid, block.entries[i].num,
                block.entries[i].str);
        }
    }
}
fclose(testfile);
};

在我的主文件中,我使用outfile = fopen("file.bin", "w");创建了一个文件,然后我将随机数据写入该文件。然后我用fclose(outfile);关闭文件在下一行我调用我的函数showEntriesOfBlock("file.bin");

有人能帮忙吗?我认为我可能搞砸了我的指针做了一些错误的文件处理程序。

这就是我给数据块和记录的方式。

for (int b=0; b<nblocks; ++b) { // for each block
    block.blockid = b;
    for (int r=0; r<MAX_RECORDS_PER_BLOCK; ++r) { // for each record
        // prepare a record
        record.recid = recid++;
        record.num = rand() % 1000;
        strcpy(record.str,"hello");   // put the same string to all records
        record.valid = true;
        memcpy(&block.entries[r], &record, sizeof(record_t)); // copy record to block
    }
    block.nreserved = MAX_RECORDS_PER_BLOCK;
    block.valid = true;
    fwrite(&block, 1, sizeof(block_t), outfile);    // write the block to the file
}
fclose(outfile);

下面是我的结构体的定义:

// This is the definition of a record of the input file. Contains three fields, recid,           num and str
typedef struct {
    unsigned int recid;
    unsigned int num;
    char str[STR_LENGTH];
    bool valid;  // if set, then this record is valid
 } record_t;

// This is the definition of a block, which contains a number of fixed-sized records
typedef struct {
    unsigned int blockid;
    unsigned int nreserved; // how many reserved entries
    record_t entries[MAX_RECORDS_PER_BLOCK]; // array of records
    bool valid;  // if set, then this block is valid
    unsigned char misc;
    unsigned int next_blockid;
    unsigned int dummy;
} block_t;

这是一个使用FILE*的工作版本(如果你正在学习…,我不建议使用它)

注意:以二进制模式打开文件:fopen(filename, "wb")或fopen(filename, "rb")

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <fstream>
const int STR_LENGTH = 10;
const int MAX_RECORDS_PER_BLOCK = 5;
//! For my test I assumed the following definitions.
//! (i.e. that block_t is a POD.)
// This is the definition of a record of the input file. Contains three fields, recid,           num and str
typedef struct
{
    unsigned int recid;
    unsigned int num;
    char str[STR_LENGTH];
    bool valid;  // if set, then this record is valid
} record_t;

// This is the definition of a block, which contains a number of fixed-sized records
typedef struct 
{
    unsigned int blockid;
    unsigned int nreserved; // how many reserved entries
    record_t entries[MAX_RECORDS_PER_BLOCK]; // array of records
    bool valid;  // if set, then this block is valid
    unsigned char misc;
    unsigned int next_blockid;
    unsigned int dummy;
} block_t;
void showEntriesOfBlock(const char *filename)
{
    FILE* testfile = fopen(filename, "rb");
    assert(testfile);
    if (!testfile)
    {
        perror("Error");
        return;
    }
    block_t block;                
    while(fread(reinterpret_cast<char*>(&block), sizeof(block_t), 1, testfile))
    {
        if (ferror(testfile))
        {
            perror("Error while reading");
            return;
        }
        //print block contents
        for (int i = 0; i < block.nreserved; ++i)
        {
            printf("this is block id: %d, record id: %d, num: %d, str: %sn",
                block.blockid, block.entries[i].recid, block.entries[i].num,
                block.entries[i].str);
        }
    }
    fclose(testfile);
};
int main(int argc, const char *argv[])
{
    std::string filename = "g:/test.dat";
    FILE* outfile;
    outfile = fopen(filename.c_str(), "wb");
    int nblocks = 10;
    int recid = 0;
    for (int b = 0; b < nblocks; ++b)
    {
        block_t block;
        block.blockid = b;
        for (int r = 0; r < MAX_RECORDS_PER_BLOCK; ++r)
        {
            // for each record
            // prepare a record
            record_t record;
            record.recid = recid++;
            record.num = rand() % 1000;
            strcpy(record.str, "hello");   // put the same string to all records
            record.valid = true;
            memcpy(&block.entries[r], &record, sizeof(record_t)); // copy record to block
        }
        block.nreserved = MAX_RECORDS_PER_BLOCK;
        block.valid = true;
        fwrite(&block, sizeof(block_t), 1, outfile);    // write the block to the file
    }
    fclose(outfile);
    showEntriesOfBlock(filename.c_str());
    return 0;
}

试试这个:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <fstream>
#include <type_traits>
void showEntriesOfBlock(char *filename)
{
    std::ifstream testfile(filename, std::ios_base::binary);
    assert(testfile);
    if (!testfile)
    {
        std::cout << "Error";
        return;
    }
    block_t block;
    int nreserved;
    while (testfile)
    {
        //! This assumes block is a POD.
        static_assert(std::is_pod<block_t>::value, "block_t is not a POD.");
        testfile.read(reinterpret_cast<char*>(&block), sizeof(block_t));
        nreserved = block.nreserved;
        //print block contents
        for (int i = 0; i < nreserved; ++i) 
        {
            printf("this is block id: %d, record id: %d, num: %d, str: %sn",
                    block.blockid, block.entries[i].recid, block.entries[i].num,
                    block.entries[i].str);
        }        
    }
    testfile.close();
};