C++ Level 2 - Assignment 9 Lab 9 - File IO The attached Text file (http://progra
ID: 3702290 • Letter: C
Question
C++ Level 2 - Assignment 9
Lab 9 - File IO
The attached Text file (http://programming.msjc.edu/cppii/labs/labsb/lab09/Numbers.txt). Links to an external site. contains both whole and floating point numbers. Of course they are in text format and not binary. Your task is to read and parse the numbers and separate them into two text files double.txt and integer.txt.
Specifics
Open the text file and read its contents one line at a time. Determine if the line read from the file is a double or an integer. You are to place the integers in a vector called iNumbers and the doubles in a vector called dNumbers. The vector iNumbers should hold pointers to the Integer class and dNumbers should hold pointers to the Double class. When you add a value to one of the vectors you should use new and call the constructor that takes a string. For example:
iNumbers.push_back(new Integer("12.23"));
Once you have the file parsed create an overloaded writeNumbers function. One of these functions should take a vector of pointers to the Integer class, the other should take a vector of pointers to the Double class. These functions should write the contents of the vector to the appropriate text file. When you are finished you should have two files one that holds int values, the other that holds double values.
Note:
You created a toString function and added to your classes. You should use this function to write the string to the text file.
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
// Convert integer to string
string toString(int num) {
stringstream ss;
ss << num;
return ss.str();
}
string toString(float num) {
stringstream ss;
ss << num;
return ss.str();
}
// write the float vector to File
int writeToFile(vector <float*> dNumbers) {
//writing Double Vector to file
ofstream outf;
outf.open("double.txt");
if (!outf) {
cout << "Cannot open output file. ";
return 1;
}
//cout << "Double Vector";
for (vector<float*>::const_iterator iter = dNumbers.begin(); iter != dNumbers.end(); ++iter) {
outf << toString(**iter) << endl;
}
outf.close();
return 0;
}
// write int vector to file
int writeToFile(vector <int*> iNumbers) {
//writing Double Vector to file
ofstream outf2;
outf2.open("integer.txt");
if (!outf2) {
cout << "Cannot open output file. ";
return 1;
}
//cout << "Integer Vector";
for (vector<int*>::const_iterator iter = iNumbers.begin(); iter != iNumbers.end(); ++iter) {
outf2 << toString(**iter) << endl;
}
outf2.close();
return 0;
}
int main() {
vector<int*> iNumbers;
vector<float*> dNumbers;
char data[100];
//string data;
// Opening input file to read
fstream ifile;
ifile.open("numbers.txt");
if (!ifile) {
cout << "Cannot open output file. ";
return 1;
}
while(!ifile.eof())
{
ifile.getline(data, 100);
//cout << data << endl;
if (strstr(data, ".")) // If there is . in string it is float.
{
//cout << "float";
dNumbers.push_back(new float(atof(data)));
}
else {
//cout << "integer";
iNumbers.push_back(new int(atoi(data)));
}
}
// write vectors to file
writeToFile(iNumbers);
writeToFile(dNumbers);
ifile.close();
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.