[C/C ++ standard library] _ [elementary] _ [Use fstream to merge text files] And fstream text files
Scenario:
1. merge text files, and do not merge the first line from the first 2nd files.
2. added a function to support more than two file mergers.
3. Problem: http://ask.csdn.net/questions/192151
It can only be said to be simple: the basics are not enough. This student may not be a developer, but it is also for poor training.
Test. cpp
# Include <fstream> # include <string> # include <iostream> using namespace std; // The last parameter is the output file. // supports merging of the remaining two files // usage. Command Line: test.exe 1.txt 2.txt output.txt int main (int argc, char const * argv []) {cout <"usage1: test.exe 1.txt 2.txt output.txt "<endl; cout <" usage2: test.exe 1.txt 2.txt... output.txt "<endl; if (argc <= 3) return-1; bool first_line_readed = false; ofstream fout (argv [argc-1], ios: out); for (int I = 1; I <argc-1; ++ I) {ifstream fin (argv [I]); string str; int line_number = 0; while (getline (fin, str) {if (! Line_number) {if (! First_line_readed) {first_line_readed = true; str. append ("\ n"); fout. write (str. c_str (), str. size () ;}} else {str. append ("\ n"); fout. write (str. c_str (), str. size () ;}line_number ++ ;}} cout <"finish... "<argv [argc-1] <endl; return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.