Easy Program: Store Data from a file into a Struct Array - c++

I am new to programming and I am having trouble reading data from a file and entering it into a struct array, while keeping track of each data being entered:
The file would contain:
Name, ID Number, and GPA
Courtney Love 1234569 3.5
Bob Joe 1234570 3.0
Dave Henry 1234571 2.9
struct Student
{
string name;
int id;
float GPA;
void printStudent();
};
Declare an array of Student type that can hold up to 5 members:
Student a_members[5];
Open the file, read in each line and store the data in the array, keep track of each student read in:
fstream file_;
file_.open ("students.txt");
if(file_.is_open())
{
while(file_.good())
{
}
}
else
{
cout << "File is not open"<< endl;
}
return 0;
I am stuck on the "while" conditional statement. After that I don't know what I should do to input the data from the file line by line and place into the "struct array". As of right now, I feel like I have tried everything! I deleted everything and figured it was best to start over. It was becoming too complicated! Maybe I am just not understanding the concept. If anyone can point me in the right direction, please do so! Thank you!

You should not use good(), just like you should not use eof().
(Neither is used in any decent beginner-level material, yet every beginner manages to find them. And then they wonder why it didn't work.)
You should instead rely on the fact that a stream itself is "true-ish" if it's in a good state, and just keep reading until it isn't.
Idiomatic C++ would look like this:
std::ifstream file("students.txt");
Student s;
while (file >> s.name >> s.id >> s.GPA)
{
// Process the student
}
or, a fancy version:
std::istream& operator>> (std::istream& is, Student& s)
{
return is >> s.name >> s.id >> s.GPA;
}
std::ifstream file("students.txt");
Student s;
while (file >> s)
{
// Process the student
}
(In your code, you'll need to also keep track of how many Students you've read.)

Here is one from possible solutions:
#include <iostream>
#include <vector>
#include <fstream>
struct Student
{
Student() : first_name(), surname(){}
char first_name[64];
char surname[64];
int id;
float GPA;
void printStudent()
{
std::cout << "Name: " << first_name << " " << surname << " ID: " << id << " GPA: " << this->GPA << std::endl;
}
};
std::vector<Student>student;
bool LoadFile(const char* filename)
{
if (filename == NULL)return false;
std::fstream stream(filename, std::ios::in);
if (!stream.is_open())return false;
else
{
char buffer[255]; // for solution 1!
while (!stream.eof())
{
memset(buffer, 0, sizeof(buffer));
Student _student;
#pragma region SOLUTION_1
//stream.getline(buffer, sizeof(buffer));
//sscanf(buffer, "%s %s %d %f", _student.first_name, _student.surname, &_student.id, &_student.GPA);
#pragma endregion
#pragma region SOLUTION_2
stream >> _student.first_name >> _student.surname >> _student.id >>_student.GPA;
#pragma endregion
student.push_back(_student);
student[student.size() - 1].printStudent();
}
}
return true;
}
int main()
{
LoadFile("students.txt");
getchar();
return 0;
}

Related

Reading file data to structure

For a homework assignment, I am supposed to create a program that uses structures to hold an artists' discography information; i.e. name, start year, end year, genre(this can be more than one) and albums with their release year(which I interpreted to mean a second structure). All this information should be read from a file.
I'm having a little bit of trouble reading data from the file into the structure variables. My main issue is that a couple of my struct variables can hold multiple strings and I am confused as to how to read them into the variables as separate values. I've tried using 2D arrays, but that doesn't seem to work, so I think that vectors might be the way to go, but that seems to be throwing up issues as well. The file is formatted as such:
artist name
artist genres;....;...
start year
end year
number of albums
album name, album year; ..., ...;
So far this is the code that I have written:
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Discography {
vector<string> trackTitle;
vector<int> trackYear;
};
struct Artist {
string artistName;
vector<string> genres;
int startingYear;
int endingYear;
int numOfTracks;
struct Discography disc;
};
istream& operator>>(istream& input, Artist& artist)
{
input >> artist.artistName;
//input >> artist.genres;
input >> artist.startingYear;
input >> artist.endingYear;
input >> artist.numOfTracks;
//input >> artist.disc.trackTitle;
//input >> artist.disc.trackYear;
return input;
}
void displayFileContents();
void searchByName();
void searchByGenre();
void searchBySong();
void searchByYear();
int main()
{
/*integer variable for menu selection*/
int selection;
vector<Artist> art;
do
{
cout << "\nPlease make a selection from one of"
<< "\nthe six options below.\n";
cout << "\n1. Display All Information\n";
cout << "2. Search by artist name\n";
cout << "3. Search by genre\n";
cout << "4. Search by song title\n";
cout << "5. Search by an active year\n";
cout << "6. Exit Program\n";
cout << "\nEnter your selection: ";
/*reading user menu selection*/
cin >> selection;
if (selection == 1) {
displayFileContents();
}
else if (selection == 2) {
searchByName();
}
else if (selection == 3) {
searchByGenre();
}
else if (selection == 4) {
searchBySong();
}
else if (selection == 5) {
searchByYear();
}
else if (selection == 6) {
cout << "Good Bye!";
}
else {
cout << "You did not enter a valid selection\n\n";
}
} while (selection != 6);
return 0;
}
void displayFileContents() {
ifstream artistFile;
artistFile.open("My Artists.txt");
Artist a[5];
if (!artistFile.is_open()) {
cout << "File could not open";
}
else
while (std::getline(artistFile, a->artistName)) {
std::cout << a->artistName << "\n";
}
}
void searchByName() {
}
void searchByGenre() {
}
void searchBySong() {
}
void searchByYear() {
}
The ultimate goal is to display the information and also to be able to search the info based on certain things like the year or artist name or genre. That part of the program I feel comfortable with, but the file reading is really stumping me. A nudge in the right direction would greatly appreciated. Thanks in advance.
I think your approach is good so far. In the struct Discography maybe you could use std::unordered_map<int, std::string> instead, but that's just my opinion. If you know in advance the number of input lines your data is composed of, I strongly recommend changing your istream& operator>>(istream& input, Artist& artist) to use std::getline:
std::istream& operator>>(std::istream& input, Artist& artist)
{
std::string line;
std::getline(input, line);
artist.artistName = line;
std::getline(input, line);
auto genres = splitString(line, ';');
artist.genres = genres;
std::getline(input, line);
artist.startingYear = std::stoi(line);
// etc ...
return input;
}
To implement std::vector<std::string> splitString(const std::string& str, char delim); you can use this answer. Then you can also think of all kinds of error handling like invalid integer conversion, empty line, not enough lines, etc.

How to read everything inside a double quoted string?

I'm quite new to C++. I'm writing a program that reads data from a file and puts it into an object. I'm doing this to try to understand the iostream better.
Each line of the file contains an alphanumeric ID in the format nxn, where n is some integer, and x is some character, a color, another integer, and then a location. Here's an example
17832X9647 "blue" 100 "New York City"
Unfortunately, the code that I've written doesn't get all of the final string, since it has spaces in it. So, I tried writing a function that would return all of the text inside double quotes (not including the double quotes) and putting it inside the input stream, while still preserving the whitespaces and discarding the double quotes. Unfortunately, that also did not work, so now I'm sitting here scratching my head, not knowing what to do, and it's because I fundamentally don't understand how the iostream works. What can I do to fix this? I would really, really not like to restructure my whole program, because I want to learn exactly how I'm treating the iostream wrong. Getting this thing to work in the most efficient way isn't important to me, just that I understand more about the iostream.
Here is my code.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string ReadDoubleString(istream & in)
{
string output_string;
char ch = '\"';
in.ignore(ch); //i'm thinking that this will make the extraction skip any double quotes
in >> output_string;
return output_string;
}
//begin class declaration and implementation for Widget
class Widget
{
friend istream& operator>>(istream&, Widget&);
public:
string ID;
string color;
int Age;
string location;
void setAttributes(string, string, int, string);
};
void Widget::setAttributes(string ID, string color, int Age, string location)
{
this->ID = ID;
this->color = color;
this->Age = Age;
this->location = location;
}
istream& operator>>(istream& in, Widget& output_widget)
{
int input_ID_pt1;
char input_ID_pt2;
int input_ID_pt3;
string full_ID;
string input_color;
int input_Age;
string input_location;
in >> input_ID_pt1 >> input_ID_pt2 >> input_ID_pt3;
input_color = ReadDoubleString(in);
in >> input_Age;
input_location = ReadDoubleString(in);
full_ID = to_string(input_ID_pt1) + input_ID_pt2 + to_string(input_ID_pt3);
output_widget.setAttributes(full_ID, input_color, input_Age, input_location);
return in;
}
//end class declaration and implementation for Widget
int main()
{
string file_name;
cout << "enter file name" << endl;
cin >> file_name;
ifstream newFile(file_name);
if (newFile.is_open())
{
Widget w1;
newFile >> w1;
cout << w1.ID << endl;
cout << w1.color << endl;
cout << w1.Age << endl;
cout << w1.location << endl;
}
else
{
cout << "could not open file!" << endl;
}
return 0;
}
My program ends up returning this when I use the data from earlier.
17832X9647
//empty line
0
//empty line
//empty line
Thank you so much for reading.

Using int alongside string and getline without errors

I'm creating a program that reads the author, title, and number of volumes from a file and prints out labels,
(ex.
Adams
A Complete History of the World
Volume 1 of 10
Adams
A Complete History of the World
Volume 2 of 10 etc.)
To make it read properly and not infinitely loop, I had to change all of my variable to string. However, for future reference to the volume number, I need it to be int so I can compare the amount.
My ideas for furthering the code with a do-while loop are commented in to show why I'd like vnum to have int value.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
string author;
string title;
string vnum;
int counter=1;
fin.open("publish.txt", ios::in);
while (!fin.eof())
{
getline(fin, author);
getline(fin, title);
getline(fin, vnum);
//do
//{
cout << author << endl;
cout << title << endl;
cout << "Volume " << counter << " of " << vnum << endl;
cout << endl;
//counter++;
//} while(counter < vnum);
}
fin.close();
return 0;
}
File I'm reading from:
Adams
A Complete History of the World
10
Samuels
My Life of Crime
2
Baum
Wizard Stories
6
First of all, avoid use of
while (!fin.eof())
See Why is “while ( !feof (file) )” always wrong? to understand the problems it would cause.
Coming to your task, I would suggest:
Create a struct to hold the data.
Add a function to read all the members of the struct from a std::istream.
Add a function to write all the members of the struct to a std::ostream.
Simplify main to use the above.
Here's what I suggest:
struct Book
{
std::string author;
std::string title;
int volume;
};
std::istream& operator>>(std::istream& in, Book& book);
std::ostream& operator<<(std::ostream& out, Book const& book);
That will help simplify main to:
int main()
{
ifstream fin;
Book book;
// Not sure why you would need this anymore.
int counter=1;
fin.open("publish.txt", ios::in);
while ( fin >> book )
{
cout << book;
++counter;
}
return 0;
}
The functions to read and write a Book can be:
std::istream& operator>>(std::istream& in, Book& book)
{
// Read the author
getline(in, book.author);
// Read the title
getline(in. book.title);
// Read the volume
in >> book.volume;
// Ignore rest of the line.
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return in;
}
std::ostream& operator<<(std::ostream& out, Book const& book)
{
out << book.author << std::endl;
out << book.title << std::endl;
out << book.volume << std::endl;
return out;
}

File to Array to Struct

I'm working on this program that is to help manage a DVD rental store. What I have to do is take a text that contains info about DVD etc:
Mean girls; comedy; PG; 2009; Regina George; 12.07.2015;
The Conjuring; Horror; R; 2013; Sara Johnson; 16.05.2016;
Pokemon 2000; Kids; G; 2000; Ash Katchem; 15.04.2016;
etc..
And then takes this information and then reads it into an array and from there the array is read into the struct and then displayed in proper order like so:
Name: Mean Girls
Genre: Comedy
Rating: PG
etc...
This is my code so far:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
struct dvd{
string name;
string genre;
string rating;
string released;
string renter;
string rentday;
void print();
bool read(ifstream & file);
};
void dvd::print(){
cout <<"Title: " << name << endl;
cout <<"Genre: " << genre << endl;
cout << "Rating: " << rating << endl;
cout << "Release date: " << released << endl;
cout << "Name of renter: " << renter << endl;
cout << "Date rented: " << rentday << endl;
}
bool dvd::read(ifstream & file)
{
getline(file, name, ';');
getline(file, genre, ';');
getline(file, rating,';');
getline(file, released, ';');
getline(file, renter, ';');
getline(file, rentday, ';');
return file.good();
}
int main() {
vector<dvd> dvds;
dvd dvd1;
ifstream file("DVD.txt");
if(! file.is_open()){
cout << "Failed to find input file" << endl;
return 1;
}
while(dvd1.read(file))
{ dvds.push_back(dvd1);
}
dvd1.print();
return 0;
}
So what I would like to do is have read the text file into the Array and from there read the Array into the struct. So instead of the text file reading into the vector I need it to read into the array and from there read the first line of the array (dvdArray[1]) into struct dvd and then print out that information using print function and then loop that until dvdArray[10] is read into struct dvd!
Thank you so much for your help! :)
Change
bool dvd::read(ifstream & file)
to
bool dvd::read(istream & file)
No other changes to its contents are required.
Then, take each line and put it into a std::istringstream, then pass it to dvd::read.
You should be able to figure out the rest on your own.
For simple reading from and writing to file, I would suggest overload << and >> for your struct class, in order to make the code easy to serialize and de-serialize in a readable fashion.
friend std::ostream& operator<< (std::ostream& stream, const dvd& dvdObj)
{
// your output stuff
// stream <<"Title: " << dvdObj.name << endl;
// ...
return stream;
}
friend std::istream& operator>> (std::istream& stream, dvd& dvdObj)
{
// your output stuff
// getline(stream, dvdObj.name, ';');
// ...
return stream;
}
Then,
// look for std::copy for reading directly into vector ... else
while( file >> dvd1 )
{
dvds.push_back(dvd1);
}
And,
for( const auto& dvd1: dvds )
{
std::cout << dvd1 ;
}

how to manipulate the txt file using C++ STL HOmework

I have a txt file that contains name, id number, mobilenumber, and location in comma separated line.
example
Robby, 7890,7788992356, 123 westminister
tom, 8820, 77882345, 124 kingston road
My task is to retrieve
Look up all of an employee's information by name.
Look up all of an employee's information by ID.
Add the information of an employee.
Update the information of an employee.
SO far I have read the file and stored the information in a vector. Code is shown below.
For tasks
1)Look up all of an employee's information by name. I will iterate in the vector and prints information containing the name . I will be able to do that
2) simialry in text file I will look for id and prints information about that.
BUT I am clueless about point 3 & 4.
I am posting my code below
void filter_text( vector<string> *words, string name)
{
vector<string>::iterator startIt = words->begin();
vector<string>::iterator endIt = words->end();
if( !name.size() )
std::cout << " no word to found for empty string ";
while( startIt != endIt)
{
string::size_type pos = 0;
while( (pos = (*startIt).find_first_of(name, pos) ) != string::npos)
std:cout <<" the name is " << *startIt<< end;
startIt++;
}
}
int main()
{
// to read a text file
std::string file_name;
std::cout << " please enter the file name to parse" ;
std::cin >> file_name;
//open text file for input
ifstream infile(file_name.c_str(), ios::in) ;
if(! infile)
{
std::cerr <<" failed to open file\n";
exit(-1);
}
vector<string> *lines_of_text = new vector<string>;
string textline;
while(getline(infile, textline, '\n'))
{
std::cout <<" line text:" << textline <<std::endl;
lines_of_text->push_back(textline);
}
filter_text( lines_of_text, "tony");
return 0;
}
#include <string>
#include <iostream>
#include <vector>
#include <stdexcept>
#include <fstream>
struct bird {
std::string name;
int weight;
int height;
};
bird& find_bird_by_name(std::vector<bird>& birds, const std::string& name) {
for(unsigned int i=0; i<birds.size(); ++i) {
if (birds[i].name == name)
return birds[i];
}
throw std::runtime_error("BIRD NOT FOUND");
}
bird& find_bird_by_weight(std::vector<bird>& birds, int weight) {
for(unsigned int i=0; i<birds.size(); ++i) {
if (birds[i].weight< weight)
return birds[i];
}
throw std::runtime_error("BIRD NOT FOUND");
}
int main() {
std::ifstream infile("birds.txt");
char comma;
bird newbird;
std::vector<bird> birds;
//load in all the birds
while (infile >> newbird.name >> comma >> newbird.weight >> comma >> newbird.height)
birds.push_back(newbird);
//find bird by name
bird& namebird = find_bird_by_name(birds, "Crow");
std::cout << "found " << namebird.name << '\n';
//find bird by weight
bird& weightbird = find_bird_by_weight(birds, 10);
std::cout << "found " << weightbird.name << '\n';
//add a bird
std::cout << "Bird name: ";
std::cin >> newbird.name;
std::cout << "Bird weight: ";
std::cin >> newbird.weight;
std::cout << "Bird height: ";
std::cin >> newbird.height;
birds.push_back(newbird);
//update a bird
bird& editbird = find_bird_by_name(birds, "Raven");
editbird.weight = 1000000;
return 0;
}
Obviously not employees, because that would make your homework too easy.
So, first off, I don't think you should store the information in a vector of strings. This kind of task totally calls for the use of a
struct employee {
int id;
std::string name;
std::string address;
//... more info
};
And storing instances of employees in an
std::vector<employee>
You see, using your strategy of storing the lines, searching for "westminster" would net me Robbie, as his line of text does include this substring, but his name isn't westminster at all. Storing the data in a vector of employee structs would eliminate this problem, and it'd make the whole thing a lot more, well, structured.
Of course you'd need to actually parse the file to get the info into the vector. I'd suggest using a strategy like:
while(getline(infile, textline, '\n')) {
std::stringstream l(textline);
getline(l,oneEmp.name, ','); //extract his name using getline
l >> oneEmp.id; //extract his id
//extract other fields from the stringstream as neccessary
employees.push_back(oneEmp);
}
As for adding information: when the user enters the data, just store it in your employees vector; and when you should need to update the file, you may simply overwrite the original data file with a new one by opening it for writing & dumping the data there (this is obviously a rather wasteful strategy, but it's fine for a school assignment (I suppose it's school assignment)).
Start by splitting the CSV line into separate fields and then populate a struct with this data
eg:
struct Employee
{
std::string name;
std::string id_number;
std::string mobilenumber;
std::string location;
};
std::vector<Employee> employees; // Note you dont need a pointer
Look at string methods find_first_of, substr and friends.