Question
I need help writing a C++ program using inputs and output (I/O). Write a function that reads from a sequential file and writes to another
I need help writing a C++ program using inputs and output (I/O).
Write a function that reads from a sequential file and writes to another sequential file. The file path and name are passed through arguments of the main function.
int main(int argc, char *argv[])
For example, the file name is config.dat and the path is the current directory.
The schema of the file is shown below.
Fiedl 1 | Field 2 | Field 3 | Field 3 | Field 3 | F3 | F3 | F3 | F3 | F3 | F3 | F3 | F3 | F3 | |
Record 1 | int | 12 | 1 | 9 | 1 | 8 | 1 | 6 | 3 | 4 | 3 | 9 | 7 | 0 |
Record 2 | float | 2 | 5.30 | 56.31 | ||||||||||
Record 3 | char | 6 | h | a | y | K | o | z | ||||||
Record 4 | float | 3 | 5.55 | 22.41 | 10.11 | |||||||||
Record 5 | Date | 2 | 2016-03-21 | 2015-12-31 |
The function should read the file, one record at a time. 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;
Write out each record with the data in reverse order to a file named reverse.dat in the current directory, following the same schema. 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
Apply exception handling with throw, and try-catch statements to handle possible runtime exceptions, and error handling. For example, one error is the number of value given in the file is not consistent with the int 12 1 9 1 8 6 3 4 3 9 7 0 extra values, such as Date 2 2012-11-01. There is one missing value, so you can consider how to handling such a case
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.
THIS IS MY PROGRAM SO FAR , PLEASE COMPLETE IT
#include
#include
#include
using namespace std;
int main(int argc, char*argv[])
{
cout << "Project has started. " << endl;
// Creates a file object list of type ifstream.
ifstream list;
list.open(argv[1]);
// Creates a file object list1 of type ofstream.
ofstream list1;
list1.open("reverse.dat", ios::app);
string value;
int number;
//
while (list >> value >> number)
{
if (value == "int")
{
int aint;
//storing the values in an int array.
int *iPtr;
iPtr = new int[number];
for (int a = 0; a < number; a++)
{
list >> aint;
iPtr[a] = aint;
cout << iPtr[a] << " ";
}
list1 << "int ";
//displaying all the integers.
for (int a = (number - 1); a >= 0; a--)
list1 << iPtr[a] << " ";
}
if (value == "float")
{
//storing the values in a float array
float *fPtr;
fPtr = new float[number];
float afloat;
for (int a = 0; a < number; a++)
{
list >> afloat;
fPtr[a] = afloat;
cout << fPtr[a] << " ";
}
list1 << "float ";
//displaying all the floats.
for (int a = (number - 1); a >= 0; a--)
list1 << fPtr[a] << " ";
}
if (value == "char")
{
//storing the values in a char array.
char *cPtr;
cPtr = new char[number];
float afloat;
char achar;
for (int a = 0; a < number; a++)
{
list >> achar;
cPtr[a] = achar;
cout << cPtr[a] << " ";
}
list1 << "Char ";
//displaying all the characters
for (int a = (number - 1); a >= 0; a--)
list1 << cPtr[a] << " ";
}
list1 << endl;
cout << endl;
}
list.close();
list1.close();
cout << "Project has ended." << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started