Notes: • The solution should be general to processing any file with the same sch
ID: 3818743 • 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.cpp///////
#include <iostream>
#include <fstream>
#include <iterator>
#include <sstream>
#include <string>
#include <string>
using namespace std;
void readFile( char* fileName) {
ofstream file;
file.open("reverse.dat", std::ios_base::app);
ifstream inFile ("config.dat");
// exit program if ifstream could not open file
if (!inFile) {
cout << "File could not be opened" << endl;
exit(1);
}
try{
while (!inFile.eof())
{
char dataType[10] = {""};
int dSize=0;
inFile >> dataType;
inFile >> dSize;
if(inFile.eof())
break;
file << dataType << " " << 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];
}
for (int i=dSize ; i > 0 ; i--)
{
file << iPtr[i] <<" ";
}
file <<endl;
}
else if (strcmp(dataType, "float") == 0)
{
float *fPtr = new float[dSize+1];
fPtr[0] = float(dSize);
for (int i = 1; i <= dSize; i++)
{
inFile >> fPtr[i];
}
for (int i=dSize ;i>0 ; i--)
{
file << fPtr[i] <<" ";
}
file <<endl;
}
else if (strcmp(dataType, "char") == 0)
{
char *cPtr = new char[dSize+1];
cPtr[0] = char(dSize);
for (int i = 1; i <= dSize; i++)
{
inFile >> cPtr[i];
}
for (int i=dSize ;i>0 ; i--)
{
file << cPtr[i] <<" ";
}
file <<endl;
}
else {
cout<<"Invalid input found!Please check your input file. ";
break;
}
}
}
catch (exception e)
{
cout << " exception found" << endl;
}
inFile.close();
file.close();
}
int main(int argc, char *argv[])
{
char* fileName = argv[1];
readFile("config.dat");
return 0;
system("pause");
}
//////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
your code is not excuting because of libraries.
system,exit functions are available in #include<cstdlib>
strings function are available in #include<cstring>
program code.
#include <iostream>
#include <fstream>
#include <iterator>
#include <sstream>
#include <cstring>
#include<cstdlib>
using namespace std;
void readFile( char* fileName) {
ofstream file;
file.open("reverse.dat", std::ios_base::app);
ifstream inFile ("config.dat");
// exit program if ifstream could not open file
if (!inFile) {
cout << "File could not be opened" << endl;
exit(1);
}
try{
while (!inFile.eof())
{
char dataType[10] = {""};
int dSize=0;
inFile >> dataType;
inFile >> dSize;
if(inFile.eof())
break;
file << dataType << " " << 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];
}
for (int i=dSize ; i > 0 ; i--)
{
file << iPtr[i] <<" ";
}
file <<endl;
}
else if (strcmp(dataType, "float") == 0)
{
float *fPtr = new float[dSize+1];
fPtr[0] = float(dSize);
for (int i = 1; i <= dSize; i++)
{
inFile >> fPtr[i];
}
for (int i=dSize ;i>0 ; i--)
{
file << fPtr[i] <<" ";
}
file <<endl;
}
else if (strcmp(dataType, "char") == 0)
{
char *cPtr = new char[dSize+1];
cPtr[0] = char(dSize);
for (int i = 1; i <= dSize; i++)
{
inFile >> cPtr[i];
}
for (int i=dSize ;i>0 ; i--)
{
file << cPtr[i] <<" ";
}
file <<endl;
}
else {
cout<<"Invalid input found!Please check your input file. ";
break;
}
}
}
catch (exception e)
{
cout << " exception found" << endl;
}
inFile.close();
file.close();
}
int main(int argc, char *argv[])
{
char* fileName = argv[1];
readFile("config.dat");
return 0;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.