Learn to read a document step-by
1 //readFile.cpp: Defines the entry point of the console application. 2 3 4#include"stdafx.h"5#include <iostream>6#include <fstream>7#include <string>8 using namespacestd;9 Ten //extension: File copy One voidFileCopy (stringFile1,stringfile2) { AIfstreaminch(file1); -Ofstream out(file2); - if(inch){ the stringLine ; - while(Getline (inch, line)) { -cout << Line <<Endl; - out<< Line <<Endl; + } - } + Else{ Acout <<"File not Exist"<<Endl; at } - } - int_tmain (intARGC, _tchar*argv[]) - { - //1. Progressive reading of TXT documents - //Ifstream in ("E:\\workspace\\cpp\\readfile\\config.txt"); in //string Line; - //While (Getline (in,line)) {//read in data row by line and save the data in lines to //cout << line << Endl; + //} - the //2. Read a file and write the contents of the file to another file * //string filePath = "e:\\workspace\\cpp\\readfile\\";//file path, absolute path here $ //Ifstream in (FilePath + "Config.txt"); Panax Notoginseng //Ofstream Out (FilePath + "Result.txt"); - //string Line; the //if (in) { + //While (Getline (in,line)) { A //cout << line << Endl; the //Out << line << Endl;//writes the content read from the config file to the result file + // } - //} $ //else{ $ //cout << "File not Exist" << Endl; - //} - the //3. Call the FileCopy method - stringFilePath ="e:\\workspace\\cpp\\readfile\\";Wuyi stringFile1 = FilePath +"Config.txt"; the stringFile2 = FilePath +"Result.txt"; - fileCopy (file1,file2); Wu - return 0; About}
C + + reads txt text content and saves the results to new text