gpt4 book ai didi

c++ - 调用函数时进程终止

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:50 27 4
gpt4 key购买 nike

我正在尝试编写一个函数来读取和打印文件的内容。我将文件名作为函数的参数。我使用 FILE *testfile 创建文件句柄,然后使用 fread 读取文件。 block_t 是一个结构,nreserved 是 block 的保留段。每个 block 都有记录。我认为没有必要告诉您 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: %s\n",
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");

有人可以帮忙吗?我认为我可能弄乱了文件处理程序的错误指示。

这就是我向 block 和记录提供数据的方式。

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: %s\n",
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;
}

关于c++ - 调用函数时进程终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com