Reading a excel file in to struct c++ - c++

I want to read a excel file in to a structure.But the thing is it reads the excel columns as whole line.I need those data in columns one by one.Using fstreams I have a file opened that contains numerous columns
I tried it with the knowledge that I have.But It displays all the details as a whole column as i've shown below
Name : Name,Nick
Nick : name,Phone
Phone : number,Carrier,Address
Carrier : Yashodhara,Yash,711256677,Mobitel,"No.
Address : 29,Bollatha,Ganemulla"
-----------------------
Name : Madushani,Madu,711345678,Mobitel,"No.
Nick : 12,
Phone : Gampaha"
Carrier : Sadeepa,Sad,789002264,Hutch,"No.
Address : 123,
-----------------------
I tried this with the following code.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//structure for store contact details
struct contacts {
string name;
string nickName;
string phoneNumber;
string carrier;
string address;
};
int main() {
const int LIMIT = 10;
contacts limit[LIMIT];
ifstream file;
file.open("Contact.csv");
if (file) {
while (!(file.eof())) {
for (int i = 0; i <= 7; i++) {
file >> limit[i].name;
file >> limit[i].nickName;
file >> limit[i].phoneNumber;
file >> limit[i].carrier;
file >> limit[i].address;
}
}
}
else {
cout << "Error";
}
// Using the following to debug output
for (int i = 0; i < 7; i++) {
cout << "Name : " << limit[i].name << endl
<< "Nick : " << limit[i].nickName << endl
<< "Phone : " << limit[i].phoneNumber << endl
<< "Carrier : " << limit[i].carrier << endl
<< "Address : " << limit[i].address << endl
<< "-----------------------" << endl;
}
return 0;
}
I want to know how to read above details in to the structure in the proper order.

You can use std::getline()
string readString;
if (file) {
while (!(file.eof())) {
getline(file,readString); // Read the column headers
for (int i = 0; i <= 7; i++) {
getline(file,limit[i].name,','); // ',' is the separator
getline(file,limit[i].nickName,',');
getline(file,limit[i].phoneNumber,',');
getline(file,limit[i].carrier,',');
getline(file,limit[i].address); // Read until the end of the line
}
}
}
I have to mention that I didn't test it so it might need some arrangements.

Related

How to fill structs within a class from a file?

I have an assignment where we need to create a class object that has many different variables, one of which being a struct. I can't figure out how to fill the struct from my setter function. I've attached some codes snippets I've pulled out of my code. my count_file_line function returns an int value of however many lines are in a txt file. I'm also really new to coding and have been struggling so if it's an obvious answer, sorry
When I run the program and try to cout teachers[I].password from within the setter function, nothing shows up (the "Flag" does show up)
struct teacher{
int id;
string password;
string first_name;
string last_name;
};
void University::set_teachers(ifstream& inFile){
int amountOfTeachers = count_file_lines(inFile);
this->teachers = new teacher[amountOfTeachers];
for(int i = 0; i < amountOfTeachers; i++){
inFile >> teachers[i].password;
inFile >> teachers[i].first_name;
inFile >> teachers[i].last_name;
cout << "Flag" << endl;
}
}
What you're trying to accomplish is a "de-serialization" of a sequence of teacher objects.
You may be interested in:
Is it possible to serialize and deserialize a class in C++?
for some general-purpose solutions. Note those may (or may not) be a bit "heavy-weight" for what you need to achieve.
Example of using tell, don't ask:
#include <iostream>
using std::cout, std::cerr, std::endl;
#include <iomanip>
using std::setw, std::setfill;
#include <fstream>
using std::ifstream, std::istream; // std::ofstream;
#include <sstream>
using std::stringstream;
#include <string>
using std::string, std::to_string;
#include <cstdint>
#include <cassert>
// stub - this function implemented and tested elsewhere
int count_file_lines(ifstream& inFile)
{
if (!inFile.good())
cerr << "\n !infile.good()" << endl;
return 5; // for test purposes
}
struct teacher
{
private:
int id; // unique number in record order
string password;
string first_name;
string last_name;
static int ID; // init value below
// note: On my system each string is 32 bytes in this object,
// regardless of char count: the chars are in dynamic memory
public:
teacher() : id(++ID) // password, first_name, last_name
{ } // default init is empty string
~teacher() = default; // do nothing
void read(istream& inFile) // tell instance to read next record
{
inFile >> password;
inFile >> first_name;
inFile >> last_name;
}
void show()
{
cout << "\n show id:" << id
<< "\n pw :" << password
<< "\n fn :" << first_name
<< "\n ln :" << last_name
<< endl;
}
};
int teacher::ID = 0; // compute unique ID number for each record
And a demo of input and output (teacher::read(), teacher::show())
Note use of "stringstream ss;". It is filled using a for loop, and passed to each teacher object using "teacher.read()".
Then the teacher values are echo'd to output using "teacher.show()"
class F834_t
{
teacher* teachers = nullptr; // do not yet know how many
ifstream inFile; // declared, but not opened
uint amountOfTeachers = 0;
stringstream ss; // for debug / demo use
public:
// use default ctor, dtor
F834_t() = default;
~F834_t() = default;
int exec(int , char** )
{
// open infile to count lines
amountOfTeachers = static_cast<uint>(count_file_lines(inFile)); // use working func
cout << "\n teacher count: " << amountOfTeachers << "\n "; // echo
// init ss with 5 values
for (uint i=1; i<=amountOfTeachers; ++i)
ss << " pw" << i << " fn" << i << " ln" << i << " ";
cout << ss.str() << endl;
teachers = new teacher[amountOfTeachers]; // allocate space, invoke default ctor of each
assert(teachers);
cout << "\n teachers: " << setw(4) << sizeof(teachers) << " (pointer bytes)"
<< "\n a teacher: " << setw(4) << sizeof(teacher) << " (teacher bytes)"
<< "\n size of all: " << setw(4) << (amountOfTeachers * sizeof(teacher))
<< " ( " << setw(3) << sizeof(teacher) << " * " << setw(3) << amountOfTeachers << ')'
<< endl;
// reset stream to start of inFIle, maybe close/open inFile
for (uint i=0;i<amountOfTeachers; ++i)
{
assert(ss.good()); // (inFile.good());
teachers[i].read(ss); // (inFile); // tell the object to read the file
}
for (uint i=0;i<amountOfTeachers; ++i)
{
teachers[i].show(); // tell the object to show its contents
}
return 0;
}
}; // class F834_t
int main(int argc, char* argv[])
{
F834_t f834;
return f834.exec(argc, argv);
}
Output - note that a much simplified input stream is created on the fly, and is echo'd early in this output.
teacher count: 5
pw1 fn1 ln1 pw2 fn2 ln2 pw3 fn3 ln3 pw4 fn4 ln4 pw5 fn5 ln5
teachers: 8 (pointer bytes)
a teacher: 104 (teacher bytes)
size of all: 520 ( 104 * 5)
show id:1
pw :pw1
fn :fn1
ln :ln1
show id:2
pw :pw2
fn :fn2
ln :ln2
show id:3
pw :pw3
fn :fn3
ln :ln3
show id:4
pw :pw4
fn :fn4
ln :ln4
show id:5
pw :pw5
fn :fn5
ln :ln5

Cannot read a string from a binary file using fstream, it is showing weird symbols instead

I'm trying to read a .bin file that I have made that include two integers and one string inside a struct. The int shows fine, but somehow the string output shows weird symbols.
This is the write script:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct student{
int no;
string name;
int score;
};
int main(){
fstream myFile;
myFile.open("data.bin", ios::trunc | ios::out | ios::in | ios::binary);
student jay, brad;
jay.no = 100;
jay.name = "Jay";
jay.score = 95;
brad.no = 200;
brad.name = "Brad";
brad.score = 83;
myFile.write(reinterpret_cast<char*>(&jay),sizeof(student));
myFile.write(reinterpret_cast<char*>(&brad),sizeof(student));
myFile.close();
cin.get();
return 0;
}
and this is the read script:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct student{
int no;
string name;
int score;
};
int main(){
fstream myFile;
myFile.open("data.bin", ios::in | ios::binary);
student readFile;
myFile.seekp(1*sizeOf(student)); //I use this because I want only specific position
//to be shown, for example I put 1 to show only Brad
myFile.read(reinterpret_cast<char*>(&readFile),sizeof(student));
cout << "No : " << readFile.no << endl;
cout << "Name : " << readFile.name << endl;
cout << "Score: " << readFile.score << endl;
myFile.close();
cin.get();
return 0;
}
The result would be like this:
No : 200
Name : ñ∩K
Score: 83
The string showed "ñ∩K" instead of "Brad".
I tried not to use seekp, and using read twice:
myFile.read(reinterpret_cast<char*>(&readFile),sizeof(student));
cout << "No : " << readFile.no << endl;
cout << "Name : " << readFile.name << endl;
cout << "Score: " << readFile.score << endl;
myFile.read(reinterpret_cast<char*>(&readFile),sizeof(student));
cout << "No : " << readFile.no << endl;
cout << "Name : " << readFile.name << endl;
cout << "Score: " << readFile.score << endl;
The result would be:
No : 100
Name : Jay
Score: 95
No : 200
Name : ε#
Score: 83
As you can see, the first position shows "Jay" fine but the next one is not. Any idea what went wrong? I'm new to C++.
What you're writing to the file is not a string, but the internal structure of the std::string object. Likely that's a pointer. When you read it back in, the pointer will be pointing to something that isn't valid. You're lucky to get any output at all rather than a crash, or demons flying from your nostrils.

How to open and read a file based on user input c++

I have an assignment that wants me to write a program that reads a text file, then outputs a modified version of that file using a version of Ceasar cipher that shifts the characters of the file that the user calls based on the shift amount that they input. For example if my file reads "hi" and they shift it by 1 it should read "ij". But when I run the program it doesnt recognize when I call in1 or out1 and I just get an error message. Can anyone help me figure out what I'm doing wrong or offer any advice on how to move forward? Thank you in advance!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
using namespace std;
int main()
{
//Declare user variables
int shift, file1chars = 0;
char filename[25];
ifstream in1;
ofstream out1;
do
{
in1.clear(); //clear status flags
//Prompt user to enter name of input file and amount of shift
cout << "Please enter the name of the input file." << endl;
cout << "Filename: ";
cin >> filename;
//Open file name
in1.open(filename);
//Error message if no file
if (!in1)
cout << "That is not a valid file. Try again\n";
} while (!in1);
do
{
out1.clear(); //clear status flags
//prompt user to input out file
cout << "Please enter the name of the output file." << endl;
cout << "Filename: ";
cin >> filename;
out1.open(filename);
//Error message if no file
if (!out1)
cout << "That is not a valid file. Try again\n";
} while (!out1);
//write some code to the input file
in1 >> "hi"
//write the integers in a different order to the output file
out1 << //idk where to go from here to initiate shift
//prompt user to enter shift
cout << "Please intput the shift amount: ";
cin >> shift;
cout << "Processing complete" << endl;
//Call file (?)
//Tell user file input is complete and is now printing statistics
cout << "\nShifted input file Complete. Now printing statistics " << endl;
//Show statistics for file
cout << "\nStatistics for file: " << filename << endl;
cout << "------------------------------------------------";
//Show characters in file and stats before shift
cout << "\n\nTotal # of characters in file: " << file1chars << endl;
cout << "Statistics before shift: " << endl;
//Show file before shift
//Show user stats after shift
cout << "\nStatistics after shift: " << endl;
//File after shift
//Close files
out1.close();
in1.close();
return 0;
}
Instead of looking at your code for line by line to see where the problem(s) could be, I ask you to think about your requirements and write code that expresses the intent as closely as possible. Create functions that follow the intended functionality.
Get the input file name.
Get the output file name.
Read the contents of the input file.
Transform the contents of the input file to create the output string.
Write the output string to the output file.
In pseudo code,
int main()
{
infile = get_input_filename()
outfile = get_output_filename()
contents = get_file_contents(infile)
output = transform_input(contents)
write_output(outfile, output)
}
Convert that to C++ code:
// Declare the functions.
std::string get_input_filename();
std::string get_output_filename();
std::string get_file_contents(std::string const& infile)
std::string transform_input(std::string const& contents)j
void write_output(std::string const& outfile, std::string const& output);
// Use them in main.
int main()
{
std::string infile = get_input_filename();
std::string outfile = get_output_filename();
std::string contents = get_file_contents(infile);
std::string output = transform_input(contents);
write_output(outfile, output);
}
// Implement the functions.
std::string get_input_filename()
{
std::string filename;
cout << "Please enter the name of the input file." << endl;
cout << "Filename: ";
cin >> filename;
return filename;
}
std::string get_output_filename()
{
std::string filename;
cout << "Please enter the name of the output file." << endl;
cout << "Filename: ";
cin >> filename;
return filename;
}
std::string get_file_contents(std::string const& infile)
{
std::ifstream inf(infile);
std::string contents;
int c;
while ( (c = inf.get()) != EOF )
{
contents += c;
}
return contents;
}
std::string transform_input(std::string const& contents)
{
std::string res;
// Do the needful to transform contents to res.
return res;
}
void write_output(std::string const& outfile, std::string const& output)
{
std::ofstream outf(outfile);
outf.write(output.c_str(), output.size();
}
If you are able to use a class or struct and functions I would propose something like this:
main.cpp
#include <string>
#include <iostream>
#include <fstream>
#include "CaesarShift.h"
int main() {
std::string filename;
std::cout << "Please enter the name of the input file. ";
std::cin >> filename;
std::ifstream fileIn;
std::string text;
fileIn.open( filename );
if ( !fileIn.is_open() ) {
std::cout << "Failed to open file: " << filename << "." << std::endl;
}
fileIn >> text;
fileIn.close();
CaesarText caesarText;
caesarText.addText( text );
std::cout << "Contents of the Caesar Text before peforming a Caesar Shift:\n"
<< caesarText << std::endl;
int amount = 0;
std::cout << "Please enter the amount to shift the text ";
std::cin >> amount;
std::cout << "Now performing the Caesar Shift: " << std::endl;
caesarShift( caesarText, amount );
std::cout << "Caesar Text after performing a Caesar Shift:\n"
<< ceasarText << std::endl;
std::ofstream fileOut;
fileOut.open( std::string( "shifted_" + filename ) );
if ( !fileOut.is_open() ) {
std::cout << "Failed to open shifted_" << filename << std::endl;
}
// Uncomment to print original text to file otherwise only modified text will be printed.
// fileOut << caesarText.originalText() << std::endl;
fileOut << caesarText.shiftedText() << std::endl;
fileOut.close();
system( "PAUSE" );
return 0;
}
CaesarShift.h
#ifndef CAESAR_SHIFT_H
#define CAESAR_SHIFT_H
class CaesarText {
std::string _originalText;
std::string _shiftedText;
public:
caesarText() = default;
explicit CeasarText( const std::string& text ) :
_originalText( text ) {}
void addText( const std::string& text ) {
_originalText = text;
}
std::string originalText() const {
return _originalText;
}
std::string shiftedText() const {
return _shiftedText;
}
friend void caesarShift( caesarText& c, int amount );
friend std::ostream& operator<<( std::ostream& out, const caesarText& ceasarText );
};
#endif // !CAESAR_SHIFT_H
CaesarShift.cpp
#include "CaesarShift.h"
#include <string>
#include <iostream>
#include <algorithm>
// Overloaded ostream operator
std::ostream& operator<<( std::ostream& o, const CaesarText& c ) {
o << "Original Text: " << c._originalText << "\n";
o << "Shifted Text: " << c._shiftedText << "\n";
return o;
}
// public friend function (visible in main) not actually a part of the class
// but performs operations on it.
// To perform the Caesar Shift here are 3 possible variations of implementing the same task.
void caesarShift( Caesar Text& text, int amount ) {
// Bound amount to the number of characters in the alphabet
// the same value used in any of the three variations below.
amount %= 26;
// Older c++ style loop.
/*for ( std::size_t i = 0; i < text._originalText.length(); i++ ) {
char c = text._originalText[i] + amount;
text._shiftedText += c;
}*/
// Modern C++ style loop
/*for ( auto& c : text._originalText ) {
text._shiftedText += c + amount;
}*/
// std::transform( s1.begin, s1.end, back_inserter( s2 ), lamda as predicate );
/*std::transform( text._originalText.begin(), text._originalText.end(),
std::back_inserter( text._shiftedText ),
[amount]( unsigned char c ) -> unsigned char { return c + amount; }
);*/
}
As for the 3 different variations of performing the Caesar Shift you can just uncomment the appropriate block section.

How to get variables stored in class from different lines?

How to get program read every line from .txt file and store 3 variables per line in different place ? I don't understand how can I store different value in same class. One line works fine but what I have tried more doesn't work.
class Team
{
public:
string name;
string dificulty;
string section;
};
void GetTeamInfo(Team& ko);
int main()
{
Team ko;
GetTeamInfo(ko);
cout << ko.name << " ";
cout << ko.dificulty<< " ";
cout << ko.section<< " ";
system("PAUSE");
}
void GetTeamInfo(Team& ko, int & i)
{
ifstream fd;
fd.open("Team.txt");
if (fd.is_open())
{
for(int i = 0; i < 10 ; i ++)
{
fd >> ko.name;
fd >> ko.dificulty;
fd >> ko.section ;
}
}
else
{
std::cout << "Mistake can't open file 'Team.txt'\n";
}
}
Try this:
void GetTeamInfo(vector<Team>& kos)
{
ifstream fd;
fd.open("Team.txt");
if (fd.is_open())
{
while (!d.eof())
{
Team ko;
fd >> ko.name;
fd >> ko.dificulty;
fd >> ko.section;
kos.push_back(ko);
}
}
...
}
I suggest you use a std::vector, since you have a number of teams.
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class Team
{
public:
string name;
string dificulty;
string section;
};
void GetTeamInfo(vector<Team>& ko_v);
int main()
{
vector<Team> ko; // a vector of Teams
GetTeamInfo(ko); // read from file inside a vector
// print every team
for(unsigned int i = 0; i < ko.size(); ++i) {
cout << ko[i].name << " ";
cout << ko[i].dificulty<< " ";
cout << ko[i].section<< " ";
cout << "\n";
}
//system("PAUSE"); // don't use system()
return 0; // return 0 should be placed at the end of main
}
void GetTeamInfo(vector<Team>& ko_v) // you had an extra parameter here, no need to
{
ifstream fd;
fd.open("Team.txt");
if (fd.is_open()) // check if file is open
{
while (!fd.eof()) // while you have more to read
{
Team ko; // create a Team
fd >> ko.name; // read data
fd >> ko.dificulty;
fd >> ko.section;
ko_v.push_back(ko); // store that Team in the vector of teams
}
}
else
{
cout << "File not opened!\n";
}
}
Why not to use an array? You could use an array of course, but since this is C++, std::vector's usage is encouraged. Moreover, you don't have to worry about the number of the Teams you are going to read from the file. If you would have used an array, you should know apriori the number of the Teams, or dynamically allocate memory.
Why not use system(pause); ?
Just for a glance, I am modifying the example with the use of an array.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Team
{
public:
string name;
string dificulty;
string section;
};
void GetTeamInfo(Team* ko_ar, const int N);
int main()
{
const int N = 3; // number of teams in the file
Team ko[N]; // a vector of Teams
GetTeamInfo(ko, N); // read from file inside a vector
// print every team
for(int i = 0; i < N; ++i) {
cout << ko[i].name << " ";
cout << ko[i].dificulty<< " ";
cout << ko[i].section<< " ";
cout << "\n";
}
//system("PAUSE"); // don't use system()
return 0; // return 0 should be placed at the end of main
}
void GetTeamInfo(Team* ko_ar, const int N)
{
ifstream fd;
fd.open("Team.txt");
int i = 0;
if (fd.is_open()) // check if file is open
{
while (!fd.eof()) // while you have more to read
{
Team ko; // create a Team
fd >> ko.name; // read data
fd >> ko.dificulty;
fd >> ko.section;
if(i == N) {
cout << "Read more than " << N << " teams\n";
break;
}
ko_ar[i++] = ko; // store that Team in the vector of teams
}
}
else
{
cout << "File not opened!\n";
}
cout << "Read " << i << " teams\n";
}
Use a vector, here you have a full example(commented):
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Team
{
public:
// Adding a constructor.
Team(string name, string dificulty, string section):
name(name),
dificulty(dificulty),
section(section)
{}
string name;
string dificulty;
string section;
};
// Defining a convenience type;
typedef vector<Team> team_list_t;
// This function now receives a vector of teams.
void GetTeamInfo(team_list_t &tl);
int main()
{
team_list_t tl;
GetTeamInfo(tl);
for (vector<Team>::iterator it = tl.begin(); it != tl.end(); ++it)
cout << it->name << " " << it->dificulty << " " << it->section << endl;
// You can also ...
for (int i = 0; i < tl.size(); i++)
cout << tl[i].name << " " << tl[i].dificulty << " " << tl[i].section << endl;
}
void GetTeamInfo(team_list_t& tl)
{
ifstream fd;
fd.open("Team.txt");
if (fd.is_open())
{
// Define variables;
string name, dificulty, section;
// Read until EOF
while(fd >> name >> dificulty >> section)
{
// Add teams to the vector/list.
tl.push_back(Team(name, dificulty, section));
}
}
else
{
std::cout << "Mistake can't open file 'Team.txt'\n";
}
}

C++ read/write to binary file. on program exit it gives System.AccessViolationException

This C++ program is created using Visual Studio 2010. It's a group project that has everyone in class stumped.
The program initially starts fine and the user can run through the program and add items that are written out to file. the items are read back in and displayed. When the user is done, on the program exiting return 0; it gives me "An unhandled exception of type System.AccessViolationException occurred. Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
When this happens it opens up a file called utility here => for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter; *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter) (*_Pnext)->_Myproxy = 0.
I can fix this by replacing return 0; with exit(0);
I know it's not a real fix though and just a band-aid over a bullet hole that is causing this issue.
After fixing (used very loosely here) that, then running the program again, it attempts to load the data file from the file system. It reads and loads the 1st item into a vector correctly but when it goes back to the start of the loop we see the same exception pop up, An unhandled exception of type System.AccessViolationException occurred.
This is the first project we have worked on using fstream and binary i/o. We had worked through a similar program that was just reading and writing strings w/out any issues.I believe that the issue stems from something in the fileHandler class but am having a difficult time pinpointing what is causing this issue.Any advice/help is greatly appreciated.Here is the code.stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <string.h>
#include <string>
#include <vector>
#include <time.h>
Week2.cpp (the main file for the project)
//Week2.cpp *******************
#include "stdafx.h"
#include "fileHandler.h"
using namespace std;
using namespace System;
int main(array<System::String ^> ^args)
{
fileHandler theFile("store.pkl");
vector<item> itemStack = theFile.getFile();
cout << "SKU Name Dept Vendor Max Order onHand" << endl;
cout << "-------------------------------------------" << endl;
for (int i = 0; i < itemStack.size(); i++)
{
cout << itemStack[i].toString() << endl;
}
vector<item> newStack;
//prompt for input
bool doneEditing = false;
while(!doneEditing)
{
int A;
int E;
int F;
int G;
string B;
string C;
string D;
string tempString;
cout << "Enter item info:" << endl << "Item SKU: ";
cin >> A;
cout << endl << "Item Name: ";
cin >> B;
cout << endl << "Item Dept: ";
cin >> C;
cout << endl << "Vendor Name: ";
cin >> D;
cout << endl << "Max Number: ";
cin >> E;
cout << endl << "Reorder Number: ";
cin >> F;
cout << endl << "OnHand Number: ";
cin >> G;
cout << endl << "Done?? Y/N: ";
cin >> tempString;
cout << endl;
item tempItem = item(A, B, C, D, E, F, G);
newStack.push_back(tempItem);
if (tempString == "Y" || tempString == "y")
{
doneEditing = true;
}
}
cout << "Saving stack to file" << endl;
theFile.putFile(newStack);
cout << "Items written to file" << endl;
vector<item> newFileStack = theFile.getFile();
cout << "After reload: " << endl;
cout << "SKU Name Dept Vendor Max Order onHand" << endl;
cout << "-------------------------------------------" << endl;
for (int i = 0; i < newFileStack.size(); i++)
{
cout << newFileStack[i].toString() << endl;
}
cout << "Thank you for using the Awesome Grocery Inventory Application" << endl;
system("PAUSE");
/*return 0; this breaks with same error as
when reading in saved file after application restart
*/
exit(0);
}
item.h
using namespace std;
#pragma once
class item
{
public:
item();
item(int sku, string name, string dept, string vendor, int max, int reorder, int onhand);
~item(void);
string toString();
int ItemSKU() const;
void ItemSKU(int val);
string ItemName() const;
void ItemName(string val);
string VendorName() const;
void VendorName(string val);
int MaxNumb() const;
void MaxNumb(int val);
int ReorderNumb() const;
void ReorderNumb(int val);
int OnHandNumb() const;
void OnHandNumb(int val);
private:
int itemSKU;
string itemName;
string itemDept;
string vendorName;
int maxNumb;
int reorderNumb;
int onHandNumb;
};
item.cpp
#include "StdAfx.h"
#include "item.h"
using namespace std;
item::item()
{
};
item::item(int sku, string name, string dept, string vendor, int max, int reorder, int onhand)
{
itemSKU = sku;
itemName = name;
itemDept = dept;
vendorName = vendor;
maxNumb = max;
reorderNumb = reorder;
onHandNumb = onhand;
}
item::~item(void)
{
}
string item::toString()
{
stringstream ss;
ss << itemSKU << "\t" << itemName << "\t" << itemDept << "\t" << vendorName << "\t" << maxNumb << "\t" << reorderNumb << "\t" << onHandNumb;
string s = ss.str();
return s;
}
int item::ItemSKU() const { return itemSKU; }
void item::ItemSKU(int val) { itemSKU = val; }
string item::ItemName() const { return itemName; }
void item::ItemName(string val) { itemName = val; }
string item::VendorName() const { return vendorName; }
void item::VendorName(string val) { vendorName = val; }
int item::MaxNumb() const { return maxNumb; }
void item::MaxNumb(int val) { maxNumb = val; }
int item::ReorderNumb() const { return reorderNumb; }
void item::ReorderNumb(int val) { reorderNumb = val; }
int item::OnHandNumb() const { return onHandNumb; }
void item::OnHandNumb(int val) { onHandNumb = val; }
fileHandler.h
#include "item.h"
using namespace std;
#pragma once
class fileHandler
{
public:
fileHandler(string);
~fileHandler(void);
vector<item> getFile();
void putFile(vector<item> &);
private:
string theFileName;
};
fileHandler.cpp
#include "stdafx.h"
#include "fileHandler.h"
using namespace std;
fileHandler::fileHandler(string name)
{
theFileName = name.c_str();
}
fileHandler::~fileHandler(void)
{
}
vector<item> fileHandler::getFile()
{
ifstream inFile;
string fileLine;
vector<item> localStack;
inFile.open(theFileName, ios::in|ios::binary);
if (inFile)
{
cout << "Getting file..." << endl;
cout << endl;
// not working on initial load if file is present at start
inFile.seekg(0);
while(!inFile.eof())
{
item tempItem;
inFile.read(reinterpret_cast< char * >(&tempItem), sizeof(item));
localStack.push_back(tempItem);
cout << "item added to stack" << endl;
} //breaks from here after reading in 1 item from saved file on reopen
} else {
ofstream newFile;
newFile.open(theFileName, ios::out|ios::binary);
newFile.close();
cout << "Creating new file..." << endl;
cout << endl;
inFile.open(theFileName, ios::in|ios::binary);
}
inFile.clear();
inFile.close();
if (localStack.size() > 0)
{
//removes some dirty data from end of stack
localStack.pop_back();
}
return localStack;
}
void fileHandler::putFile( vector<item> &items )
{
ofstream outFile;
outFile.open(theFileName, ios::out|ios::binary);
if(!outFile)
{
cerr<<"File could not be created"<<endl;
system("pause");
exit(1);
}
for (int i = 0; i < items.size(); i++)
{
outFile.write(reinterpret_cast<const char *>(&items[i]), sizeof(item));
}
outFile.clear();
outFile.close();
}
You cannot perform binary I/O this way with an object that contains std::string members. A std::string contains pointer(s) to dynamically allocated memory for its actual contents. You need to perform some type of serialization instead. The usual suggestion is Boost serialization.