Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

C++ Programming:

How would you write a function that takes a filename and an array of struct time, opens a file, reads each line in the file as the number of days, converts this to a struct time and stores this is in an array?

Here is my code so far (p.s. the function i need help with is called void readData):

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<vector>

using namespace std;

struct time{
// private:
int years, months, days;
//public:
time(){}
time(int days){

}

};

time getYearsMonthsDays(int days){
time x;
x.years = days/450;
days = days%450;
x.months = days/30;
x.days = days%30;
return x;

}

int getTotalDays(time x){
int totalDays;
totalDays = x.years*450 + x.months*30 + x.days;
return totalDays;
}

void openofile(string ofilename,ofstream &fout){
fout.open(ofilename.c_str());
if(!fout){
cout << "Error opening output file..." << endl;
exit(1);
}
}

void openifile(string ifilename,ifstream &fin){
fin.open(ifilename.c_str());
if(!fin){
cout << "Error opening input file..." << endl;
exit(1);
}
}

void readData(vector <time> &myTimeVector, ifstream &fin){
string ifilename; // takes a file name
int days;
time xArray[days]; // takes an array of struct time
fin.open(ifilename.c_str()); // opens the file
while (!fin.eof()){
fin >> days; // reads each line as the number of days
time x = time(days); // converts this to a struct time
myTimeVector.push_back(x); // and stores this in the array
}
fin.close();
}

bool compare(time a, time b){
if (a.days < b.days)
return true;
else
return false;
}

void swap(time &a, time &b){
time temp;
temp = a;
a = b;
b = temp;
}

void sort(vector <time> &myTimeVector){
for (int i = 0; i < myTimeVector.size(); i++)
for (int j = i+1; j < myTimeVector.size(); j++)
if (compare(myTimeVector[i], myTimeVector[j]))
swap (myTimeVector[i], myTimeVector[j]);
}

void printData(vector <time> myTimeVector, ostream &out){
time x;
for (int i = 0; i < myTimeVector.size(); i++){
out << x.years << "\t" << x.months << "\t" << x.days << endl;
}
}

int main(){

time x;
string ifilename1 = "/home/ec2-user/environment/timedata.txt";
string ofilename1 = "/home/ec2-user/environment/timeoutput.txt";
vector<time> myTimeVector;
ifstream fin;
ofstream fout;

openifile(ifilename1,fin);
openofile(ofilename1,fout);
readData(myTimeVector,fin);
fin.close();
printData(myTimeVector,cout);
printData(myTimeVector,fout);
cout << "========================"<<endl;
fout << "========================"<<endl;
sort(myTimeVector);
printData(myTimeVector,cout);
printData(myTimeVector,fout);
fout.close();
return 0;
}

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education