プロジェクトでは、テキスト ファイルの列を異なる配列で読み取りたいと考えています。まず、ファイルを 1 つの 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());
}
}