简单处理文本文件(用于记录密码用)

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <map>

using namespace std;


vector<string> split_string(const string &in, char del, bool skip_empty) {
    vector<string> res;

    if (in.empty() || del == '')
        return res;


    string field;
    istringstream f(in);
    if (del == '
') {
        while(getline(f, field)) {
            if (field.empty() && skip_empty)
                continue;
            res.push_back(field);
        }
    } else {
        while(getline(f, field, del)) {
            if (field.empty() && skip_empty)
                continue;
            res.push_back(field);
        }
    }
    return res;
}


// defined head 
         typedef pair<string,string> id;
    //typedef pair<string, id> db_t;
     map<string, id> db_t;
     typedef map<string, id> dt_tt;
     //vector<dt_tt> db_type;

//end
vector<dt_tt> read_db(string filename) {


    //ifstream db("E:\learning_c++\sndnvaps_user_db\usr_db.txt");
    ifstream db(filename.c_str());
    vector<string> s_v;
    vector<dt_tt> db_type_t;
    
    string line;
    //char del = '	';
    while(getline(db, line)) {
        s_v = split_string(line, '	', false);

    if (!s_v.empty()) {
        dt_tt tmp;
        tmp.insert(pair<string,id>(s_v.at(0), pair<string,string>(s_v.at(1),s_v.at(2))));
            db_type_t.push_back(tmp);

    } else {
        cout << "s_v is empty" <<endl;
     }
    }
    db.close();

    return db_type_t;
}

void print_db(vector<dt_tt> dt) {

    if (!dt.empty()) {
        vector<dt_tt>::iterator it;
        map<string, id>::iterator m_it;
        map<string, id> m_id;
        for (it = dt.begin(); it != dt.end(); it++) {

        m_id = *it;
        for (m_it = m_id.begin(); m_it != m_id.end(); m_it++) 
            cout << m_it->first <<  "
" << m_it->second.first <<  "
"<< m_it->second.second << "
"  << endl;
        }
        }
}


int add_info_to_db(string filename, string url, string id, string pwd) {

    ofstream db(filename.c_str(),ios::out|ios::app);

    db << url << "	" << id << "	" << pwd << "	" << "
";
    db.close();

    return 0;
}

int modify_line(string filename, string ori, string dest ) {

    fstream db(filename.c_str(), ios::in | ios::out);
    char str[256];
    int line = 0;
    do {

    db.getline(str, 256, '
');
    line++;
    }    while (line < 3);
    int len = strlen(str);
    int p = -1;
    for (int i = 0; i < len; i++) {
        char s0[25] = "";
        memcpy(s0, str+i, ori.length());
        if (!strcmp(s0, ori.c_str()))
            p = i;
    }
    if (p >= 0) {
        db.seekp(p - len - 3, ios::cur);
           if(ori.length() > dest.length()){
               for (int i = dest.length() ; i < ori.length(); i++) {
                   dest += " ";
               }
           }
        db.write(dest.c_str(),dest.length());
    }

    db.close();
    return 0;

}





int main(int argc, char* argv[]) {
    vector<dt_tt> temp;
    string file = "E:\learning_c++\sndnvaps_user_db\usr_db.txt";
    temp = read_db(file);
    print_db(temp);
    /*
    add_info_to_db(file, "github.com", "sndnvaps", "447826");
    temp = read_db(file);
    print_db(temp);
    */

     modify_line(file, "github.com", "baidu.com" );
     temp = read_db(file);
     print_db(temp);


    
    return 0;
}

txt文件内容如下:

www.baidu.com    test    test
www.google.com    test_one    test_one
github.com    sndnvaps    447826    
原文地址:https://www.cnblogs.com/sn-dnv-aps/p/3661596.html