调用函数时进程终止
Process terminates when function is called
我试图写一个函数,将读取和打印文件的内容。我将文件名作为函数的参数。我使用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();
};
相关文章:
- 运行代码时,c++ 会终止进程
- CLion 如何终止进程运行?
- 使用进程 ID 终止进程
- 有没有办法通过过滤进程的名称来自动终止进程?
- Windows资源监视器使用哪些Win32函数来检测已终止进程的磁盘和网络活动
- 终止进程并关闭其所有打开的侦听端口
- 如何释放被终止进程锁定的端口
- 使用进程名称终止进程
- 如何检测终止进程事件、计算机关闭和计算机崩溃
- 我无法使用终止进程杀死子进程
- 使用终止进程终止"mstsc.exe"进程时出现错误代码(5)访问被拒绝
- 在跨平台C++启动和终止进程
- 使用SIGSEGV或SIGABRT信号转储核心并终止进程
- 如何使用管理权限调用终止进程
- QT中的运行和终止进程C++通过按下按钮
- 使用SIGHUP重新启动终止进程
- dbus-c++中的断言错误,终止进程
- 如果在堆上定义,QProcess 不会终止/终止进程
- WinAPI - windows关闭前的清理终止进程
- 从VS10应用程序中查找/终止进程(VC6应用程序)