如何将数据从文本文件读取到2D动态数组中

问题描述

我只是想问一下如何从文本文件中读取数据,直接将其读取到动态分配的2D数组中,然后打印该数据。

这是我尝试过的方法,但是我不确定自己缺少什么或哪里出了问题;非常感谢您的帮助。

inFile.open("data.txt");
if(!inFile) //if its not open
{
    cout << "unable to open file." << endl;
}

 //declaring 2D array
for(int r = 0; r<numLines; r++)
{
    for(int c = 0; c<numLines; c++)
    {
        myArray[r] = new string[numLines];
    }
}

//counting the number of lines in the file
stringstream ss(line);
while(getline(inFile,line,','))
{
    for(int r = 0; r < numLines; r++)
    {
        for(int c = 0; c<numLines; c++)
        {
            myArray[r][c]=line;
        }
    }
    numLines++;
}
cout << endl;

//deleting the array
for(int r=0; r<arraySize; r++)
{
    for(int co=0; co<arraySize; co++)
    {
        delete [] myArray[r];
    }
    delete [] myArray;
}

//closing the file
inFile.close();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)