在我编写的处理 csv 文件的相当基本的 C++ 程序中寻找错误
Looking for an error in a fairly basic C++ program I've written which deals with csv files
似乎无法弄清楚为什么这个程序不能运行。它应该将来自 csv 文件的数据存储到一个名为 SurnameInfo 的结构中(当与循环遍历每一行的循环一起使用时)但是每当我 运行 它到达 csv 文件的 151671 的第 1280 行时,崩溃,并给出 windows "program.exe has stopped working" 弹出窗口。有人看到任何可能导致这种情况的事情吗?谢谢!!
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAXLINE = 1000;
const int MAXARRAY = 1000;
int numberOfNames;
struct SurnameInfo
{
char *name;
int count;
float pctrace[6];
};
SurnameInfo*surnames[MAXARRAY];
void processLine(char *line, int n)
{
surnames[n] = new SurnameInfo; //allocate memory
char * pch = strtok(line, ",");//start tokenizing
int len = strlen(pch); // name length
surnames[n]->name = new char[len+1]; //allocate memory
strcpy(surnames[n]->name, pch); // copy name
surnames[n]->count = atoi(strtok(NULL, ","));//get count
for (int i = 0; i < 6; i++)
{
pch = strtok(NULL, ",");
surnames[n]->pctrace[i] = pch[0] == '(' ? -1 : atof(pch);
}
}
void readLines()
{
char line[MAXLINE];
ifstream inputfile;
inputfile.open("names.csv");
if (!inputfile) return; // can't open
inputfile.getline(line, MAXLINE); //skip title
inputfile.getline(line, MAXLINE);
numberOfNames = 0;
while (!inputfile.eof()) //not end of file
{
processLine(line, numberOfNames++);
inputfile.getline(line, MAXLINE);
}
inputfile.close();
}
int main() {
readLines();
return 0;
}
我发现代码与您所说的内容存在差异。
const int MAXARRAY = 1000;
&& SurnameInfo*surnames[MAXARRAY];
违背 151671 of the csv file
.
您正在分配 1000 并试图在无人看管的情况下将更多内容推送到堆,这意味着它开始消耗分配给程序本身的内存。或者它试图访问它不应该访问的区域(可能是分配了其他进程的程序区域),因此推出 Segmentation Fault
此外,您需要有一种方法来破坏动态馈送的 Surnames
。
我的建议:
Approach 1 :
先通读文件,得到行数。将相应的内存分配给 Surnames
并按原样进行。
虽然它需要额外扫描一次文件,但可以解决您的目的。如果文件大小很大,时间复杂度会非常高。(也许你可以在阅读时缓存东西,使用向量??(考虑一下))
Approach 2 :
实施类似于调整 vector.On 大小的功能,每次向 Surnames
添加新内容时,释放之前在堆上分配的内存,并通过深度复制以更高的内存规格重新分配并插入新信息。
此外,
surnames[n]->pctrace[i] = pch[0] == '(' ? -1 : atof(pch);
我不太确定这是否能正常工作。为了安全和更清晰的代码,将其放在括号中。像这样
surnames[n]->pctrace[i] = ((pch[0] == '(') ? -1 : atof(pch));
如果这是您第一次尝试使用 C++,那么这已经做得很好了。干杯。
希望回答对您有所帮助。
似乎无法弄清楚为什么这个程序不能运行。它应该将来自 csv 文件的数据存储到一个名为 SurnameInfo 的结构中(当与循环遍历每一行的循环一起使用时)但是每当我 运行 它到达 csv 文件的 151671 的第 1280 行时,崩溃,并给出 windows "program.exe has stopped working" 弹出窗口。有人看到任何可能导致这种情况的事情吗?谢谢!!
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAXLINE = 1000;
const int MAXARRAY = 1000;
int numberOfNames;
struct SurnameInfo
{
char *name;
int count;
float pctrace[6];
};
SurnameInfo*surnames[MAXARRAY];
void processLine(char *line, int n)
{
surnames[n] = new SurnameInfo; //allocate memory
char * pch = strtok(line, ",");//start tokenizing
int len = strlen(pch); // name length
surnames[n]->name = new char[len+1]; //allocate memory
strcpy(surnames[n]->name, pch); // copy name
surnames[n]->count = atoi(strtok(NULL, ","));//get count
for (int i = 0; i < 6; i++)
{
pch = strtok(NULL, ",");
surnames[n]->pctrace[i] = pch[0] == '(' ? -1 : atof(pch);
}
}
void readLines()
{
char line[MAXLINE];
ifstream inputfile;
inputfile.open("names.csv");
if (!inputfile) return; // can't open
inputfile.getline(line, MAXLINE); //skip title
inputfile.getline(line, MAXLINE);
numberOfNames = 0;
while (!inputfile.eof()) //not end of file
{
processLine(line, numberOfNames++);
inputfile.getline(line, MAXLINE);
}
inputfile.close();
}
int main() {
readLines();
return 0;
}
我发现代码与您所说的内容存在差异。
const int MAXARRAY = 1000;
&& SurnameInfo*surnames[MAXARRAY];
违背 151671 of the csv file
.
您正在分配 1000 并试图在无人看管的情况下将更多内容推送到堆,这意味着它开始消耗分配给程序本身的内存。或者它试图访问它不应该访问的区域(可能是分配了其他进程的程序区域),因此推出 Segmentation Fault
此外,您需要有一种方法来破坏动态馈送的 Surnames
。
我的建议:
Approach 1 :
先通读文件,得到行数。将相应的内存分配给 Surnames
并按原样进行。
虽然它需要额外扫描一次文件,但可以解决您的目的。如果文件大小很大,时间复杂度会非常高。(也许你可以在阅读时缓存东西,使用向量??(考虑一下))
Approach 2 :
实施类似于调整 vector.On 大小的功能,每次向 Surnames
添加新内容时,释放之前在堆上分配的内存,并通过深度复制以更高的内存规格重新分配并插入新信息。
此外,
surnames[n]->pctrace[i] = pch[0] == '(' ? -1 : atof(pch);
我不太确定这是否能正常工作。为了安全和更清晰的代码,将其放在括号中。像这样
surnames[n]->pctrace[i] = ((pch[0] == '(') ? -1 : atof(pch));
如果这是您第一次尝试使用 C++,那么这已经做得很好了。干杯。
希望回答对您有所帮助。