Unknown error in inserting values in multimap - c++

I have the following code, where I try to insert values into a multimap of 2 strings, but I keep getting an error that I cannot understand. I've been trying to solve this for hours.
The whole point of the program is to sort the lines of a dictionary based on the automatic sorting of the multimap insertion.
// sort_entries_of_multiple_dictionaries.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iomanip>
#include <sstream>
// Prototypes
int indexDict(std::multimap<std::string, std::string>& dict);
int main()
{
std::multimap<std::string, std::string> dict;
if(indexDict(dict) == 0)
return 0;
}
int indexDict(std::multimap<std::string, std::string>& dict)
{
std::ifstream inputFile{ "output.txt", std::ios::in };
std::string currentDictEntry{};
size_t currentLine{};
if (!inputFile)
{
std::cerr << "input.txt FILE NOT FOUND in the current directory" << std::endl;
system("pause");
return 0;
}
while (std::getline(inputFile, currentDictEntry))
{
//std::cout << currentDictEntry << std::endl; // TO DELETE
std::string currentWord{};
size_t delimiterPos = currentDictEntry.find('\t', 0);
if (delimiterPos == std::string::npos)
std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl;
else
{
//std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE
for (char& ch : currentDictEntry)
{
if (ch != '\t')
{
currentWord += ch;
}
else
break;
}
std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE
auto value = currentDictEntry.substr(delimiterPos, std::string::npos);
std::cout << "size= " << value.size() << '|' << value << std::endl;
dict.insert( currentWord, currentWord/*, value*/ );
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
The error I keep getting is:
unary '++': '_Iter' does not define this operator or a conversion to a type acceptable to the predefined operator
illegal indirection

as #Evg said, it accepts a std::pair
dict.insert(std::make_pair(currentWord, value));
if I understand your intention correctly, you don't want to save the \t into your result, so add 1 after delimiterPos to get the correct value:
auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos);
test run. output.txt:
4 d
1 a
2 b
3 c
0
output:
"0" - ""
"1" - "a"
"2" - "b"
"3" - "c"
"4" - "d"
full code:
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <iomanip>
#include <sstream>
// Prototypes
int indexDict(std::multimap<std::string, std::string>& dict);
int main()
{
std::multimap<std::string, std::string> dict;
if (indexDict(dict) == 0)
return 0;
for (auto& i : dict) {
std::cout << "\"" << i.first << "\" - \"" << i.second << "\"\n";
}
}
int indexDict(std::multimap<std::string, std::string>& dict)
{
std::ifstream inputFile{ "output.txt", std::ios::in };
std::string currentDictEntry{};
size_t currentLine{};
if (!inputFile)
{
std::cerr << "output.txt FILE NOT FOUND in the current directory" << std::endl;
system("pause");
return 0;
}
while (std::getline(inputFile, currentDictEntry))
{
//std::cout << currentDictEntry << std::endl; // TO DELETE
std::string currentWord{};
size_t delimiterPos = currentDictEntry.find('\t', 0);
if (delimiterPos == std::string::npos)
std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl;
else
{
//std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE
for (char& ch : currentDictEntry)
{
if (ch != '\t')
{
currentWord += ch;
}
else
break;
}
std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE
auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos);
std::cout << "size= " << value.size() << '|' << value << std::endl;
dict.insert(std::make_pair(currentWord, value));
}
if (currentLine == 50) return 0; // TO DELETE
currentLine++;
}
return 1;
}
small mistakes in your code: you don't need <algorithm> and <vector>. also your error message said input.txt instead of output.txt.

I Change dict.insert( currentWord, currentWord/*, value*/ ); To dict.insert({ currentWord,currentWord }); and error Has solved

Related

Finding Top Word Count in .txt file - while loop going extremely slow and not working properly

I'm trying to essentially iterate through every word in the .txt file and when I find a word (from my words map) with more than the maxwordcount variable I add it into the front of the topwords vector
int main(int argc, char** argv) {
fstream txtfile;
string filename = argv[1];
string word, tempword;
int maxwordcount = 0;
int wordcount = 0;
int uniquewordcount = 0;
vector<pair <string, int> > topwords;
map<string, int> words;
if (argc != 2) {
cout << "Incorrect number of arguments on the command line bud" << endl;
}else{
txtfile.open(filename.c_str());
if (txtfile.is_open()) {
while (txtfile >> word){
//removePunctuation(word);
//transform(word.begin(), word.end(), word.begin(), [](unsigned char c){ return::tolower(c); }); //makes string lowercase using iterator
if (words.find(word) == words.end()) {
words[word] = 1; //adds word into the map as a pair starting with a word count of 1
if (words[word] > maxwordcount) { //For case if word is the first word added to the map
maxwordcount = words[word]; //change maxwordcount
topwords.insert( topwords.begin(), make_pair(word, words[word]) ); //insert word into the front of the top words vector
cout << "word: '" << word << "' word-count: " << words[word] << endl;
}
uniquewordcount++;
}else{ //the word is found
words[word]++; //increment count for word by 1
if (words[word] > maxwordcount) { //check if wordcount > maxwordcount
topwords.insert( topwords.begin(), make_pair(word, words[word]) ); //insert word into the front of the top words vector
}
}
wordcount++;
}
At the end of the program I want to display top 10 or so words from the txt file. I tested whether the while loop was running by displaying a live wordcount (cout). The number was going up, but it was going up extremely slow. Also I'm using huge books for my txt files.
Image of results when running
I also don't completely understand inserting variables into maps and vectors, so something might be going wrong there.
I've hit a dead-end, so anything will help at this point.
I used a smaller text file too to test:
This is a small sentence to test test test
hey hey
Results:
word: 'This' word-count: 1
1
2
3
4
5
6
7
7
7
8
8
There were 11 words in the file.
There were 8 unique words in the file.
Top 20 words in little.txt:
hey 2
test 3
test 2
This 1
Segmentation fault
I know I'm doing something wrong, but I don't have a clue where to look next or what to test. Still an amateur at C++ and C too.
you should read the file by lines, process line by line
read file file by line: https://www.systutorials.com/how-to-process-a-file-line-by-line-in-c/
https://www.geeksforgeeks.org/split-a-sentence-into-words-in-cpp/
https://www.w3schools.com/cpp/cpp_functions.asp
https://www.w3schools.com/cpp/cpp_function_param.asp
https://www.w3schools.com/cpp/cpp_function_return.asp
https://www.w3schools.com/cpp/cpp_pointers.asp <--- very importend
https://www.w3schools.com/cpp/cpp_references.asp <--- likewise importend
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <regex>
#include <iterator>
using namespace std;
vector<std::pair <string, int> > topwords;
void store(vector<pair <string, int> > &topwords, string str){
auto pos = std::find_if(topwords.begin(), topwords.end(),
[str](std::pair<string, int> const &b) {
return b.first == str;
});
//std::cout<< pos->first << endl;
if(pos != topwords.end()){
std::cout << "word: " << pos->first << " " << pos->second << " found" << endl;
pos->second++;
}
else{
std::cout << "not found" << endl;
topwords.push_back( make_pair(str,1) );
}
}
void removeDupWord(string str)
{
// Used to split string around spaces.
istringstream ss(str);
// Traverse through all words
/*
do {
// Read a word
string word;
ss >> word;
// Print the read word
cout << word << endl;
store(topwords, word );
// While there is more to read
} while (ss);
*/
string word;
while (ss >> word) {
//cout << word << endl;
const std::regex sanitized{ R"([-[\]{}()*+?.,\^$|#\s])" };
std::stringstream result;
std::regex_replace(std::ostream_iterator<char>(result), word.begin(), word.end(), sanitized, "");
//store(topwords, word );
store(topwords, result.str() );
}
}
void readReadFile(string &fileName){
std::cout << "fileName" << fileName << endl;
std::ifstream file(fileName);
std::string str;
while (std::getline(file, str)) {
//std::cout << str << "\n";
removeDupWord(str);
//store(topwords, str);
}
}
bool compareFunction (const std::pair<std::string, int> &a, const std::pair<std::string, int> &b) {
return a.first<b.first; // sort by letter
}
bool compareFunction2 (const std::pair<std::string, int> &a, const std::pair<std::string, int> &b) {
return a.second>b.second; // sort by count
}
bool cmp(pair<string, int> &A, pair<string, int> &B) {
return A.second < B.second;
}
void check(vector<pair <string, int> > &topwords){
std::pair<string, int> mostUsedWord = make_pair("",0);
for(auto ii : topwords){
std::cout << "word: " << ii.first << " count: " << ii.second << endl;
if(ii.second > mostUsedWord.second){
mostUsedWord.first = ii.first;
mostUsedWord.second = ii.second;
}
}
std::cout << "most used Word: " << mostUsedWord.first << " x " << mostUsedWord.second << " Times." << endl;
}
void get_higestTopTenValues(vector<pair <string, int> > &topwords){
std::sort(topwords.begin(),topwords.end(),compareFunction2);//sort the vector by count
int MAX = std::max_element(topwords.begin(), topwords.end(), cmp)->second;
std::cout << "max: " << MAX << endl;
for(auto ii : topwords){
//std::cout << "word: " << ii.first << " count: " << ii.second << endl;
if(ii.second >= (MAX - 10)){
std::cout << ii.first << " " << ii.second << endl;
}
}
}
void get_LowestTopTenValues(vector<pair <string, int> > &topwords){
std::sort(topwords.begin(),topwords.end(),compareFunction2);//sort the vector by count
int MIN = std::min_element(topwords.begin(), topwords.end(), cmp)->second;
std::cout << "min: " << MIN << endl;
for(auto ii : topwords){
//std::cout << "word: " << ii.first << " count: " << ii.second << endl;
if(ii.second <= (MIN + 9)){
std::cout << ii.first << " " << ii.second << endl;
}
}
}
int main ()
{
std::string word, fileName;
fileName = "input.txt";
readReadFile(fileName);
topwords.push_back( make_pair("ba",1) );
topwords.push_back( make_pair("bu",1) );
topwords.push_back( make_pair("hmmm",1) );
topwords.push_back( make_pair("what",1) );
topwords.push_back( make_pair("and",1) );
topwords.push_back( make_pair("hello",1) );
word = "hellos";
store(topwords, word);
store(topwords, word);
store(topwords, word);
store(topwords, word);
word = "hello";
store(topwords, word);
store(topwords, word);
store(topwords, word);
store(topwords, word);
store(topwords, word);
store(topwords, word);
std::sort(topwords.begin(),topwords.end(),compareFunction);//sort the vector by letter
// or
//std::sort(topwords.begin(),topwords.end(),compareFunction2);//sort the vector by count
std::cout << "---------------------------------------" << endl;
std::cout << " get all values" << endl;
std::cout << "---------------------------------------" << endl;
check(topwords);
std::cout << "---------------------------------------" << endl;
std::cout << " get the top 10 highest values" << endl;
std::cout << "---------------------------------------" << endl;
get_higestTopTenValues(topwords);
std::cout << "---------------------------------------" << endl;
std::cout << " get the top 10 lowest values" << endl;
std::cout << "---------------------------------------" << endl;
get_LowestTopTenValues(topwords);
}
This question has his been aswered long time ago. I stumbled over the question and asnwer and I find everything overly complicated.
Therefore I would like to add a more modern C++ solution making use of existing STL elements.
This makes the code more compact.
Please see below:
#include <iostream>
#include <utility>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
const std::string fileName{"r:\\loremipsum.txt"};
int main() {
if (std::ifstream textFileStream{ fileName }; textFileStream) {
// Here we store the count of all words
std::unordered_map<std::string, size_t> counter{};
size_t countOfOverallWords{}; // Counter for the number of all words
// Read all words from file, remove punctuation, and count teh occurence
for (std::string word; textFileStream >> word; counter[word]++) {
word.erase(std::remove_if(word.begin(), word.end(), ispunct), word.end());
++countOfOverallWords;
}
// For storing the top 10
std::vector<std::pair<std::string, size_t>> top(10);
// Get top 10
std::partial_sort_copy(counter.begin(), counter.end(), top.begin(), top.end(),
[](const std::pair<std::string, size_t >& p1, const std::pair<std::string, size_t>& p2) { return p1.second > p2.second; });
// Now show result
std::cout << "Count of overall words:\t " << countOfOverallWords << "\nCount of unique words:\t " << counter.size() << "\n\nTop 10:\n";
for (const auto& t : top) std::cout << "Value: " << t.first << "\t Count: " << t.second << '\n';
}
else std::cerr << "\n\nError: Could not open source file '" << fileName << "'\n\n";
return 0;
}
Developed and tested with Microsoft Visual Studio Community 2019, Version 16.8.2.
Additionally compiled and tested with clang11.0 and gcc10.2 with flags --std=c++17 -Wall -Wextra -Wpedantic
Language: C++17

C++ / Boost:: Propery_Tree - Is it possible to take ini config value with dot in name [duplicate]

"A": "1"
"A.B": "2"
"A.C": "3"
How to get the value of A.B if i iterate through the ptree it works. if i try
to get value of pt.get_child("A\.B").get_value<std::string>(). i get the following exception
terminate called after throwing an instance of boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::ptree_bad_path> >'
what(): No such node
please find the complete code below
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
#include <string>
#include <iterator>
using boost::property_tree::ptree;
/* Indent Json Output */
std::string indent(int level) {
std::string s;
for (int i = 0; i < level; i++) s += " ";
return s;
}
/* Print tree in json format */
void printTree(ptree & pt, int level) {
if (pt.empty()) {
std::cerr << "\"" << pt.data() << "\"";
} else {
if (level) std::cerr << std::endl;
std::cerr << indent(level) << "{" << std::endl;
for (ptree::iterator pos = pt.begin(); pos != pt.end();) {
std::cerr << indent(level + 1) << "\"" << pos-> first << "\": ";
printTree(pos->second, level + 1);
++pos;
if (pos != pt.end()) {
std::cerr << ",";
}
std::cerr << std::endl;
}
std::cerr << indent(level) << " }";
}
return;
}
int main()
{
ptree pt;
read_ini("sample.ini", pt);
printTree(pt, 0);
std::cout << pt.get_child("A.B").get_value<std::string>() << std::endl; //tries to resolve A.B to two nodes
std::cout << pt.get_child("A\\.B").get_value<std::string>() << std::endl; //error
}
sample.ini
A=1
A.B=2
A.C=3
You can use alternative path delimiters, but it's a bit tricky and not very well documented.
You have to temporarily specify an alternative path separator:
Live On Coliru
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
int main() {
ptree pt;
pt.put("a.b", "first");
pt.put(ptree::path_type("a|complicated.name", '|'), "second");
write_ini(std::cout, pt);
}
Prints
[a]
b=first
complicated.name=second

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 . . .

ptree get_value with name including "."

"A": "1"
"A.B": "2"
"A.C": "3"
How to get the value of A.B if i iterate through the ptree it works. if i try
to get value of pt.get_child("A\.B").get_value<std::string>(). i get the following exception
terminate called after throwing an instance of boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::ptree_bad_path> >'
what(): No such node
please find the complete code below
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
#include <string>
#include <iterator>
using boost::property_tree::ptree;
/* Indent Json Output */
std::string indent(int level) {
std::string s;
for (int i = 0; i < level; i++) s += " ";
return s;
}
/* Print tree in json format */
void printTree(ptree & pt, int level) {
if (pt.empty()) {
std::cerr << "\"" << pt.data() << "\"";
} else {
if (level) std::cerr << std::endl;
std::cerr << indent(level) << "{" << std::endl;
for (ptree::iterator pos = pt.begin(); pos != pt.end();) {
std::cerr << indent(level + 1) << "\"" << pos-> first << "\": ";
printTree(pos->second, level + 1);
++pos;
if (pos != pt.end()) {
std::cerr << ",";
}
std::cerr << std::endl;
}
std::cerr << indent(level) << " }";
}
return;
}
int main()
{
ptree pt;
read_ini("sample.ini", pt);
printTree(pt, 0);
std::cout << pt.get_child("A.B").get_value<std::string>() << std::endl; //tries to resolve A.B to two nodes
std::cout << pt.get_child("A\\.B").get_value<std::string>() << std::endl; //error
}
sample.ini
A=1
A.B=2
A.C=3
You can use alternative path delimiters, but it's a bit tricky and not very well documented.
You have to temporarily specify an alternative path separator:
Live On Coliru
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;
int main() {
ptree pt;
pt.put("a.b", "first");
pt.put(ptree::path_type("a|complicated.name", '|'), "second");
write_ini(std::cout, pt);
}
Prints
[a]
b=first
complicated.name=second

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();
}