My text file is like
我的文本文件就像
Alex Garcia 1000 userid password
Sana Lopez 300 uid pwd
I am trying to save above text file in 2D array
我想在2D数组中保存上面的文本文件
ifstream Records("customerdata.txt");
string dataarray[6][6];
if (Records.is_open())
{
while ( Records.good() )
{
for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
getline(Records,dataarray[i][j],' ');
}
}
}
Records.close();
}
else cout << "Unable to open file";
When I try to output array using for loop I get some values missing. I dont know what I am doing wrong.
当我尝试使用for循环输出数组时,我得到一些值丢失。我不知道我做错了什么。
1 个解决方案
#1
0
You need to tokenize each of the lines you read in using something like this, plus your datarray is larger than the number of records read in, you only have two rows not 6.
你需要使用这样的东西来标记你读入的每一行,加上你的数据阵列大于读入的记录数,你只有两行而不是6行。
ifstream Records("customerdata.txt");
string dataarray[6][6];
if (Records.is_open())
{
while ( Records.good() )
{
size_t row=0;
size_t col=0;
std::string myLine;
getline(Records,myLine);
std::istringstream nlineSteam(myLine);
std::string token;
while(nlineSteam >> token){
dataarray[row][col]=token;
col++;
}
row++;
col=0;
}
Records.close();
}
else cout << "Unable to open file";
#1
0
You need to tokenize each of the lines you read in using something like this, plus your datarray is larger than the number of records read in, you only have two rows not 6.
你需要使用这样的东西来标记你读入的每一行,加上你的数据阵列大于读入的记录数,你只有两行而不是6行。
ifstream Records("customerdata.txt");
string dataarray[6][6];
if (Records.is_open())
{
while ( Records.good() )
{
size_t row=0;
size_t col=0;
std::string myLine;
getline(Records,myLine);
std::istringstream nlineSteam(myLine);
std::string token;
while(nlineSteam >> token){
dataarray[row][col]=token;
col++;
}
row++;
col=0;
}
Records.close();
}
else cout << "Unable to open file";