c++将txt文件读取并写入二维数组

如何使用c++读取txt文件中n行3列数据,并写入到一个二维数组中。。。。求大神指教

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
 
using namespace std;
 
int main ()
{
    string PATH("");
    int row = 0,col = 0;
    printf("input path:\n");
    getline(cin,PATH);
    printf("input rows and columns\n");
    scanf("%d%d",&row,&col);
 
    if(row<0||col<0)
    {
        printf("wrong size!\n");
        return -1;
    }
 
    FILE*fp = fopen(PATH.c_str(),"r");
    if(fp==NULL)
    {
        printf("wrong path\n");
        return -1;
    }
 
    float array[row][col];
    for(int i=0; i <row; ++ i)
        for(int j=0; j <col; ++ j)
        {
            fscanf(fp,"%f",&array[i][j]);
        }
 
    fclose(fp);
    for(int i=0; i <row; ++ i)
    {
        for(int j=0; j <col; ++ j)
        {
            printf("%f ",array[i][j]);
        }
        printf("\n");
    }
 
    return 0;
}

追问

你好,我刚用你的代码试了下,提示input path,请问这里的input path我需要如何输入才能读取预先已有的TXT文档中的内容呢(内容为n行3列的数据),十分感谢您!!!

温馨提示:答案为网友推荐,仅供参考