C + + Learning Note The text file reads the data into a two-dimensional array created by the vector template and is stored as a new text file

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.