text file contents to array - c++

I have a text file-> info.txt which contains the follow Id,games,amount
info.txt
1,left4dead,50
2,warcraft3,60
I know how to extract the details using vectors with my codes shown below.
stock.h
#ifndef stock_stock_h
#define stock_stock_h
#include <iostream>
class stock {
public:
stock() {
columnOneText = " ";
columnTwoText = " ";
}
stock(std::string columnOneText,
std::string columnTwoText
);
std::string getColumnOne();
std::string getColumnTwo();
void setItemId(std::string columnOneText);
void setItemDescription(std::string columnTwoText);
private:
std::string columnOneText, columnTwoText;
};
#endif
main.cpp
#include "stock.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
stock::stock(string columnOneText,
string columnTwoText)
{
setItemId(columnOneText);
setItemDescription(columnTwoText);
};
string stock::getColumnOne() {
return columnOneText;
}
string stock::getColumnTwo() {
return columnTwoText;
}
void stock::setItemId(string columnOneText) {
this->columnOneText = columnOneText;
}
void stock::setItemDescription(std::string columnTwoText) {
this->columnTwoText = columnTwoText;
}
int main(){
vector<stock> itemDetails;
string line;
string columnOneText;
string columnTwoText;
ifstream readFile("info.txt");
while(getline(readFile,line)) {
stringstream iss(line);
getline(iss, columnOneText,',');
getline(iss, columnTwoText, ',');
//consturctor
stock splitedColumns(columnOneText,
columnTwoText
);
itemDetails.push_back(splitedColumns);
}
readFile.close();
cout << "this is column One in text file" << endl;
for (int i =0; i<itemDetails.size(); i++) {
cout << itemDetails[i].getColumnOne() << " " << endl;
}
cout << "this is column Two in text file" << endl;
for (int i =0; i<itemDetails.size(); i++) {
cout << itemDetails[i].getColumnTwo() << " " << endl;
}
}
what I don't really know is how to extract the details using arrays instead of using vectors.
I have tried this but it doesn't seem to work
string line;
string columnOneText;
string columnTwoText;
string columnOneTextArray[7];
while(getline(readFile,line)) {
stringstream iss(line);
getline(iss, columnOneText,',');
getline(iss, columnTwoText, ',');
for(int i=0; i<8; i++) {
columnOneTextArray[i] = columnOneText;
cout << columnOneTextArray[i];
}
}
You guys might ask why do I want to do it in arrays instead of using vector,
I just curious and exploring how it can be done using arrays.

You problem can be solved in a lot simpler manner by using struct definitions in addition to using vectors (your use of classes is a bit of overkill, IMHO):
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <sstream>
//this utilizes a more idiomatic naming convention as opposed to `columnX`
typedef struct Stock {
int id;
std::string games;
int amount;
} Stock;
//Syntactic sugar for easier output
std::ostream& operator << (std::ostream& in, const Stock& stock) {
in << stock.id << " " << stock.games << " " << stock.amount;
return in;
}
int main() {
std::string filename = "info.txt";
std::fstream ifile;
std::vector <Stock> inventory;
Stock stock_t;//a temporary Stock variable
std::string temp;
ifile.open(filename.c_str(), std::ios::in);
if (!ifile.is_open()) {
std::cerr << "There was a problem opening the input file: \"" << filename << "\"\n";
exit(1);
}
while (ifile >> temp) {
std::istringstream iss(temp);
int column = 0;
//break each constituent line by using the comment as a delimiter:
while(std::getline(iss, temp, ',')) {
if (column == 0) {
stock_t.id = std::atoi(temp.c_str());
}
else if (column == 1) {
stock_t.games = temp;
}
else if (column == 2) {
stock_t.amount = std::atoi(temp.c_str());
}
++column;
}
inventory.push_back(stock_t);
}
for (int i = 0; i < inventory.size(); ++i) {
std::cout << inventory[i] << "\n";
}
ifile.close();
return 0;
}
Which outputs:
1 left4dead 50
2 warcraft3 60

Related

C++ Only reading last line when reading text from a list

Ive written a loop function that compares a type String to another String, like such
double robotComplexity() {
double complexity;
if(newChar == "A") {
cout << "Character: " << newChar;
} else {
cout << "Program isnt working! :(";
}
if(complexity > 100) {
complexity = 100;
}
cout << "\nThe Robot Complexity is: " << complexity << endl;
return complexity;
}
However when the program runs, the latter of the if statement returns "Program does not work :("
However within the text file there is in fact a record that contains a character of A
A:Head:1:2:15.
B:Torso:0:6:5.
C:Leg:0:4:6.
D:Arm:0:4:8.
E:Tail:0:6:2.
I am unsure is why the loop function doesnt recongize it. The expected output for would return "A" in string form, as the program loops for the records of the file until it locates the character A linked to a variable partCode. As of now it does not do this.
My full program is as follows
#include <iostream>
#include <string>
#include <conio.h>
#include <stdio.h>
#include "driver.h"
#include <sstream>
#include <fstream>
#include <fstream>
#include <vector>
using namespace std;
//declaration of customer varialbes
std::string customerName;
std::string projectName;
std::string partNumber;;
//declaration of parts variables
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
//declaration of builder variables
std::string name;
int ability;
int variability;
std::vector<string> builderVector;
std::vector<string> partsVector;
std::vector<string> customerVector;
std::ifstream buildersList("Builders.txt");
std::ifstream partsList("Parts.txt");
std::ifstream customerList("Customers.txt");
std::string outputFile = "output.txt";
std::string input;
std::string newChar;
std::stringstream convertChar;
void readFile() //function to read Builders, Customers and Parts text file
{
std::string line;
while (std::getline(partsList, line)) {
line.pop_back(); //removing '.' at end of line
std::string token;
std::istringstream ss(line);
convertChar << partCode;
convertChar >> newChar;
// then read each element by delimiter
int counter = 0;//number of elements you read
while (std::getline(ss, token, ':')) { //spilt into different records
switch (counter) {//put into appropriate value-field according to element-count
case 0: newChar = token; //convert partCode from a char to a string
break;
case 1: partName = token; break;
case 2: maximum = stoi(token); break;
case 3: minimum = stoi(token); break;
case 4: complexity = stoi(token); break;
default: break;
}
counter++; //increasing counter
}
//cout << "Part Code: " << newChar << endl;
// cout << "Part Name: "<< partName << endl;
// cout << "Maximum: "<< maximum << endl;
// cout << "Minimum: "<< minimum << endl;
// cout << "Complexity: "<< complexity << endl;
}
while (std::getline(customerList, line)) {
line.pop_back();//removing '.' at end of line
std::string token;
std::istringstream ss(line);
// then read each element by delimiter
int counter = 0;//number of elements you read
while (std::getline(ss, token, ':')) {//spilt into different records
switch (counter) {//put into appropriate value-field according to element-count
case 0: customerName = token; break;
case 1: projectName = token; break;
case 2: partNumber = token; break;
default: break;
}
counter++;//increasing counter
}
// cout << "Customers name: " << customerName << endl;
// cout << "Project name: "<< projectName << endl;
// cout << "Part number: "<< partNumber << endl;
}
}
double robotComplexity() {
double complexity;
while(partsList.is_open) {
if(newChar == "A") {
cout << "Character: " << newChar;
} else {
cout << "Program isnt working! :(";
}
}
if(complexity > 100) {
complexity = 100;
}
cout << "\nThe Robot Complexity is: " << complexity << endl;
return complexity;
}
double robotVariability() {
double variability;
cout << "\nThe Robot Variability is: " << variability << endl;
return variability;
}
void writeFile() //writes to a file output.txt the end calculations.
{
}
Main file
#include <iostream>
#include <string>
#include <conio.h>
#include <stdio.h>
#include "driver.h"
#include "implementation.cpp"
#include <fstream>
#include <vector>
using namespace std;
int main() {
readFile();
writeFile();
robotComplexity();
getch();
return 0;
}
If anyone can provide a solution that would be great.
I have just rearranged your code, as it should look like, and it works properly.
In coding, having a messy code means having bugs you can not trace. Split your code to the smallest logical components, and write the code so it will be readable (no need for comments in code!). I have done nothing fancy other than reorganizing the parsing code.
Also, notice that you have a dot (.) in the end of each line, you should either remove it from the input, or handle it in code.
// these are the streams used:
#include <istream>
#include <sstream>
std::vector<std::string> parse_stream(std::istream& stream)
{
std::vector<std::string> res{};
std::string line;
while(std::getline(stream, line))
{
res.push_back(line);
}
return res;
}
std::vector<std::string> split_line(std::istream& stream, char delimiter)
{
std::vector<std::string> res{};
std::string token;
while(std::getline(stream, token, delimiter))
{
res.push_back(token);
}
return res;
}
void print_vector(const std::vector<std::string>& vec)
{
for (const std::string& str: vec)
{
std::cout << str << ", ";
}
std::cout << std::endl;
}
int main()
{
std::string in_str = "A:Head:1:2:15\nB:Torso:0:6:5\nC:Leg:0:4:6\nD:Arm:0:4:8\nE:Tail:0:6:2";
std::istringstream ss{in_str};
auto parsed_stream = parse_stream(ss);
print_vector(parsed_stream);
for (const auto& line : parsed_stream)
{
std::istringstream line_ss{line};
auto splited_line = split_line(line_ss, ':');
print_vector(splited_line);
}
}
The output is:
A:Head:1:2:15, B:Torso:0:6:5, C:Leg:0:4:6, D:Arm:0:4:8, E:Tail:0:6:2,
A, Head, 1, 2, 15,
B, Torso, 0, 6, 5,
C, Leg, 0, 4, 6,
D, Arm, 0, 4, 8,
E, Tail, 0, 6, 2,

Am I building a correct way of organizing characters so I can count them?

The code is supposed to be able to count the characters in total then count each time they appear within a text file. I tried building a struct that makes an array which is an integer and char array at the same time so that I can have the counting in the same place as my array. But now I am stuck. I've looked online a lot but cannot find what I need to help me. Anyone got some advice? Also in the code if you see anything that should be changed I appreciate the tips! I am newer at c++ so go easy on me please.
Structs, multiple arrays, searching internet for answers
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
struct letters {
int count;
char letter;
};
constexpr int SIZE = 10000;
std::string fileName, word;
int count = 0, charCount = 0;
int Alphabet[26];
letters chars[];
void getFileName(std::string& fileName);
int countWords(int& count, std::string& fileName, std::string word);
int countChar(int& charCount, std::string& fileName, letters chars[]);
void sortChars(letters chars[SIZE], int SIZE);
int main()
{
getFileName(fileName);
countWords(count, fileName, word);
countChar(charCount, fileName, chars);
sortChars(chars, SIZE);
return 0;
}
void getFileName(std::string& fileName)
{
std::cout << "Please enter the name of the file followed by the type (ex: text.txt) : " << std::endl;
std::getline(std::cin, fileName);
}
int countWords(int& count, std::string& fileName, std::string word)
{
count = 0;
std::ifstream infile(fileName);
while (infile >> word) {
count++;
}
std::cout << count << std::endl;
return count;
}
int countChar(int& charCount, std::string& fileName, letters chars[])
{
std::ifstream infile(fileName);
while (infile >> chars->letter) {
count++;
}
std::cout << charCount;
return charCount;
}
void sortChars(letters chars[SIZE], int SIZE)
{
int i = 0;
std::ifstream infile(fileName);
while (infile >> chars[i].letter) {
for (int i = 0; i <= chars->count; i++) {
if (infile == chars[i].letter) {
chars[i].count++;
}
}
}
}
void printCount()
{
std::cout << count << std::endl;
std::cout << charCount << std::endl;
std::ifstream infile(fileName);
}
The struct should count the number of times 'A' or 'a', should be able to convert to one case, but I can do this after it counts one or the other. My tester file is in all lowercase so that would be a good place to start.
Even bigger hint, use a std::unordered_map to count the characters:
#include <cstdlib> // EXIT_FAILURE
#include <cctype> // std::isupper(), std::tolower()
#include <string> // std::string<>, std::getline()
#include <unordered_map> // std::unordered_map<>
#include <iostream> // std::ifstream
#include <fstream> // std::cout, std::cerr
int main()
{
std::string file_name;
if (!std::getline(std::cin, file_name)) {
std::cerr << "Input error. :(\n\n";
return EXIT_FAILURE;
}
std::ifstream is{ file_name };
if (!is.is_open()) {
std::cerr << "Couldn't open \"" << file_name << "\" for reading. :(\n\n";
return EXIT_FAILURE;
}
std::size_t num_words = 0;
std::unordered_map<char, std::size_t> char_counts;
for (std::string word; is >> word; ++num_words) {
for (auto ch : word) {
if (std::isupper(ch))
ch = std::tolower(ch);
++char_counts[ch];
}
}
for (auto const &count : char_counts)
std::cout << "'" << count.first << "': " << count.second << '\n';
std::cout << "Number of words: " << num_words << "\n\n";
} // when control reaches the end of main() without encountering a return-statement
// it has the same effect as return 0;
If you insist on splitting that few lines of code up into functions:
#include <cstdlib> // EXIT_FAILURE
#include <cctype> // std::isupper(), std::tolower()
#include <string> // std::string<>, std::getline()
#include <unordered_map> // std::unordered_map<>
#include <iostream> // std::ifstream
#include <fstream> // std::cout, std::cerr
std::string get_file_name()
{
std::cout << "Filename: ";
std::string file_name;
if (!std::getline(std::cin, file_name))
std::cerr << "Input error. :(\n\n";
return file_name;
}
std::ifstream open_file(std::string file_name)
{
std::ifstream file{ file_name };
if (!file.is_open())
std::cerr << "Couldn't open \"" << file_name << "\" for reading. :(\n\n";
return file;
}
std::size_t get_file_stats(std::istream &is, std::unordered_map<char, std::size_t> &char_counts)
{
std::size_t num_words = 0;
for (std::string word; is >> word; ++num_words) {
for (auto ch : word) {
if (std::isupper(ch))
ch = std::tolower(ch);
++char_counts[ch];
}
}
return num_words;
}
int main()
{
std::string file_name{ get_file_name() };
if (!file_name.length())
return EXIT_FAILURE;
std::ifstream is{ open_file(file_name) };
if (!is.is_open())
return EXIT_FAILURE;
std::unordered_map<char, std::size_t> counts;
std::cout << "Number of words: " << get_file_stats(is, counts) << "\n\n";
for (auto const &count : counts)
std::cout << "'" << count.first << "': " << count.second << '\n';
}

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

Save a Struct Vector into a Tab delimited Text File

I'm reading a 2 to 4 gb .txt file and then I manipulate some of the data and want to save my struct vector as a tab delimited .txt file. I read some of the other questions but still not clear to me how I'm going to do it in my program.
So my question is: How to save the Input vector results as a tab delimited .txt file?
Below is my code:
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
using namespace std;
struct Input_Spec {
std::string Data;
std::string Ativo;
int Buy_Sell;
double Sequencia;
double Id;
int Event;
std::string Hr_Priority;
double Priority;
double Price;
double Qtd_Total;
double Qtd_Traded;
std::string Data_Order;
std::string Data_Time_Order;
std::string State_Of_Order;
std::string Condition_Of_Order;
double Broker;
};
void split(const std::string &s, char delim, std::string elems[])
{
std::stringstream ss(s);
std::string item;
long i = 0;
while (std::getline(ss, item, delim))
{
elems[i++] = item;
}
}
int main()
{
ifstream infile1("C:\\Teste\\Teste.txt");
ofstream output("output.txt");
string word;
string columns[16];
string line;
int row=0;
long c=0;
long filescol=0;
for (int i = 0; std::getline(infile1, word); ++i)
{
row++;
}
//cout<<row;
infile1.close();
ifstream infile("C:\\Teste\\Teste.txt");
vector<Input_Spec> Input(row);
while( getline(infile, line))
{
split(line,';', columns);
if (columns[0]!="")
{
Input[filescol].Data =columns[0];
Input[filescol].Ativo =columns[1];
Input[filescol].Buy_Sell = stoi(columns[2]);
Input[filescol].Sequencia = stod(columns[3]);
Input[filescol].Id = stod(columns[4]);
Input[filescol].Event = stoi(columns[5]);
Input[filescol].Hr_Priority = columns[6];
Input[filescol].Priority = stod(columns[7]);
Input[filescol].Price = stod(columns[8]);
Input[filescol].Qtd_Total = stod(columns[9]);
Input[filescol].Qtd_Traded = stod(columns[10]);
Input[filescol].Data_Order = columns[11];
Input[filescol].Data_Time_Order = columns[12];
Input[filescol].State_Of_Order = columns[13];
Input[filescol].Condition_Of_Order = columns[14];
Input[filescol].Broker = stod(columns[15]);
filescol++;
c++;
}
if (c>(999))
{
break;
infile.close();
return 0;
}
}
infile.close();
return 0;
}
Here's a fragment:
ofstream output ("output.txt");
output << Input[filescol].Data << '\t';
output << Input[filescol].Ativo << '\t';
output << Input[filescol].Buy_Sell << '\t';
//...
output << Input[filescol].Broker << '\n';
Is this what you are talking about?
At first, please let me give you some hints on your code: You don't need to count all lines of the input file and you don't need to reserve memory for all items in your vector beforehand. You can use the push_back() function to add items to the vector. This saves you one iteration on the input file.
I put this improvement into the main() function below. I also expanded a little bit on the answer by #ThomasMatthews, so that you can see how to loop over the vector in order to save the data in it:
int main()
{
ifstream infile("C:\\Teste\\Teste.txt");
ofstream output("C:\\Teste\\output.txt");
string line;
string columns[16];
vector<Input_Spec> Input;
Input_Spec oneInput;
while (getline(infile, line))
{
split(line, ';', columns);
if (!columns[0].empty())
{
oneInput.Data = columns[0];
oneInput.Ativo = columns[1];
oneInput.Buy_Sell = stoi(columns[2]);
oneInput.Sequencia = stod(columns[3]);
oneInput.Id = stod(columns[4]);
oneInput.Event = stoi(columns[5]);
oneInput.Hr_Priority = columns[6];
oneInput.Priority = stod(columns[7]);
oneInput.Price = stod(columns[8]);
oneInput.Qtd_Total = stod(columns[9]);
oneInput.Qtd_Traded = stod(columns[10]);
oneInput.Data_Order = columns[11];
oneInput.Data_Time_Order = columns[12];
oneInput.State_Of_Order = columns[13];
oneInput.Condition_Of_Order = columns[14];
oneInput.Broker = stod(columns[15]);
Input.push_back(oneInput);
}
}
// ----------------------
// Modify your data here.
// ----------------------
for(vector<Input_Spec>::const_iterator it = Input.begin(); it != Input.end(); it++)
{
output << it->Data << '\t';
output << it->Ativo << '\t';
output << it->Buy_Sell << '\t';
output << it->Sequencia << '\t';
output << it->Id << '\t';
output << it->Event << '\t';
output << it->Hr_Priority << '\t';
output << it->Priority << '\t';
output << it->Price << '\t';
output << it->Qtd_Total << '\t';
output << it->Qtd_Traded << '\t';
output << it->Data_Order << '\t';
output << it->Data_Time_Order << '\t';
output << it->State_Of_Order << '\t';
output << it->Condition_Of_Order << '\t';
output << it->Broker << '\n';
}
output.close();
infile.close();
return 0;
}
I guess that more improvements could be done to your code, but this should give you a good start.

How to read data from a text file into a struct

I'm completely new to C++ and currently I'm trying to read very basic text file which look like this:
Dr John Doe
British
2
Soccer
Swimming
and my expected output should look like:
My information
Name: John Doe
Nationality: British
I have 2 hobbies:
1. Soccer
2. Swimming
My header file:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
const int MAX = 80;
const int MAXNO = 5;
enum Title {Miss, Mrs, Mr, Dr, Unknown};
struct Date
{
int day;
int month;
int year;
};
struct MyInfo
{
char name [MAX];
char national [MAX];
int noOfHobbies;
char hobby [MAXNO][MAX];
};
void getMyInfo (fstream& , char[] , MyInfo&);
void displayMyInfo (MyInfo);
My functions:
#include "Lab_1.h"
void getMyInfo (fstream& afile,char fileName[], MyInfo& x) {
afile.open (fileName);
if (!afile)
{
cout << "Binary file " << fileName << " opened for creation failed" << endl;
exit (-1);
}
cout << "\n" << "Begin reading of " << fileName << endl;
string line;
while(getline(afile, line))
{
afile >> x.national;
afile >> x.noOfHobbies;*/
if (afile >> x.name >> x.national >> x.noOfHobbies) {
cout << "Name: " << x.name << ", "
<< "National: " << x.national << ", "
<< "noOfHobbies: " << x.noOfHobbies << ", "
<< endl;
}
}
}
void displayMyInfo (MyInfo x) {
}
My main function:
#include "Lab_1.h"
int main () {
fstream afile;
MyInfo x;
string fileName;
getMyInfo(afile,"textfile.txt",x);
//displayMyInfo(x);
afile.close ();
}
The above code output nothing because I just put everything I understand over the forum with similar question. Since I'm already stuck for 1 day even though I've already done a lot of research but most of them suggest to use vector which I'm not familiar with at this moment, so can someone give me a solution to this problem? Thank you very much for your help in advance.
Random act of madness kindness:
Live On Coliru
#include <fstream>
#include <set>
struct Person {
std::string name;
std::string nationality;
std::set<std::string> hobbies;
friend std::istream& operator>>(std::istream& is, Person& into) {
size_t n = 0;
if (getline(is, into.name) &&
getline(is, into.nationality) &&
is >> n && is.ignore(1024, '\n'))
{
while (n--) {
std::string hobby;
if (getline(is, hobby))
into.hobbies.insert(hobby);
else
is.setstate(std::ios::failbit);
}
}
return is;
}
};
#include <iostream>
int main() {
std::ifstream ifs("input.txt");
Person p;
if (ifs >> p) {
std::cout << "My information\n";
std::cout << p.name << "\n";
std::cout << p.nationality << "\n";
std::cout << "I have " << p.hobbies.size() << " hobbies:\n";
size_t counter = 0;
for(auto const& hobby : p.hobbies) {
std::cout << ++counter << ". " << hobby << "\n";
}
} else {
std::cerr << "Parse failure\n";
}
}