problem with sequential function

Hi, I was asked to write a function that reads from a sequential file and writes to another sequential file. Here is a description of what was given. The file path and name are passed through arguments of the main function. The function must be written using different files such as 'config.dat' an 'reverse.dat'. The real question will be posted after the explanation of the details that eed to be included within the function.

*******************

The information below is some information about how the function works and are some details about what needs to be included from the function.

int main(int argc, char *argv[])

For example, the file name is ‘config.dat’ and the path is the current directory as shown from the schema of the file shown below:

The function should read the file, one record at a time (from table). Each record has exactly 3 fields. The first field (call it: dType) contains the string: int, float or char. The second field (call it: dSize) contains an integer number. This integer is equal to the number of elements in the third field. The third field (call it: dSeq) contains a dSize number of dType elements (e.g. 12 integers, as shown below).

int 12 1 9 1 8 6 3 4 3 9 7 0

Each record can be stored into one of the following arrays depending on the type of the field,

int *iPtr;
float *fPtr;
char *cPtr;
Date *dPtr;

The function must be able to basically read the table record by record. Each record must then have its data reverse its order

As a last step, it is needed to write out each reversed record to a file named “reverse.dat”. For example, the above example should be written as

int 12 0 7 9 3 4 3 6 8 1 9 1
float 2 56.31 5.30
chat 6 z o K y a h
float 3 10.11 22.41 5.55
Date 2 2015-12-31 2016-03-21
int 12 1 9 1 8 6 3 4 3 9 7 0

*********************

so far this is what i have done :

******the "config.dat" file has the following code

int 12 1 9 1 9 1 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

******the date.h file has the following code :

#ifndef DATE_H
#define DATE_H

class Date{
public:
Date(); //Main constructor
Date(const Date&); //Copy constructor
void setDate(int, int, int); //sets the date
/* accessing functions for the day, month and year */
int getDay(); //returns the day
int getMonth(); //returns the month
int getYear(); //returns the year
void printDate(); //prints the date
~Date(); //destructor

private:
/*Data members of class Date*/
int day;
int month;
int year;
};
#endif

******* the main.cpp file has the following code(reversing was done here also):

#include <iostream>
#include <fstream>
#include <string>
#include "date.h" // for values of type date
using namespace std;


//Read Function to Read a File and reverse it's contents.
void Read(char* iFile_Name, char* oFile_Name)
{
int *iPtr; //integer pointer to point to the integer vals in the file
char *cPtr; // char pointer to point to the char vals in the file
float *fPtr; // float pointer to point to the float vals in the file
Date *dPtr; // Date ponter to point the values of type date in the file
int i = 0; // iterator
string dType; // type indicator
int dSize; // vals size indicator
ifstream inFile(iFile_Name); // open file "config.dat"
ofstream outFile(oFile_Name); // open file "reverse.dat"
//checking if the file exsits or not
if (!inFile)
{
//if not, prompt the user and terminate.
cout << "Error opening file: " << iFile_Name;
exit(1);
}
//try block for exception handling.
try
{
cout << "File Exists, Start reading/processing file\n";
//pass through the elements of the file till reaching the EOF End Of File
while (!inFile.eof())
{
inFile >> dType >> dSize; // read type and size indicators
outFile << dType << " " << dSize << " "; // write into the reverse.dat file
if (dType == "int")
{ // if the type is int then save the vals into an int array iPtr
iPtr = new int[dSize]; // memory allocation for the array
for (i = 0; i < dSize; i++) // pass through the values
inFile >> iPtr[i]; // save them into the array
// reverse the vals
for (i = dSize - 1; i >= 0; i--)
outFile << iPtr[i] << " "; // save them reversed order into the O_file object "reversed.dat"
}
//check if the typs is float
else if (dType == "float")
{
fPtr = new float[dSize];// memory allocation for the array
for (i = 0; i < dSize; i++) // pass through the values
inFile >> fPtr[i]; // save them into the array
// reverse the vals
for (i = dSize - 1; i >= 0; i--)
outFile << fPtr[i] << " ";// save them reversed order into the O_file object "reversed.dat"
}

// same as previous for the char type.
else if (dType == "char")
{
cPtr = new char[dSize];
for (i = 0; i < dSize; i++)
inFile >> cPtr[i];

for (i = dSize - 1; i >= 0; i--)
outFile << cPtr[i] << " ";
}

else if (dType == "Date")
{
dPtr = new Date[dSize];
for (i = 0; i < dSize; i++)
inFile >> dPtr[i];

for (i = dSize - 1; i >= 0; i--)
outFile << dPtr[i] << " "; // save the reversed values of date into the O)file object "reversed.dat"

}
//write new line into the reversed.dat file
outFile << endl;
iPtr = 0;
}
cout << "Writing Reversed vals. to File:" << oFile_Name << "\n";
}
//to catch the exception
catch (std::ifstream::failure e)
{
cout << "Exception opening/reading/closing file\n" << e.what();
}
//closing the file streams.
inFile.close();
outFile.close();

}

int main(int argc, char* argv[])
{
//passing files names to arguments vector argv[]
argv[1] = "config.dat";
argv[2] = "reversed.dat";
cout << "Reading File " << argv[1] << " ...\n";
//calling a fucntion to read the file
Read(argv[1], argv[2]);
cout << "Finished writing to file " << argv[2] << " :)";
return 0;
}
I was just wondering if someone can help verify if everything is written properly. I am not sure if I have done it correctly.
It looks fine, mostly. You allocate arrays for reading the 3rd field, but you don't release the memory.

But Read() should really return the stuff it read. You can use the standard collections in the STL to hold them. That would allow you to pass them to a Process then Write function for further processing. Also, Read() shouldn't do anything with the output file.
Topic archived. No new replies allowed.