Notes: • The solution should be general to processing any file with the same sch
ID: 3818287 • Letter: N
Question
Notes:
• The solution should be general to processing any file with the same schema, not only
applicable to the given file example.
• The program will be tested with data files with the same schema.
Assignment Marking Scheme
: • Program correctness (75%)
• Program indentation and readability (5%)
• Choice of significant names for identifiers (5%)
• Comments - description of variables and constants (5%)
/// MY IMPLEMENTATION OF THE CODE BUT I DONT KNOW WHAT I AM DOING WRONG NOTING IS OUTPUTTING :( !!!! THE CODE IS IN C ++ CAN SOMEONE PLEASE HELP ME BY DOING THE CORRECT IMPLEMENTATION OF THE CODE BY THE GIVEN GUIDELINE CAUSE DONT KNOW WHAT IM DOING WRONG
///// DataStorage.h///
class DataStorage{
private: //Pointers' data members
int *iPtr; //Private data member: pointer of type integer
float *fPtr; //Private data member: pointer of type float
char *cPtr; //Private data member: pointer of type character
// Date *dPtr;
int dSize;
public: //Member functions
DataStorage(); //Constructor
void setIntPtr(int*); //Member function that sets pointer as an integer type
void setFloatPtr(float*); //Member function that sets pointer as a float type
void setCharPtr(char*); //Member function that sets pointer as a character type
int* getIntPtr(); //Accessing function that returns a pointer of type integer
float* getFloatPtr(); //Accessing function that returns a pointer of type float
char* getCharPtr(); //Accessing function that returns a pointer of type character
};
//// DataStorage. cpp///
#include
#include
#include "DataStorage.h"
//Constructor of the class DataStorage
DataStorage::DataStorage()
{
}
//end of constructor
//Member function that sets pointer as an integer type
void DataStorage::setIntPtr(int* intPtr)
{
iPtr = intPtr;
fPtr = 0;
cPtr = 0;
}
//end of setIntPtr function
//Member function that sets pointer as a float type
void DataStorage::setFloatPtr(float* floatPtr)
{
iPtr = 0;
fPtr = floatPtr;
cPtr = 0;
}
//end of setFloatPtr function
//Member function that sets pointer as a character type
void DataStorage::setCharPtr(char* charPtr)
{
iPtr = 0;
fPtr = 0;
cPtr = charPtr;
}
//end of setCharPtr function
//Accessing function that returns a pointer of type integer
int* DataStorage::getIntPtr()
{
return iPtr;
}
//end of getIntPtr function
//Accessing function that returns a pointer of type float
float* DataStorage::getFloatPtr()
{
return fPtr;
}
//end of getFloatPtr function
//Accessing function that returns a pointer of type character
char* DataStorage::getCharPtr()
{
return cPtr;
}
//end of getCharPtr function
///// driver.cpp///
#include "DataStorage.h"
#include
#include
#include
using namespace std;
void readFile(char*, DataStorage*& dsPtr, int* size) {
ifstream inFile;
inFile.open("G:\config.dat");
// exit program if ifstream could not open file
if (!inFile) {
cout << "File could not be opened" << endl;
exit(1);
}
char line[200];
int numLines=0;
inFile.unsetf(std::ios_base::skipws);
// find out how many lines the file has
unsigned line_count = std::count(std::istream_iterator(inFile),std::istream_iterator(),' ');
dsPtr = new DataStorage[line_count + 1];
inFile.clear();
inFile.setf(std::ios_base::skipws);
inFile.seekg(0);
int totalLine = 0;
//Wrting the exception code
try{
while (!inFile.eof())
{
char dataType[10];
int dSize;
inFile >> dataType;
inFile >> dSize;
if (strcmp(dataType, "int") == 0)
{
int *iPtr = new int[dSize+1];
iPtr[0] = dSize;
for (int i = 1; i <= dSize; i++)
inFile >> iPtr[i];
dsPtr[totalLine++].setIntPtr(iPtr);
}
else if (strcmp(dataType, "float") == 0)
{
float *fPtr = new float[dSize+1];
fPtr[0] = dSize;
for (int i = 1; i <= dSize; i++)
inFile >> fPtr[i];
dsPtr[totalLine++].setFloatPtr(fPtr);
}
else if (strcmp(dataType, "char") == 0)
{
char *cPtr = new char[dSize+1];
cPtr[0] = dSize;
for (int i = 1; i <= dSize; i++)
inFile >> cPtr[i];
dsPtr[totalLine++].setCharPtr(cPtr);
}
}
*size = totalLine;
}
//Trying to catch the exception
catch (exception e)
{
cout << "do sth" << endl;
}
inFile.close();
}
//Start testing DataStorage function
void main()
{
DataStorage* dsPtr;
int numLines=0;
readFile("G:\config.dat", dsPtr,&numLines);
for (int i = 0; i < numLines; i++)
{
if (dsPtr[i].getCharPtr())
{
char* ptr = dsPtr[i].getCharPtr();
for (int j = 1; j <= (int)ptr[0]; j++)
cout << ptr[j] << ' ';
}
else if (dsPtr[i].getFloatPtr())
{
float* ptr = dsPtr[i].getFloatPtr();
for (int j = 1; j <= (int)ptr[0]; j++)
cout << ptr[j] << ' ';
}
else
{
int* ptr = dsPtr[i].getIntPtr();
for (int j = 1; j <= (int)ptr[0]; j++)
cout << ptr[j] << ' ';
}
cout << endl;
}
system("pause");
}
//end of the main function
///config.dat///
int 12 1 9 1 8 6 3 4 3 9 7 0
float 2 5.30 56.31
chat 6 h a y K o z
float 3 5.55 22.41 10.11
///reverse.dat////
0 7 9 3 4 3 6 8 1 9 1 12 int
56.31 5.30 2 float
z o K y a h 6 chat
10.11 22.41 5.55 3 float
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <sstream>
using namespace std;
class DataStorage{
private: //Pointers' data members
int *iPtr; //Private data member: pointer of type integer
float *fPtr; //Private data member: pointer of type float
char *cPtr; //Private data member: pointer of type character
// Date *dPtr;
int dSize;
public: //Member functions
DataStorage(); //Constructor
void setIntPtr(int*); //Member function that sets pointer as an integer type
void setFloatPtr(float*); //Member function that sets pointer as a float type
void setCharPtr(char*); //Member function that sets pointer as a character type
int* getIntPtr(); //Accessing function that returns a pointer of type integer
float* getFloatPtr(); //Accessing function that returns a pointer of type float
char* getCharPtr(); //Accessing function that returns a pointer of type character
};
//Constructor of the class DataStorage
DataStorage::DataStorage()
{
}
//end of constructor
//Member function that sets pointer as an integer type
void DataStorage::setIntPtr(int* intPtr)
{
iPtr = intPtr;
fPtr = 0;
cPtr = 0;
}
//end of setIntPtr function
//Member function that sets pointer as a float type
void DataStorage::setFloatPtr(float* floatPtr)
{
iPtr = 0;
fPtr = floatPtr;
cPtr = 0;
}
//end of setFloatPtr function
//Member function that sets pointer as a character type
void DataStorage::setCharPtr(char* charPtr)
{
iPtr = 0;
fPtr = 0;
cPtr = charPtr;
}
//end of setCharPtr function
//Accessing function that returns a pointer of type integer
int* DataStorage::getIntPtr()
{
return iPtr;
}
//end of getIntPtr function
//Accessing function that returns a pointer of type float
float* DataStorage::getFloatPtr()
{
return fPtr;
}
//end of getFloatPtr function
//Accessing function that returns a pointer of type character
char* DataStorage::getCharPtr()
{
return cPtr;
}
void readFile(DataStorage*& dsPtr, int* size) {
ifstream inFile;
inFile.open("config.dat");
// exit program if ifstream could not open file
if (!inFile) {
cout << "File could not be opened" << endl;
exit(1);
}
char line[200];
int numLines=0;
inFile.unsetf(std::ios_base::skipws);
// find out how many lines the file has
unsigned line_count = count(std::istream_iterator<char>(inFile),std::istream_iterator<char>(),' ');
cout << line_count << endl;
dsPtr = new DataStorage[line_count + 1];
inFile.clear();
inFile.setf(std::ios_base::skipws);
inFile.seekg(0);
int totalLine = 0;
//Wrting the exception code
try{
while (!inFile.eof())
{
char dataType[10];
int dSize;
inFile >> dataType;
inFile >> dSize;
// cout << dSize << dataType << endl;
if (strcmp(dataType, "int") == 0)
{
int *iPtr = new int[dSize+1];
iPtr[0] = dSize;
for (int i = 1; i <= dSize; i++)
inFile >> iPtr[i];
dsPtr[totalLine++].setIntPtr(iPtr);
}
else if (strcmp(dataType, "float") == 0)
{
float *fPtr = new float[dSize+1];
fPtr[0] = dSize;
for (int i = 1; i <= dSize; i++)
inFile >> fPtr[i];
dsPtr[totalLine++].setFloatPtr(fPtr);
}
else if (strcmp(dataType, "char") == 0)
{
char *cPtr = new char[dSize+1];
cPtr[0] = dSize;
for (int i = 1; i <= dSize; i++)
inFile >> cPtr[i];
dsPtr[totalLine++].setCharPtr(cPtr);
}
if(totalLine == line_count){
break;
}
}
*size = totalLine;
}
//Trying to catch the exception
catch (exception e)
{
cout << "do sth" << endl;
}
inFile.close();
}
//Start testing DataStorage function
int main()
{
DataStorage* dsPtr;
int numLines=0;
readFile(dsPtr,&numLines);
ofstream file("output.dat");
for (int i = 0; i < numLines; i++)
{
if (dsPtr[i].getCharPtr())
{
char* ptr = dsPtr[i].getCharPtr();
for (int j = (int)ptr[0]; j >= 0 ; j--){
file << ptr[j];
cout << ptr[j] << ' ';
}
}
else if (dsPtr[i].getFloatPtr())
{
float* ptr = dsPtr[i].getFloatPtr();
for (int j = (int)ptr[0]; j >= 0 ; j--){
file << ptr[j];
cout << ptr[j] << ' ';
}
}
else
{
cout << "Correct data " << endl;
int* ptr = dsPtr[i].getIntPtr();
for (int j = 1 ; j < (int)ptr[0] ; j++){
cout << ptr[j] << ' ';
}
cout << endl << "reverse String" << endl;
for (int j = (int)ptr[0]; j >= 0 ; j--){
file << ptr[j];
cout << ptr[j] << ' ';
}
}
cout << endl;
file.close();
}
return 0;
}
description :
-----------------
1. while(inFile.eof()) loop is not getting stoped so applied check. we maintain the total line numbers in file so after reading number of line, i have used break statement so that loop can be termincated after reading that much line.
2. file write fuctioanlity was missing so modified accordingly.
Please let me know if face any challanges to run the above code.
output
------------
3
Correct data
1 9 1 8 6 3 4 3 9 7 0
reverse String
0 0 7 9 3 4 3 6 8 1 9 1 12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.