[C/C ++ standard library] _ [elementary] _ [Use fstream to merge text files] And fstream text files

Source: Internet
Author: User

[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.

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.