MY IMPLEMENTATION OF THE CODE LISTED BELOW BUT It\'s NOT OUTPUTTING CORRECTLY IT
ID: 3819615 • Letter: M
Question
MY IMPLEMENTATION OF THE CODE LISTED BELOW BUT It's NOT OUTPUTTING CORRECTLY
ITS SUPPOSED TO OUTPUT THIS BY READING FROM A FILE CALLED CONFIG.DAT AS STATED BY THE GUIDLINe:
int 12 1 9 1 8 6 3 4 3 9 7 0
float 2 5.30 56.31
char 6 h a y K o z
float 3 5.55 22.41 10.11
BUT FOR SOME REASON THIS IS MY OUTPUT
//////HERE IS MY CODE/////////////////////
//DataStorage.cpp////////////////////////////////
#include <iostream>
#include <fstream>
#include <iterator>
#include <sstream>
#include <string>
using namespace std;
void readFile( char* fileName) {
ofstream file;
file.open("reverse.dat", std::ios_base::app);
ifstream inFile (fileName);
// inFile.open("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;
// cout<<"int 12 1 9 1 8 6 3 4 3 9 7 0"<<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] <<" ";
}
cout<<file <<endl;
// cout<<"float 2 5.30 56.31"<<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] <<" ";
}
// cout<<"char 6 h a y K o z"<<endl;
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 = "/Users/fatema/Desktop/config.dat" ;
readFile(fileName);
// 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
char 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 char
10.11 22.41 5.55 3 float
Explanation / Answer
Your input file is wrong.. Notice the first line.. it says.. int 12.. so there should be 12 integers.. while there are only 11 integers after 12.. Hence the error is coming.. I have changed your code a bit.. to provide you the expected output:
#include <iostream>
#include <fstream>
#include <iterator>
#include <sstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
void readFile(char* fileName) {
ofstream file;
file.open("reverse.dat");
ifstream inFile (fileName);
// inFile.open("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;
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 << dSize <<" " << dataType << " " ;
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 << dSize <<" " << dataType << " " ;
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 << dSize <<" " << dataType << " " ;
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 = "input.txt" ;
readFile(fileName);
}
// I have taken a input.txt file in the same directory which is as below:
int 11 1 9 1 8 6 3 4 3 9 7 0
float 2 5.30 56.31
char 6 h a y K o z
float 3 5.55 22.41 10.11
Output:
0 7 9 3 4 3 6 8 1 9 1 11 int
56.31 5.3 2 float
z o K y a h 6 char
10.11 22.41 5.55 3 float
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.