gpt4 book ai didi

c++ - 从同一个文件读取和写入?

转载 作者:行者123 更新时间:2023-11-28 08:34:44 30 4
gpt4 key购买 nike

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

if (titlemap.count(words[i]) == 1)

到达我重新打开文件并读取 vector 中的所有数据然后存储更新的数据但是但实际上程序并没有进入下面的循环。

for (int j = 0; j < vec.size(); j++) Can anyone suggest why is it so? I am very confused and frustrated


// 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;
}

最佳答案

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

只需添加:

wfile.flush()

在打开以供读取之前,确保缓冲区被刷新到磁盘并可供备用句柄使用。

关于c++ - 从同一个文件读取和写入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59419346/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com