I want to read in data from a text file and output to a new file with some more data. All the data is transferred properly but just out of order. When I use the >>
operators (extractors) everything is in order correctly however the commas are left in. When I use getline
the commas are gone but the data is not in the right order.
Here's what's in the text file:
Astronomy, 34684, MoWed, 7:15pm-9:55pm, JC16
ComputerScience, 36822, MoWed, 9:00am-10:40am, E137
Calculus, 32700, MoTuTh, 11:00am-12:15am, CW134
ComputerOrganization, 45665, Th, 7:20pm-9:55pm, E137
here's what I get when using getline
Class: Astronomy
-Class ID: 34684
-Meeting Days: MoWed
-Class Time: 7:15pm-9:55pm
-Class Location: JC16
ComputerScience
Class: 36822
-Class ID: MoWed
-Meeting Days: 9:00am-10:40am
-Class Time: E137
Calculus
-Class Location: 32700
Class: MoTuTh
-Class ID: 11:00am-12:15am
-Meeting Days: CW134
ComputerOrganization
-Class Time: 45665
-Class Location: Th
Heres what I get when I use the extractors
Class: Astronomy,
-Class ID: 34684,
-Meeting Days: MoWed,
-Class Time: 7:15pm-9:55pm,
-Class Location: JC16
Class: ComputerScience,
-Class ID: 36822,
-Meeting Days: MoWed,
-Class Time: 9:00am-10:40am,
-Class Location: E137
Class: Calculus,
-Class ID: 32700,
-Meeting Days: MoTuTh,
-Class Time: 11:00am-12:15am,
-Class Location: CW134
Class: ComputerOrganization,
-Class ID: 45665,
-Meeting Days: Th,
-Class Time: 7:20pm-9:55pm,
-Class Location: E137
any tips? here's the code
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//Holds all the class information
struct Course{
string courseName;
string courseNum;
string courseDay;
string courseTime;
string courseLoc;
};
//Extracts data from the file with the course information
Course getInfo(ifstream &inFile);
//Creates a file with the data from 'getInfo'
void writeInfo(ofstream &outFile, Course);
int main(){
ifstream inFile; //link to input file
ofstream outFile; //link to output file
Course course; //holds all course info
inFile.open("Courses.txt"); //opens textfile
outFile.open("Courses.dat"); //creates new file
course = getInfo(inFile); //priming read
while (inFile) {
writeInfo(outFile, course); //write info to output file
course = getInfo(inFile); //get info from input file
}
inFile.close();
outFile.close();
}
Course getInfo(ifstream &inFile){
Course course;
getline(inFile, course.courseName, ',');
getline(inFile, course.courseNum, ',');
getline(inFile, course.courseDay, ',');
getline(inFile, course.courseTime, ',');
getline(inFile, course.courseLoc, ',');
// inFile >> course.courseName >> course.courseNum >> course.courseDay;
// inFile >> course.courseTime >> course.courseLoc;
return course;
}
void writeInfo(ofstream &outFile, Course course){
outFile << "Class: " << course.courseName << endl;
outFile << " -Class ID: " << course.courseNum << endl;
outFile << " -Meeting Days: " << course.courseDay << endl;
outFile << " -Class Time: " << course.courseTime << endl;
outFile << " -Class Location: " << course.courseLoc << endl;
}