对于一个项目,我想读取不同数组中的文本文件的列。首先,我在一个 2D 字符串数组中读取文件,并将该数组拆分为不同的int
或float
1D 数组。但是当我使用or方法将数字转换为int
of时,我遇到了分段错误。有人有其他解决方案吗?float
atof
atoi
void Tftdiag::readFile(std::string file)
{
string testline;
char tab2[1024];
strcpy(tab2, file.c_str());
ifstream Test( tab2 );
if (!Test)
{
cout << "There was an error opening the file.\n";
}
//store words in array
int x=0,y=0;
while( Test>>testline )
{
word[y][x]=testline;
x++;
if (testline=="")
y++;
}
//output whole array with array position numbers for each entry
cout<<"Array contents:\n";
for (int y=0;y<50;y++)
{
for (int x=0;x<6;x++)
cout<<word[y][x]<<"("<<y<<","<<x<<")"<<endl;
}
for(int i=0; i<50; i++) {
voltage[i]= atof("0.5".c_str());
//currentArray[i]= atof(word[50][1].c_str());
//lux[i]= ::atof(word[50][2].c_str());
//red[i]= atoi(word[50][3].c_str());
//green[i]= atoi(word[50][3].c_str());
//blue[i]= atoi(word[50][3].c_str());
}
}