从同一文件读取和写入?

时间:2019-12-20 03:00:04

标签: c++ file file-handling ifstream ofstream

我正在文件中存储一些数据,但是之后

  

如果(titlemap.count(words [i])== 1)

达到

我重新打开文件并读取矢量中的所有数据,然后存储更新的数据,但是 但是实际上该程序不会进入以下循环。

  

for(int j = 0; j


// Cmarkup1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include"Markup.h"
#include <msxml.h>
#include <ctime>
#include <unordered_map>
#include <cctype>
#include <string>
#include <functional>
#include <algorithm>
#include "functions.h"
#include <map>
#include <fileapi.h>
using namespace std;
int main()
{

    // Open the file for parsing.
    ofstream wfile("title.txt");
    bool check = false;
    string delimiter = " ,:,";

    int results = 0, pages = 1;
    time_t timer;
    timer = clock();
    CMarkup xmlfile;

    unordered_map<string, string> titlemap;
    unordered_map<string, string> textmap;
    vector <string> words;

    xmlfile.Load(MCD_T("simplewiki-latest-pages-articles.xml"));
    xmlfile.FindElem();
    xmlfile.IntoElem();
    int line=0;

    while (xmlfile.FindElem(MCD_T("page"))) {

        xmlfile.IntoElem();
        xmlfile.FindElem(MCD_T("title"));
        MCD_STR(title);
        title = xmlfile.GetData();
        string str(title.begin(), title.end());
        transform(str.begin(), str.end(), str.begin(), ::tolower);
        split(words, str, is_any_of(delimiter));

        for (int i = 0; i < words.size(); i++) {
            if (titlemap.count(words[i]) == 1) {
                ifstream rfile;
                rfile.open("title.txt");
                vector<string> vec;
                string line;
                while (getline(rfile, line)) {
                    vec.push_back(line);
                }
                for (int j = 0; j < vec.size(); j++) {
                    if (words[i] == vec[j]) {
                        cout << vec[j] <<"Checking"<< endl;
                        wfile << vec[j] << ",page" << pages << endl;
                    }
                    else
                        wfile << vec[j] << endl;
                    //wfile.close();
                }
            }
            else {
                //wfile.open("title.txt");
                keeponlyalphabets(words[i]);
            titlemap.insert(make_pair(words[i], words[i]));
            wfile << words[i] <<"-page"<<pages<< endl;
            ++line;
            }
        }
            words.clear();
            //cout << str << endl;
            //xmlfile.FindElem(MCD_T("text"));
            //MCD_STR(text);
            //text = xmlfile.GetData();
            //string str1(text.begin(), text.end());
            //transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
            //str1 = keeponlyalphabets(str1);
            //removestopwords(str1);
            //textmap.insert(make_pair(str1, str1));
            //cout << str1 << endl;
            if (pages > 100)
                break;
            pages++;
            xmlfile.OutOfElem();


        }
    wfile.close();

    //  for (auto it : titlemap)
        //  cout << it.first << endl;
        cout << "Total lines are as:  "<<line << endl;
        /*string input;
        cout << "press s to seach the data" << endl;
        getline(cin, input);
        if (input == "s") {

            string key;
            cout << "Enter Key" << endl;
            cin >> key;
            transform(key.begin(), key.end(), key.begin(), ::tolower);
            size_t temp;
            cout << endl;
            for (auto it = data.begin(); it != data.end(); it++) {
                //temp = it->first.find(key);
                //cout << temp;
                if (it->first.find(key) != std::string::npos) {
                    cout << it->second << endl;
                    results++;
                }
            }

        }
        else
            cout << "Invalid Character Exiting....." << endl;
        timer = clock() - timer;
        cout << "Total time taken by the process is:  " << (float)timer / CLOCKS_PER_SEC << endl;
        cout << " Total Results : " << results << endl;
        */
        return 0;
    }

1 个答案:

答案 0 :(得分:0)

针对该文件打开了单独的流,每个流上都有单独的缓冲区write仅实际上很少偶尔写入磁盘(通常是在缓冲区已满时,对于较小的写入可能要花一些时间,并且总是在关闭文件之前)。因此,当您重新打开文件进行读取时,它不会看到任何残留在用户空间缓冲区中的东西。

只需添加:

wfile.flush()

在打开以进行读取之前,请确保将缓冲区刷新到磁盘上并供备用句柄使用。