![](/img/trans.png)
[英]How to convert a matrix of stl vectors to matrix of 2d arrays (or to 1d array)
[英]convert 2d string array to 1D float ,int arrays
對於項目,我想以不同的數組讀取文本文件的列。 首先,我在一個2D字符串數組中讀取文件,然后將此數組拆分為不同的int
或float
1D數組。 但是,當我使用atof
或atoi
方法將數字轉換為float
int
時,出現了分段錯誤。 有人有其他解決方案嗎?
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());
}
}
我已經運行了調試器,並且在索引0處出現錯誤,該索引的值為“ 7,701”,並帶有以下行。
電壓[i] = atof(word [50] [0] .c_str());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.