Reading and Writing from same file? - c++

i am storing some data in the file but after
if (titlemap.count(words[i]) == 1)
is reached i reopened the file and reading all data in a vector and then storing updated data but
But actually the program is not going into the below loop.
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;
}

You have separate streams opened against the file, with separate buffers on each. writeing to a file only actually writes to disk infrequently (typically when a buffer fills, which may take a while for small writes, and always just before the file is closed). So when you re-open the file for read, it won't see anything still stuck in user-space buffers.
Just add:
wfile.flush()
prior to opening for read, to ensure the buffers are flushed to disk and available to the alternate handle.

Related

Substitution cipher:Which one?

I'm a beginner and I have a question(somehow silly and stupid :) )...Today I decided to challenge myself and I came around the challenge that wanted me to create a program that ciphers (or encrypts) the message using the substitution cipher method...I solved the challenge by myself but mine is way different than the solution itself...I just want to know which one is better and why? and also is there anything I missed in my own code?
So here is the code I've written:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string secretMessage {};
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
cout << "Enter your secret message: ";
getline(cin, secretMessage);
//Encryption
for(size_t i{0}; i<secretMessage.length(); ++i){
for(size_t j{0}; j<alphabet.length(); ++j){
if (secretMessage.at(i) == alphabet.at(j)){
secretMessage.at(i) = key.at(j);
break;
}
}
}
cout << "Encrypting The Message..." << endl;
cout << "Encrypted Message: " << secretMessage << endl;
//Decryption
for(size_t i{0}; i<secretMessage.length(); ++i){
for(size_t j{0}; j<key.length(); ++j){
if (secretMessage.at(i) == key.at(j)){
secretMessage.at(i) = alphabet.at(j);
break;
}
}
}
cout << "\nDecrypting The Encryption..." << endl;
cout << "Decrypted: " << secretMessage << endl;
return 0;
}
And here is the solution:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string secretMessage {};
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string encryptedMessage {};
string decryptedMessage {};
cout << "Enter your secret message: ";
getline(cin, secretMessage);
cout << "\nEncrypting Message..." << endl;
//Encryption
for(char c:secretMessage){
size_t position = alphabet.find(c);
if (position != string::npos){
char newChar {key.at(position)};
encryptedMessage += newChar;
} else{
encryptedMessage += c;
}
}
cout << "Encrypted Message: " << encryptedMessage << endl;
//Decryption
cout << "\nDecrypting Message..." << endl;
for(char c:encryptedMessage){
size_t position = key.find(c);
if (position != string::npos){
char newChar {alphabet.at(position)};
decryptedMessage += newChar;
} else{
decryptedMessage += c;
}
}
cout << "Decrypted Message: " << decryptedMessage << endl;
return 0;
}
Note:I have also included the decryption part too
I find both code snipets rather complex.
Please have a look at this more easy solution
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
// The encoding alphabet and key
constexpr std::string_view alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _" };
constexpr std::string_view key{ "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba_ " };
// String to encrypt
std::string message{"Hello world"};
// Here we will store the result
std::string result;
std::transform(message.begin(), message.end(), std::back_inserter(result), [&key, &alphabet](const char c)
{ size_t pos{ alphabet.find(c) }; return (pos != std::string::npos) ? key[pos] : '_'; });
// Show result
std::cout << "\nEncrypted: " << result << "\n";
message = result;
result.clear();
std::transform(message.begin(), message.end(), std::back_inserter(result), [&key, &alphabet](const char c)
{ size_t pos{ alphabet.find(c) }; return (pos != std::string::npos) ? key[pos] : '_'; });
// Show result
std::cout << "\nDecrypted: " << result << "\n";
return 0;
}
This is using more modern C++ language elements. Encrypting and decrypting is implemented via one std::transform statement each.
Of course you should never use such encoding in real live, because the key is visible in the exe file.
Anyway, maybe it helps you to have some more ideas . . .

Why does my vector::erase call throw "vector subscript out of range"?

I'm writing a program that saves words from a .txt file in vector words, calculates how many words are there (num_elements) and prints these words randomly to the screen (no duplicates).
It all works fine up until rw.erase line, which just spits out the error "vector subscript out of range".
Why is my erase call throwing "vector subscript out of range"?
#include <iostream>
#include <string>
#include <fstream>
#include <Windows.h>
#include <direct.h>
#include <filesystem>
#include <time.h>
#include <random>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
void path_to_main_dir() {
string path = "C:/Randomizer/";
for (const auto& entry : experimental::filesystem::directory_iterator(path)) {
cout << entry.path() << endl;
}
}
int main() {
path_to_main_dir();
string dateread;
printf("Which file do you want to open? ");
cout << "---------------------------------------------------------------------------------------------------------" << endl;
path_to_main_dir();
cout << endl;
cout << "---------------------------------------------------------------------------------------------------------" << endl;
cout << "User: ";
getline(cin, dateread);
string path_to_file = "C:/Randomizer/" + dateread + ".txt";
ifstream readfile(path_to_file.c_str());
vector<string> words;
string word;
while (getline(readfile, word))
{
words.push_back(word);
}
readfile.close();
srand(time(NULL));
string randomword;
vector<string> rw = { words };
int num_elements = size(words);
cout << endl;
cout << "Number of words in the file: ";
cout << num_elements;
cout << endl;
for (unsigned int a = 0; a < num_elements; a = a + 1)
{
randomword = rw[rand() % num_elements];
cout << randomword << endl;
rw.erase(remove(rw.begin(), rw.end(), randomword), rw.end());
num_elements -= 1;
system("pause");
}
goto firstline;
return 0;
}
If the error only happens in the case where there are duplicates, it could be because the num_elements is wrong. The remove/erase call will have deleted as many duplicates as there are, but num_elements has only been reduced by one.
Fortunatly, vectors know their own size, so rather than trying to remember its internal information for it, you can just ask!
int main()
{
//...
//Code to read words from file
//...
cout << "Number of words in file: " << words.size() << endl;
while(!words.empty())
{
string randomWord = words[rand() % words.size()];
cout << randomWord << endl;
words.erase(remove(words.begin(), words.end(), randomWord), words.end());
}
return 0;
}
From what I can see, you only use words to create rw, so we could just use words directly instead.

string equal doesn't work c++

I have been struggling with comparing two strings which I read from files, "one" & "two" both have the same words (e.g. salt) but it doesn't return "Equal". I have also used == but it made no difference.
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
int main (){
string en[100];
string line;
int i=0;
ifstream fileEn ("hey.txt");
if (fileEn.is_open()){
while (!fileEn.eof()){
getline(fileEn,line);
en[i]=line;
i++;
}
}
fileEn.close();
string fa[100];
string line1;
i=0;
ifstream fileFa ("hey1.txt");
if (fileFa.is_open()){
while (!fileFa.eof()){
getline(fileFa,line1);
fa[i]=line1;
i++;
}
}
fileFa.close();
ifstream Matn("matn.txt");
string matn[100];
if(Matn.is_open()){
for(int i = 0; i < 100; ++i){
Matn >> matn[i];
}
}
Matn.close();
string one = en[0];
string two = matn[0];
cout << one << " " << two;
if(one.compare(two) == 0){
cout << "Equal";
}
}
I suggest adding some debugging output to your program:
while (!fileEn.eof()){
getline(fileEn,line);
// Debugging output
std::cout << "en[" << i << "] = '" << line << "'" << std::endl;
en[i]=line;
i++;
}
and
for(int i = 0; i < 100; ++i){
Matn >> matn[i];
// Debugging output
std::cout << "matn[" << i << "] = '" << matn[i] << "'" << std::endl;
}
Hopefully you can see what the problem is by looking at the output.
In addition, please note that use of while (!fileEn.eof()){ ... } is not correct. See Why is iostream::eof inside a loop condition considered wrong?.
I suggest changing that loop to:
while (getline(fileEn,line)) {
// Debugging output
std::cout << "en[" << i << "] = '" << line << "'" << std::endl;
en[i]=line;
i++;
}
Similarly, don't assume that Matn >> matn[i] is successful. I suggest changing that loop to:
for(int i = 0; i < 100; ++i) {
std::string s;
if ( !(Matn >> s) )
{
// Read was not successful. Stop the loop.
break;
}
matn[i] = s;
// Debugging output
std::cout << "matn[" << i << "] = '" << matn[i] << "'" << std::endl;
}

ifstream::read not working?

I am trying to read from a .csv file. There are two functions below, one for writing and one for reading.
The file contains a simple table:
date,first,second
1 a one
2 b two
3 c three
4 c four
For some reason, the statement while(file_stream.read(&c,1)); does not read anything. It stops at the first character and I'm dumbfounded as to why. Any clues?
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
std::string filename;
std::string line_string;
ifstream file_stream;
stringstream ss;
vector< vector<string> > vec;
char c;
void read_file()
{
filename = "test.csv";
cout << filename << endl;
file_stream.open(filename.c_str(),ios::out|ios::binary);
if(file_stream.fail())
{
cout << "File didn't open" << endl;
return;
}
if(file_stream.is_open())
cout << "file opened" << endl;
while(file_stream.read(&c,1)); // this isn't working
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
file_stream.close();
cout << "string is: " << ss.str() << endl;
//get each line
int counter = 0;
vector<string> invec;
while(getline(ss,line_string,'\n'))
{
string header_string;
stringstream header_stream;
header_stream << line_string;
while(getline(header_stream, header_string,','))
{
invec.push_back(header_string);
}
invec.push_back(header_string);
vec.push_back(invec);
invec.clear();
counter++;
}
}
void test_output()
{
for(int i = 0; i < vec.size();i++)
{
for(int in = 0; in < vec[0].size(); in++)
cout << vec[i][in] << " ";
cout << endl;
}
}
int main()
{
read_file();
test_output();
}
Look very very carefully at the line that is not working:
while(file_stream.read(&c,1)); // this isn't working
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
The ; character at the end of the while statement does NOT belong! You are running a no-body loop that does not terminate until read() fails, and THEN your code enters the bracketed block to output the last character that was successfully read (if any).
You need to remove that erroneous ; character:
while(file_stream.read(&c,1)) // this works
{
cout <<"char c is: " << c;
ss << noskipws << c;
}
Now, the real question is - why are you reading the input file character-by-character into a std::stringstream in the first place? You can use std::getline() with the input std::ifstream directly:
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
std::vector< std::vector<std::string> > vec;
void read_file()
{
std::string filename = "test.csv";
std::cout << filename << std::endl;
std::ifstream file_stream;
file_stream.open(filename.c_str(), ios::binary);
if (!file_stream)
{
std::cout << "File didn't open" << std::endl;
return;
}
std::cout << "file opened" << std::endl;
//get each line
std::vector<std::string> invec;
std::string line;
int counter = 0;
if (std::getline(file_stream, line))
{
std::istringstream iss(line);
while (std::getline(iss, line, ','))
invec.push_back(line);
vec.push_back(invec);
invec.clear();
++counter;
while (std::getline(file_stream, line))
{
iss.str(line);
while (iss >> line)
invec.push_back(line);
vec.push_back(invec);
invec.clear();
++counter;
}
}
}
void test_output()
{
if (!vec.empty())
{
for(int in = 0; in < vec[0].size(); ++in)
std::cout << vec[0][in] << ",";
std::cout << std::endl;
for(int i = 1; i < vec.size(); ++i)
{
for(int in = 0; in < vec[i].size(); ++in)
std::cout << vec[i][in] << " ";
std::cout << std::endl;
}
}
}
int main()
{
read_file();
test_output();
}

Write multimap fo file Fast way c++

I am using the following code to cound the frequency of words :
// Program for Owen written by Briana Morrison
//#pragma warning (disable : 4786)
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
// program assumes that the filename is the only thing passed into program
// if you are using standard argc and argv, then arguments to main should change, and uncomment
// first line.
int main(int argc, char * argv[])
{
string filename(argv[1]);
// string filename;
//cout << "Enter filename" << endl;
//cin >> filename;
ifstream infile(filename.c_str());
//ifstream infile("poe.txt");
string word;
bool debug = false; // for debugging purposes
int count = 0; // count of words for debugging
// create a map of words to frequencies
map<string, int, less<string> > words;
// create a multimap of frequencies to words
multimap<int, string, greater<int> > freq;
// loop while there is input in the file
infile >> word; //priming read
while (infile)
{
count++;
// convert word to lowercase
for (int i = 0; i < word.length(); i++)
if ('A' <= word[i] && word[i] <= 'Z')
word[i] = tolower(word[i]);
if (debug) cout << word << endl;
// if word not found, add to map, otherwise increment count
if (words.find(word) != words.end())
{
words[word]++;
if (debug) cout << word << " found and count incremented to " << words[word] << endl;
}
else
{
words[word] = 1;
if (debug) cout << word << " not found and count incremented to " << words[word] << endl;
}
infile >> word;
}
if (debug) cout << "count is " << count << " and map has " << words.size() << endl;
// now go through map and add everything to multimap...words still in alphabetical order
map<string, int, less<string> >::iterator it = words.begin();
for (it = words.begin(); it != words.end(); it++)
{
pair<int, string> p(it->second, it->first);
freq.insert(p);
}
if (debug) cout << "map has " << words.size() << " and multimap has " << freq.size() << endl;
ofstream outfile("myout.txt");
multimap<int, string, greater<int> >::iterator myit=freq.begin();
for (myit = freq.begin(); myit != freq.end(); myit++)
{
outfile << myit->first << "\t" << myit->second << endl;
}
outfile.close();
return 0;
}
The problem is not in here I think
When i am writing the words to a file, it gets slower by every iteration why?
ofstream outfile("myout.txt");
multimap<int, string, greater<int> >::iterator myit=freq.begin();
for (myit = freq.begin(); myit != freq.end(); myit++)
{
outfil<< myit->first << "\t" << myit->second << endl;
}
outfile.close();
How can I write the multimap in a fast way to a file?
You can use '\n' instead of std::endl to avoid flushing it for every line.
outfil << myit->first << '\t' << myit->second << '\n';
for (myit = freq.begin(); myit != freq.end(); ++myit)
{
outfil<< myit->first << "\t" << myit->second << "\n";
}
this should be faster.
Or you can buffer the data and write them all at once, not line by line.
I don't see why your loop should get slower with every single iteration, but note that you're using formatted output (which is what operator<< does), which is notoriously slow. In case your strings don't contain null bytes you could make your code more efficient by writing the std::string via ostream::write i.e.
outfil << myit->first;
outfil.write( "\t", 1 );
outfil.write( myit->second.c_str(), myit->second.size() );
outfil.write( "\n", 1 );