C ++ string application: extract unique elements from all files in batches

Source: Internet
Author: User
Tags strfind

I am very grateful to anyone who has time to grade and teach me code standards and ideas.

Requirements:

There are more than 600 HTM files in the Directory D: \ Wamp \ www \ om \ products of Gavin. Now you need to <title> ..... </title> extract to a text file. The text file is divided into two columns. The first column is the absolute path of the file, and the second column is the corresponding <title> ..... </title>, separated by semicolons.

It is known that a filelist.txt file under D: \ Wamp \ www \ om \ products stores all files in the current folder.

Solution: Write a file name using cve-tain.txt, read a file name from filelist.txt, open the file, and extract <title> ..... </title>, the absolute path of the file and <title> ..... </title> write to D: \ Wamp \ www \ om \ products \ mapfile.txt.

The source code is as follows:

// Extradocelem. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> # include <fstream> # include <string> # include <windows. h> # include <assert. h> using namespace STD; // search for characters in a file and return the string getstrfromfile (string & filename, string & Str) {string strline; ifstream fin (filename. c_str (); If (! Fin) {cout <"error opening the file" <FILENAME <"for input" <Endl; exit (-1) ;}string: size_type err; while (Getline (FIN, strline) {// cout <strline. c_str () <Endl; err = strline. find (STR); If (ERR = string: NPOs) {// cout <"No title in this file" <Endl ;} else {// cout <strline. c_str () <Endl; // cout <err <Endl; break;} fin. close (); Return strline;} int _ tmain (INT argc, _ tchar * argv []) {string filelist = "d :\\ Wamp \ www \ om \ Products \ filelist.txt "; // string mapfilename =" D: \ Wamp \ www \ om \ products \ mapfile.txt "; // string strdir, the absolute path of the file to store the ing; // string strpath = "D: \ Wamp \ www \ om \ products \\"; string strfind = "<title>"; string strtitle; // 1. each time a row is read to a string ifstream fin (filelist. c_str (); If (! Fin) {cout <"error opening the file" <filelist <"for input" <Endl; exit (-1);} ofstream out_file (mapfilename. c_str (), IOS: APP); While (Getline (FIN, strdir) {strdir = strpath + strdir; strtitle = getstrfromfile (strdir, strfind); cout <strdir. c_str () <Endl; out_file <strdir; out_file <";"; out_file <strtitle; out_file <"\ n";} out_file.close (); Fin. close (); Return 0 ;}

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.