Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I need help writing a C++ program using inputs and output (I/O). Write a fun

ID: 3682578 • Letter: H

Question

Hi,

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.



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). 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


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.

Any help would be appreciated. and please explain each step

Fiedl 1 Field 2 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

Explanation / Answer

/*** C++ code C++ program using inputs and output (I/O). ***/
/* compile as g++ filename.cpp
run as ./a.out inputfile outfile
*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;


int main(int argc, char* argv[])
{
string dType; // storing data type
int dSize; // storing size
string dSeq; // storing each element of sequence
ifstream file_(argv[1]); // argv[1] holds input filename
ofstream file(argv[2]); // argv[2] holds output file name

while(file_ >> dType >> dSize )
{
vector<string> elements;
for (int i = 0; i < dSize; ++i)
{
file_ >> dSeq;
elements.push_back(dSeq); // reading the sequence in elements vector
}
reverse(elements.begin(),elements.end()); // reversing it

file << dType << " " << dSize <<" ";
for (int i = 0; i < dSize; ++i)
{
file << elements[i]<< " "; // putting the reversed vector in output file
}
file << " ";
  
}

file_.close(); // closing both input and output files
file.close();   

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote