How to parse the following text file into class? - c++

I have the following class
class Film {
Person authors[5]; //This will actually include only the director
string title;
string producer;
int n_authors;
int year;
int running_time;
Person actors[5];
int n_actors;
}
And the following file format (don't ask me why I use this, I MUST use this format)
Stanley
Kubrick
#
2001: A Space Odissey
*
1968
161
Keir
Dullea
Gary
Lockwood
#
The # indicates the end of a list (in this case a 'Person' class), while the * a missing field (in this case the producer, btw the producer field must be filled with an * in the class).
The class Person consists of Name and Surname and has an overloaded operator >> that calls:
void load(ifstream& in) {
getline(in,name);
getline(in,surname);
}
What's the best method to parse this file structure? I can't use regular expressions or anything more advanced than ifstream. My concern is on how (and where in the code) to detect the end of file and the end of a list of people.

The standard line reading idiom:
#include <fstream> // for std::ifstream
#include <sstream> // for std::istringstream
#include <string> // for std::string and std::getline
int main()
{
std::ifstream infile("thefile.txt");
std::string line;
while (std::getline(infile, line))
{
// process line
}
}
Where it says "process line" you should add some logic that tracks the current state of the parser.
For your simple application you could proceed in bits, reading lists and tokens as specified by the format. For instance:
std::vector<std::string> read_list(std::istream & in)
{
std::string line;
std::vector<std::string> result;
while (std::getline(in, line))
{
if (line == "#") { return result; }
result.push_back(std::move(line));
}
throw std::runtime_error("Unterminated list");
}
Now you can say:
std::string title, producer, token3, token4, token5, token6;
std::vector<std::string> authors = read_list(infile);
if (!(std::getline(infile, title) &&
std::getline(infile, producer) &&
std::getline(infile, token3) &&
std::getline(infile, token4) &&
std::getline(infile, token5) ) )
{
throw std::runtime_error("Invalid file format");
}
std::vector<std::string> actors = read_list(infile);
You can use std::stoi to convert tokens 3 – 5 to integers:
int year = std::stoi(token4);
int runtime = std::stoi(token5);
Note that the n_authors and n_actors variables are redundant, since you have self-terminated lists already. You can or should use the variables as an integrity check if you like.

Related

Conditions and files

class Client
{
public:
Client(int id, string title, int age):
~Client();
void addTW(int id, string title, int age);
int getID() const {return id;}
string getTitle() const {return title;}
int getAge() const {return age;}
private:
int id;
string title;
int age;
};
I have two functions:
load(), which is loading input .txt file - file's having titles of movies and age you need to have in order to watch the movie (e.x. Pulp Fiction - 16) and
addTW(int id, string title, int age), which adds Movies.
So, while adding a movie, you need to type id, title and age. I want to make that you cannot add movie if you're under a certain age (e.x. 16 or whatever). Age must be re-added from the .txt file. Basically age in connected with and only title.
I've never used .txt files. So I have no idea how to start.
#include <fstream>
void Client::addTW(int id, string title, int age)
{
int i, n = tw.size();
for(i = 0;i<n;i++)
{
ToWatch* newTW = new ToWatch(id, title, age);
tw.push_back(newTW);
return;
}
}
void Client::load()
{
ifstream input;
input.open("input.txt");
if(input.fail())
{ cout<<"Failure"<<endl;}
else
{
string s;
while(input>>s)
{
cout<<s<<" ";
}
}
input.close();
}
I am not sure, if the design of your class is OK. This you can find out by yourself.
I can help you with reading the file and extracting the age for a given title:
Please see:
#include <iostream>
#include <fstream>
#include <string>
unsigned int getAgeFromFile(const std::string& title) {
// We set a default age of 0. So, if we cannot find the title in the list, then everybody can look it
unsigned int resultingAge{ 0 };
// Define an ifstream variable. Use its constructor, to open the file, then check, if open was ok
if (std::ifstream fileMovies("input.txt"); fileMovies) {
// Read all lines in the text file in a loop with std::getline. std::getline will return false,
// if we are at end-of-file or in case of some other error. Then the loop will stop
for (std::string line{}; std::getline(fileMovies, line); ) {
// So, now we have a line from the file in tour "line" variable.
// Check, if the searched title is in it
if (line.find(title) != std::string::npos) {
// Ok, we found the title in this line. Now, we need to extract the age.
// It is at the end of the line and separated by a space. So, search from the end of the line for a space
if (size_t pos{ line.rfind(' ') }; pos != std::string::npos) {
// We found a space. Now, convert the number.
resultingAge = std::stoul(line.substr(pos));
}
}
}
}
// return result or default value, if not found
return resultingAge;
}
In your addTW function you need to insert one line before the
push_back.
if (age > getAgeFromFile(title))
Hope this helps.
Compiled and tested with VS2019 and C++17

C++ reading in a comma delimited file[with one section as mm/dd/yyyy] by line, placing into a struct securely

To expand on the title, I am reading a file line-by-line that appears as so:
FirstName,LastName,mm/dd/yyyy,SSN,Role,Salary,Zip,Phone
I have this code I just wrote but am having some trouble placing it into my struct as I'm using std::string as opposed to char[]. I want to continue using std::string for purposes down the road. Also, excuse any syntax errors as I haven't written in c/c++ in a while. I have also read the most elegant way to iterate to words of a string but I am still confused on how to do that with the slashes involved in the date format. SSN and Salary are private members of a struct that will be pushed into a vector for later use. How can I do this using c++ libraries? To be honest, the istringstream confuses me as they include some type of parser inside their struct directly. Is this honestly the best way to accomplish what I'm trying to do?
char stringData[150]; //line to be read in
while(fgets(stringData, 150, infile) != NULL) {
if( currentLine == 1) {
fgets(stringData, 150, infile); //get column names | trash
}
else {
lineSize = sscanf(stringData, "%[^,],%[^,],%d/%d/%d,%d,%[^,],%lf,%[^,],%s", temp.firstName,temp.lastName,
&temp.birthMonth,&temp.birthDay,&temp.birthYear,
&tempSSN, temp.role, &tempSalary, temp.zip,
temp.phoneNum);
if(lineSize != 10) { //error message due to a row being incorrect
cerr << "/* ERROR: WRONG FORMAT OF INPUT(TOO FEW OR TOO MANY ARGUMENTS) ON LINE: */" << currentLine << '\n';
exit(1);
}
temp.setSSN(tempSSN);
temp.setSalary(tempSalary);
vector.push_back(temp);//push Employee temp into the vector and repeat loop
}
currentLine++
}
TL;DR: What is the easiest way to do this using c++ libraries?
As Sam Varshavchik already mentioned, the easiest way would be separating input with , then separate on of them with / again.
Thanks to this famous question I'm using following approach to split string :
template<typename Out>
void split(const std::string &s, char delim, Out result)
{
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
{
*(result++) = item;
}
}
std::vector<std::string> split(const std::string &s, char delim)
{
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
assuming that this is your structure :
struct info
{
std::string firstName;
std::string lastName;
std::string birthMonth;
std::string birthDay;
std::string birthYear;
std::string tempSSN;
std::string role;
std::string tempSalary;
std::string zip;
std::string phoneNum;
};
I would implement your needed function like this :
void parser(std::string fileName, std::vector<info> &inf)
{
std::string line;
std::ifstream infile(fileName);
int index = inf.size();
while(std::getline(infile, line))
{
inf.push_back({});
std::vector<std::string> comma_seprated_vec = split(line, ',');
inf.at(index).firstName = comma_seprated_vec.at(0);
inf.at(index).lastName = comma_seprated_vec.at(1);
inf.at(index).tempSSN = comma_seprated_vec.at(3);
inf.at(index).role = comma_seprated_vec.at(4);
inf.at(index).tempSalary = comma_seprated_vec.at(5);
inf.at(index).zip = comma_seprated_vec.at(6);
inf.at(index).phoneNum = comma_seprated_vec.at(7);
std::vector<std::string> slash_seprated_vec = split(comma_seprated_vec.at(2), '/');
inf.at(index).birthMonth = slash_seprated_vec.at(0);
inf.at(index).birthDay = slash_seprated_vec.at(1);
inf.at(index).birthYear = slash_seprated_vec.at(2);
++index;
}
}
Then you can use it like this :
int main()
{
std::vector<info> information;
parser("some file", information);
return 0;
}
There you go, your information are presented in information variable.

How to read pieces of string into a class array C++

I have an array of dvd from a Video class I created
Video dvd[10];
each video has the property,
class Video {
string _title;
string _genre;
int _available;
int _holds;
public:
Video(string title, string genre, int available, int holds);
Video();
void print();
void read(istream & is, Video dvd);
int holds();
void restock(int num);
string getTitle();
~Video();
};
I'm trying to fill up this array with data from my text file where each info such as the title and genre is separated by a comma
Legend of the seeker, Fantasy/Adventure, 3, 2
Mindy Project, Comedy, 10, 3
Orange is the new black, Drama/Comedy, 10, 9
I've tried using getline(in, line, ',') but my brain halts when its time to insert each line into the dvd array.
I also created a read method to read each word separated by a whitespace but I figured thats not what I really want.
I also tried to read a line with getline, store the line in a string and split it from there but I get confused along the line.
**I can get the strings I need from each line, my confusion is in how to insert it into my class array in the while loop especially when I can only read one word at a time.
I need help on what approach I should follow to tackle this problem.
**My code
#include <iostream>
#include <fstream>
#include <cassert>
#include <vector>
#define MAX 10
using namespace std;
class Video {
string _title;
string _genre;
int _available;
int _holds;
public:
Video(string title, string genre, int available, int holds);
Video();
void print();
void read(istream & is, Video dvd);
int holds();
void restock(int num);
string getTitle();
~Video();
};
Video::Video(string title, string genre, int available, int holds){
_title = title;
_genre = genre;
_available = available;
_holds = holds;
}
void Video::read (istream & is, Video dvd)
{
is >> _title >> _genre >> _available>>_holds;
dvd = Video(_title,_genre,_available,_holds);
}
int Video::holds(){
return _holds;
}
void Video::restock(int num){
_available += 5;
}
string Video::getTitle(){
return _title;
}
Video::Video(){
}
void Video::print(){
cout<<"Video title: " <<_title<<"\n"<<
"Genre: "<<_genre<<"\n"<<
"Available: " <<_available<<"\n"<<
"Holds: " <<_holds<<endl;
}
Video::~Video(){
cout<<"DESTRUCTOR ACTIVATED"<<endl;
}
int main(int params, char **argv){
string line;
int index = 0;
vector<string> tokens;
//Video dvd = Video("23 Jump Street", "comedy", 10, 3);
//dvd.print();
Video dvd[MAX];
dvd[0].holds();
ifstream in("input.txt");
/*while (getline(in, line, ',')) {
tokens.push_back(line);
}
for (int i = 0; i < 40; ++i)
{
cout<<tokens[i]<<endl;
}*/
if(!in.fail()){
while (getline(in, line)) {
dvd[index].read(in, dvd[index]);
/*cout<<line<<endl;
token = line;
while (getline(line, token, ',')){
}
cout<<"LINE CUT#####"<<endl;
cout<<line<<endl;
cout<<"TOKEN CUT#####"<<endl;*/
//dvd[index] =
index++;
}
}else{
cout<<"Invalid file"<<endl;
}
for (int i = 0; i < MAX; ++i)
{
dvd[i].print();
}
}
First, I would change the Video::read function into an overload of operator >>. This will allow the Video class to be used as simply as any other type when an input stream is being used.
Also, the way you implemented read as a non-static member function returning a void is not intuitive and very clunky to use. How would you write the loop, and at the same time detect that you've reached the end of file (imagine if there are only 3 items to read -- how would you know to not try to read a fourth item)? The better, intuitive, and frankly, de-facto way to do this in C++ is to overload the >> operator.
(At the end, I show how to write a read function that uses the overloaded >>)
class Video
{
//...
public:
friend std::istream& operator >> (std::istream& is, Video& vid);
//..
};
I won't go over why this should be a friend function, as that can be easily researched here on how to overload >>.
So we need to implement this function. Here is an implementation that reads in a single line, and copies the information to the passed-in vid:
std::istream& operator >> (std::istream& is, Video& vid)
{
std::string line;
std::string theTitle, theGenre, theAvail, theHolds;
// First, we read the entire line
if (std::getline(is, line))
{
// Now we copy the line into a string stream and break
// down the individual items
std::istringstream iss(line);
// first item is the title, genre, available, and holds
std::getline(iss, theTitle, ',');
std::getline(iss, theGenre, ',');
std::getline(iss, theAvail, ',');
std::getline(iss, theHolds, ',');
// now we can create a Video and copy it to vid
vid = Video(theTitle, theGenre,
std::stoi(theAvail), // need to change to integer
std::stoi(theHolds)); // same here
}
return is; // return the input stream
}
Note how vid is a reference parameter, not passed by value. Your read function, if you were to keep it, would need to make the same change.
What we did above is that we read the entire line in first using the "outer" call to std::getline. Once we have the line as a string, we break down that string by using an std::istringstream and delimiting each item on the comma using an "inner" set of getline calls that works on the istringstream. Then we simply create a temporary Video from the information we retrieved from the istringstream and copy it to vid.
Here is a main function that now reads into a maximum of 10 items:
int main()
{
Video dvd[10];
int i = 0;
while (i < 10 && std::cin >> dvd[i])
{
dvd[i].print();
++i;
}
}
So if you look at the loop, all we did is 1) make sure we don't go over 10 items, and 2) just use cin >> dvd[i], which looks just like your everyday usage of >> when inputting an item. This is the magic of the overloaded >> for Video.
Here is a live example, using your data.
If you plan to keep the read function, then it would be easier if you changed the return type to bool that returns true if the item was read or false otherwise, and just calls the operator >>.
Here is an example:
bool Video::read(std::istream & is, Video& dvd)
{
if (is.good())
{
is >> dvd;
return true;
}
return false;
}
And here is the main function:
int main()
{
Video dvd[10];
int i = 0;
while (i < 10 && dvd[i].read(std::cin, dvd[i]))
{
dvd[i].print();
++i;
}
}
Live Example #2
However, I still say that the making of Video::read a non-static member makes the code in main clunky.

Accessing local variables from void functions

I have two functions that read files and initialize variables containing data parsed from the files read.
These variables include several vectors, counters (line counts) and a few singular variables (string and ints).
The problem I am having is that these variables all need to be accessed in later functions, and the idea is to avoid global variables. Since the functions are void, they cannot return variables, and I have found (unlike my normal language of Python) returning multiple variables is difficult.
What is a better way to go about this?
The vectors in each of the read*() functions need to be accessed in a new function I am building. But I also need the num* variables, and the recipe & serving variables.
EDIT: My code currently
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
void readNutrients(string input_file) {
ifstream in(input_file.c_str());
string line;
vector<string> nName, nUnits;
vector<double> nAmount, nCalories;
string name, units;
double amount, calories;
int numNut = 0;
while (getline(in, line)) {
numNut++;
int pos = line.find(';');
name = line.substr(0, pos);
nName.push_back(name);
line = line.substr(pos + 1);
istringstream iss(line);
iss >> amount >> units >> calories;
nAmount.push_back(amount);
nUnits.push_back(units);
nCalories.push_back(calories);
}
}
void readRecipe(string input_file) {
ifstream in(input_file.c_str());
string line;
string recipe;
vector<string> rName, rUnits;
vector<double> rAmount;
string name, units;
double amount;
double servings;
int numIng = 0;
while (getline(in, line)) {
numIng++;
if (numIng == 1) {
int pos = line.find('\n');
recipe = line.substr(0, pos);
}
else if (numIng == 2) {
istringstream iss(line);
iss >> servings;
}
else {
istringstream iss(line);
iss >> amount >> units >> ws;
rAmount.push_back(amount);
rUnits.push_back(units);
getline(iss, name);
rName.push_back(name);
}
}
}
void readFiles(string nutrientFile, string recipeFile) {
readNutrients(nutrientFile);
readRecipe(recipeFile);
}
int main(int argc, char** argv) {
readFiles(argv[1], argv[2]);
return 0;
}
Since you included your code, I have a better idea of what's going on.
What you need is to create a structure that can hold the result of your parsing. Since your function is not returning anything, it's only logical that you won't have access to it's result.
I think your intent here is to have a list of nutrients read from a file, and read every nutrients from that file and fill up the list in your program.
The problem is that your program has no idea of what makes a nutrient a nutrient. You should teach him that by declaring what makes a nutrient a nutrient:
struct Nutrient {
std::string name, unit;
double amount, calories;
};
Then, instead of creating a bunch of lists of values, you should create a list of nutrients.
std::vector<Nutrient> readNutrients(std::string input_file) {
// Here declare your vector:
std::vector<Nutrient> nutrients;
// declare line, calories, name...
while (std::getline(in, line)) {
// fill your variables name calories etc...
// create a nutrient
Nutrient n;
// fill the nutrient with values from the parsing.
n.name = name;
n.unit = units;
n.amount = amount;
n.calories = calories;
// add the nutrient to the list.
nutrients.push_back(n);
}
// return a filled list of nutrient.
return nutrients;
}
By the way, you don't need the num* variables, since nutrients.size() will return you the number of nutrients in the list.
That solution goes the same with recipes: Create a type to add the concept of a recipe in your program, and use that type.
Please note that this code is not optimal, std::move from C++11 should will grant you enormous speed up.
I don't understand your case clearly. But because you can't get result as return values of void function, it may get results by output arguments using pointers or refrence types.
for example:
void _read(const char* file, vector<string>& r_list, int* pState)
{
// do parsing file
// do outputs
*pState = (your_number);
r_list.push_back("your string");
}
Hope this is useful for you.

C++ formatted input must match value

I'm reading a file with C++; the file looks like:
tag1 2345
tag2 3425
tag3 3457
I would like to have something like
input>>must_be("tag1")>>var1>>must_be("tag2")>>var2>>must_be("tag3")>>var3;
Where everything blows up if what's being taken in doesn't match the argument of must_be() and, when done, var1=2345, var2=3425, var3=3457.
Is there a standard way of doing this? (Hopefully where "tag1" need not necessarily be a string, but this is not a requirement.) fscanf from C made it quite easy.
Thanks!
To clarify, each >> reads in one whitespace-delimited set of characters from input. I want to match some of the in-coming blocks of characters (tagX) against strings or data I have specified.
You need to implement operator>> for your class. Something like this :
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
struct A
{
A(const int tag_):tag(tag_),v(0){}
int tag;
int v;
};
#define ASSERT_CHECK( chk, err ) \
if ( !( chk ) ) \
throw std::string(err);
std::istream& operator>>( std::istream & is, A &a )
{
std::string tag;
is >> tag;
ASSERT_CHECK( tag.size() == 4, "tag size" );
std::stringstream ss(std::string(tag.begin()+3,tag.end()));
int tagVal;
ss >> tagVal;
std::cout<<"tag="<<tagVal<<" a.tag="<<a.tag<<std::endl;
ASSERT_CHECK( a.tag == tagVal,"tag value" );
is >> a.v;
return is;
}
int main() {
A a1(1);
A a2(2);
A a3(4);
try{
std::fstream f("in.txt" );
f >> a1 >> a2 >> a3;
}
catch(const std::string &e)
{
std::cout<<e<<std::endl;
}
std::cout<<"a1.v="<<a1.v<<std::endl;
std::cout<<"a2.v="<<a2.v<<std::endl;
std::cout<<"a3.v="<<a3.v<<std::endl;
}
Take a note that for wrong tag value, an exception will be thrown (meaning the tag much match).
Can't you read it line by line, and matching tags for each line? If the tag doesn't match what you expect you just skip the line and move on to the next.
Something like this:
const char *tags[] = {
"tag1",
"tag2",
"tag3",
};
int current_tag = 0; // tag1
const int tag_count = 3; // number of entries in the tags array
std::map<std::string, int> values;
std::string line;
while (current_tag < tag_count && std::getline(input, line))
{
std::istringstream is(line);
std::string tag;
int value;
is >> tag >> value;
if (tag == tags[current_tag])
values[tag] = value;
// else skip line (print error message perhaps?)
current_tag++;
}