读取txt中的数据到数组中
读取txt文件,在知道其列数,但不知道其行数的情况下,读取其数据并将其存放在二维数组中。
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line , word; int row=0; int num=0; ifstream infile( "D:\VC++\readfile\readfile\yhui.txt " ); if ( !infile ) { cout << "Error: can"t open the file !" << endl ; return -1; } while ( getline( infile , line )) { row ++; } infile.clear(); int **IVdata; IVdata = new int *[row]; for ( int i =0; i<row; i++) { IVdata[i]= new int [2]; } infile.seekg(ios::beg); if (infile.eof() ) { cout<<"end of file! " ; } while (infile >> i) { IVdata[(int)(num / 2)][num % 2]= i; num++; } for (i=0;i<row;i++) { for (int j=0;j<2;j++) { cout << IVdata[i][j] << " "; } cout << endl; } infile.close(); return 0; }
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: 如何动态分配二、三维数组