Notes: • The solution should be general to processing any file with the same sch
ID: 3818750 • 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
//////DataFile.cpp////////////
#include <iostream>
#include <fstream>
#include <iterator>
#include <sstream>
#include <string>
using namespace std;
class DataFiles
{
public:
int *iPtr;
float *fPtr;
char *cPtr;
int dSize;
};
int main()
{
int dSize,i,j;
char dType[2000];
int numOfRec=0;
int count=0;
ifstream inputFile("/Users/fatema/Documents/Visual Studio 2012/Projects/DataStorage/DataStorage/config.dat",ios::in);
if(inputFile.eof())
{
cout<<"Error in opening config.data file ";
system("pause");
return 0;
}
while(!inputFile.eof())
{
inputFile.getline(dType,2000);
numOfRec++;
}
inputFile.close();
DataFiles *dSeq = new DataFiles[numOfRec];
ifstream file_op("/Users/fatema/Documents/Visual Studio 2012/Projects/DataStorage/DataStorage/config.dat",ios::in);
ofstream file_output("/Users/fatema/Desktop/reverse.dat",ios::out);
while(file_op>>dType)
{
file_op>>dSize;
dSeq[count].iPtr = (int *)NULL;
dSeq[count].fPtr = (float *)NULL;
dSeq[count].cPtr = (char *)NULL;
dSeq[count].dSize = dSize;
if(strcmp(dType,"int")==0)
{
dSeq[count].iPtr = new int [dSize];
for(i=0;i<dSize;i++)
{
file_op>>dSeq[count].iPtr[i];
}
}
else
if(strcmp(dType,"float")==0)
{
dSeq[count].fPtr = new float [dSize];
for(i=0;i<dSize;i++)
{
file_op>>dSeq[count].fPtr[i];
}
}
else
if(strcmp(dType,"char")==0)
{
dSeq[count].cPtr = new char [dSize];
for(i=0;i<dSize;i++)
{
file_op>>dSeq[count].cPtr[i];
}
}
count++;
}
file_op.close();
cout<<" Displaying record from file ";
for(i=0;i<count;i++)
{
if(dSeq[i].iPtr != NULL)
{
cout<<"RECORD "<<(i+1)<<" int ";
cout<<dSeq[i].dSize<<" ";
for(j=0;j<dSeq[i].dSize;j++)
{
cout<<dSeq[i].iPtr[j]<<" ";
}
}
if(dSeq[i].cPtr != NULL)
{
cout<<"RECORD "<<(i+1)<<" char ";
cout<<dSeq[i].dSize<<" ";
for(j=0;j<dSeq[i].dSize;j++)
{
cout<<dSeq[i].cPtr[j]<<" ";
}
}
if(dSeq[i].fPtr != NULL)
{
cout<<"RECORD "<<(i+1)<<" float ";
cout<<dSeq[i].dSize<<" ";
for(j=0;j<dSeq[i].dSize;j++)
{
cout<<dSeq[i].fPtr[j]<<" ";
}
}
cout<<endl;
}
if(file_output)
{
cout<<endl<<" Now open, 'reverse.dat' file for displaying each record in reverse order ";
for(i=0;i<count;i++)
{
if(dSeq[i].iPtr != NULL)
{
for(j=dSeq[i].dSize-1;j>=0;j--)
{
file_output<<dSeq[i].iPtr[j]<<" ";
}
file_output<<dSeq[i].dSize<<" ";
file_output<<" int ";
}
if(dSeq[i].cPtr != NULL)
{
for(j=dSeq[i].dSize-1;j>=0;j--)
{
file_output<<dSeq[i].cPtr[j]<<" ";
}
file_output<<dSeq[i].dSize<<" ";
file_output<<" char ";
}
if(dSeq[i].fPtr != NULL)
{
for(j=dSeq[i].dSize-1;j>=0;j--)
{
file_output<<dSeq[i].fPtr[j]<<" ";
}
file_output<<dSeq[i].dSize<<" ";
file_output<<" float ";
}
file_output<<endl;
}
cout<<endl;
file_output.close();
}
system("pause");
return 0;
}
//////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 <iomanip>
using namespace std;
const char * FOUT_NAME = "reverse.dat";
int main(int argc, char *argv[]) {
if (argc != 2) {
cout << "Usage: reverse.exe path/to/file/filename" << endl;
exit(1);
}
ifstream ifs(argv[1]);
ofstream ofs(FOUT_NAME);
if (!ifs) {
cout << "Error: Can't open input file - " << argv[1] << endl;
exit(1);
}
if (!ofs) {
cout << "Error: Can't open output file - " << FOUT_NAME << endl;
exit(1);
}
string dType;
size_t dSize;
while (ifs.good()) {
ifs >> dType >> dSize;
ofs << left << setw(4) << dType << ' ' << dSize << ' ';
if (dType == "int") {
int * iPtr = new int[dSize];
for ( size_t i = 0; i < dSize; ++i )
ifs >> iPtr[i];
for ( size_t i = 0; i < dSize; ++i )
ofs << iPtr[dSize-i-1] << " ";
delete[] iPtr;
} else if (dType == "float") {
float * fPtr = new float[dSize];
for ( size_t i = 0; i < dSize; ++i )
ifs >> fPtr[i];
for ( size_t i = 0; i < dSize; ++i )
ofs << fPtr[dSize-i-1] << " ";
delete[] fPtr;
} else if (dType == "char") {
char * cPtr = new char[dSize];
for ( size_t i = 0; i < dSize; ++i )
ifs >> cPtr[i];
for ( size_t i = 0; i < dSize; ++i )
ofs << cPtr[dSize-i-1] << " ";
delete[] cPtr;
} else {
cout << "Error: " << dType << " record type isn't supported" << endl;
exit(1);
}
ofs << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.