gpt4 book ai didi

c++ - VC++ 上的访问冲突错误

转载 作者:行者123 更新时间:2023-11-28 00:58:55 25 4
gpt4 key购买 nike

为什么我在以下程序中收到“访问冲突错误读取”:

错误是在读取文件的while循环上。

#include <iostream>

class fileReader
{
public:
FILE *fp;
char** lines;
fileReader()
{
fp = NULL;
}
fileReader(const char* path)
{
int i=0;
fp = fopen(path,"r");
while ( fgets(lines[i], 100, fp) )
i++;
}
};

int main(int argv, char** argc)
{
const char* path = "D:\\PS4263-2.txt";
fileReader *p = new fileReader(path);
for (int i=0; i<2; i++)
std::cout<<p->lines[i];
return 0;
}

编辑

如答案所述,我将代码更改为(如下),但我仍然遇到相同的错误。

#include <iostream>

class fileReader
{
public:
FILE *fp;
char** lines;
fileReader()
{
fp = NULL;
}
fileReader(char* path)
{
int j=0;
fp = fopen(path,"r");
if (fp == NULL)
return;
else
{
lines = (char**) malloc(sizeof(char *)*56000);
for (int i=0; i<56000; i++)
lines[i] = (char*)malloc(sizeof(char)*1440);
while ( fgets(lines[j], 1440, fp) )
j++;
fclose(fp);
}
}
};

int main(int argv, char** argc)
{
char* path = "D:\\testfile.txt";
fileReader *p = new fileReader(path);
for (int i=0; i<2; i++)
std::cout<<p->lines[i];
return 0;
}

最佳答案

这段代码有很多问题。但主要是,问题在于您正在编写一些邪恶的 C/C++ 混合体。选择两种语言中的一种,然后使用它。

这是您的代码的修订版:

#include <iostream>

class fileReader
{
public:
FILE *fp;
char** lines;
fileReader() : fp(NULL) // initialization of members happens here
{
//fp = NULL; // anything here happens *after* initialization
lines = new char*[100]; // let's just assume max 100 lines. We have to allocate space for them
for (int i = 0; i < 100; ++i) {
lines[i] = new char[100]; // allocate space for the contents of each individual line
}
}
fileReader(const char* path)
{
lines = new char*[100]; // let's just assume max 100 lines. We have to allocate space for them
for (int i = 0; i < 100; ++i) {
lines[i] = new char[100]; // allocate space for the contents of each individual line
}

int i=0;
fp = fopen(path,"r");
while ( fgets(lines[i], 100, fp) )
i++;
}
~fileReader() {
// deallocate and close our members:
fclose(fp);
for (int i = 0; i < 100; ++i) {
delete[] lines[i]; // delete the contents of each line
}
delete[] lines; // delete the lines array
}
};

int main(int argv, char** argc)
{
const char* path = "D:\\PS4263-2.txt";
fileReader p(path); // don't use new unless you really really have to
for (int i=0; i<2; i++)
std::cout<<p.lines[i];
return 0;
}

现在至少它可以工作了,如果每行包含少于 100 个字符并且有少于 100 行并且文件存在并且我们真正应该防范的其他十几种情况。特别是,我们在内存管理上花费了大量精力:为所有行数据分配和释放空间。

但是,如果我们真的开始编写 C++,我们只需做一些更改就可以做得更好。

#include <iostream> // we need this for the standard streams (cout)
#include <fstream> // we need proper C++ file streams too
#include <string> // C++ has strings. Don't waste your time on char pointers
#include <vector> // C++ has a dynamic array class. Don't use pointers as ad-hoc arrays

class fileReader
{
public:
// FILE* fp; // no point in making this a class member, when it's only used in one function
std::vector<std::string> lines; // use a vector of strings. Much easier to manage
fileReader() // vectors are automatically initialized, no need to do anything
{
}
fileReader(std::string path)
{
std::ifstream fp(path); // create an input file stream
std::string result; // store the contents of the current line here
while (std::getline(fp, result)) {
lines.push_back(result); // append the resulting line to the end of the vector
}
}
};

int main(int argv, char** argc)
{
std::string path = "blah.txt";
fileReader p(path); // don't use new unless you absolutely have to
for (int i=0; i<2; i++)
std::cout<<p.lines[i];
return 0;
}

请注意,我们不再需要管理数组内存。当 vector 和字符串超出范围时,它们会自动清理。而且因为我们不再使用 new 来分配 fileReader,所以当 超出范围时它会自动被删除。这有效地启动了一个链式 react ,其成员开始自行清理:文件流关闭, vector 在要求其存储的字符串清理并关闭后释放其内存。整个程序折叠并关闭,我们无需编写一行代码来处理它。

关于c++ - VC++ 上的访问冲突错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9766984/

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