To read this article, first refer to:
input, output, and documentation for C + + learning notes
Test data:
1 /*reading a TXT file to a two-dimensional array*/2#include <iostream>3#include <fstream>4#include <vector>5#include <string>6 7 using namespacestd;8 9typedef vector< vector<int> > D2array;//two-dimensional arraysTentypedef vector<int> D1array;//one-dimensional arrays One A - intMain () - { the introw =Ten; - intCol =Ten; -Ifstream input ("e:\\c++\\c++ code\\item_basedcf\\datafordebug.txt");//Open Input File -Ofstream Output ("e:\\c++\\c++ code\\item_basedcf\\mytext.txt");//Open the file you want to write to, and if the file does not exist, automatically create a new +D2array out(Row, D1array (col,0));//declares a two-dimensional array that writes the read-in data to the array - + if(!input.is_open ())//if the file fails to open A { atcout <<"File is not existing!"<<Endl; -Exit1); - } - - for(inti =0; i < row; i++) - { in for(intj =0; J < Col; J + +) - { toInput >> outI [j];//reads characters from input stream object inputs to out +cout << outI [j] <<" "; -Output << outI [j] <<" ";//The character is stored in output stream object outputs because the output stream is already associated with the mytext.txt, so it is written to the file the } *cout << Endl;//line break, otherwise displayed in one line $Output << Endl;//Wrap, otherwise the text is all in one linePanax Notoginseng } - the + input.close (); A output.close (); the +System"PAUSE"); - return 0; $}
Run Result: Successfully read in out, and dump to Mytext.txt
Interested readers can be changed to a function, the use of direct call can be, the program can not read the place to leave a message!