C++ call functions with information from CSV file. DisplayLogFIle should call th
ID: 3597453 • Letter: C
Question
C++ call functions with information from CSV file.
DisplayLogFIle should call the information from CsvLogReader but I'm not sure how to get the information from there. Could I a pointer in the right direction?
DisplayLogFile
#include "DisplayLogFile.h"
#include "CsvLogReader.h"
#include <iostream>
#include <fstream>
DisplayLogFile::DisplayLogFile()
{
}
bool DisplayLogFile::displayLogFile(CsvLogReader &reader)
{
reader.get_file_header();
printf("%d", reader.chad.day);
putchar(' ');
printf("%d", reader.chad.month);
putchar(' ');
printf("%d", reader.chad.year);
putchar(' ');
printf("%c", reader.chad.sector);
putchar(' ');
for (int i = 0; i < 8; i++)
{
printf("%c", reader.chad.custID[i]);
}
putchar(' ');
for (int i = 0; i < 15; i++)
{
printf("%c",reader.chad.custName[i]);
}
// printf("%s",bob);
putchar(' ');
printf("%f", reader.chad.gallons[0]);
putchar(' ');
printf("%f", reader.chad.gallons[1]);
putchar(' ');
printf("%f", reader.chad.gallons[2]);
putchar(' ');
return true;
}
CsvLogReader.cpp
#include "CsvLogReader.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
// prototypes
static void copyString2Array(char *dest, string source, int n); // copy n chars from source string to destination array of chars
CsvLogReader::CsvLogReader(string fName)
{
chad.day = 0;
chad.month = 0;
chad.year = 0;
chad.sector = 'a';
chad.custID[0] = 'f';
chad.custID[1] = 'f';
chad.custID[2] = 'f';
chad.custID[3] = 'f';
chad.custID[4] = 'f';
chad.custID[5] = 'f';
chad.custID[6] = 'f';
chad.custID[7] = 'f';
chad.custName[0] = 'g';
chad.custName[1] = 'g';
chad.custName[2] = 'g';
chad.custName[3] = 'g';
chad.custName[4] = 'g';
chad.custName[5] = 'g';
chad.custName[6] = 'g';
chad.custName[7] = 'g';
chad.custName[8] = 'g';
chad.custName[9] = 'g';
chad.custName[10] = 'g';
chad.custName[11] = 'g';
chad.custName[12] = 'g';
chad.custName[13] = 'g';
chad.custName[14] = 'g';
chad.gallons[0] = 0;
chad.gallons[1] = 0;
chad.gallons[2] = 0;
inFile.open("Water_log.csv");
//int day, month, year; // date of last meter reading
//char sector; // city sector (N, S, E, W)
//char custID[8]; // customer ID (7 characters ending with a trailing 0)
//char custName[15]; // customer name (14 characters ending with a trailing 0)
//double gallons[3]; // array of water usage for each month of the quarter
}
/*
Function: CsvLogReader::~CsvLogReader()
Parameters:
none
Return:
none
Description:
Class destructor. If a file is open, then it is closed.
*/
CsvLogReader::~CsvLogReader()
{
if (inFile.is_open())
inFile.close();
}
/*
Function: CsvLogReader::get_next_field(string &s, char delim)
Parameters:
s - destination string for the data
delim - the delimiter (default = ',' if none is supplied)
Return:
false if file is not open, or the attempted read fails, or eof encountered
else true
Description:
This function reads one field from a text file, from the current file position to the
delimiter specified by the delim parameter.
*/
bool CsvLogReader::get_next_field(string &s, char delim)
{
bool success = !inFile.eof();
//getline inputs data as string s and stops at delim getline(inFile, s, delim);
if (!getline(inFile, s, delim))
{
success = false; return success;
}
cout << "status =" << success << "string =" << s << endl; return success;
}
/*
Function: bool CsvLogReader::get_file_header()
Parameters:
none
Return:
false if file stream is not open or eof encountered
Description:
This function reads the first line in a text file and discards it.
The purpose is simply to get past the CSV log file header.
*/
bool CsvLogReader::get_file_header()
{
string line;
if (inFile.is_open())
{
getline(inFile, line);
if (inFile.eof())
{
return false;
}
return true;
}
return false;
}
/*
Function: bool CsvLogReader::get_next_record(logRecord &record)
Parameters:
record - reference to a data structure to receive the data
Return:
false if file stream is not open or eof encountered before it can be read
Description:
This function reads one record from a CSV file and stores the data
in the referenced data structure. This function calls get_next_field
9 times, once for each field in a log file, and copies the field data into
the appropriate field in the data structure. Note, the delimiter in
the last field of a CSV log record ends with a ' ' and not ','
*/
bool CsvLogReader::get_next_record(logRecord &record)
{
string line;
if (inFile.is_open())
{
for (int x = 0;x < 9; x++)
{
string s;
get_next_field(s);
if (x == 0)
record.day = atoi(s.c_str());
else if (x == 1)
record.month = atoi(s.c_str());
else if (x == 2)
record.year = atoi(s.c_str());
else if (x == 3)
record.sector = (char)s.c_str();
else if (x == 4)
strcpy_s(record.custID, s.c_str());
else if (x == 5)
strcpy_s(record.custName, s.c_str());
else if (x == 6)
record.gallons[0] = strtod(s.c_str(),NULL);
else if (x == 7)
record.gallons[1] = strtod(s.c_str(), NULL);
else if (x == 8)
record.gallons[2] = strtod(s.c_str(), NULL);
}
}
else
return false;
}
/*
Function: static void copyString2Array(char *dest, string source, int n)
Parameters:
dest - destination array
source - source string
n - number of characters to copy
Return:
n/a
Description:
This function copies a string of chars into an array of chars. The number of
characters copied is limited to the smallest of the string length or n. Before
returning, a '' is appended to the destination array.
*/
static void copyString2Array(char *dest, string source, int n)
{
for (int i = 0; i < n; i++)
{
dest[i] = source[i];
}
dest[n] = '';
}
Explanation / Answer
#include "DisplayLogFile.h"
#include "CsvLogReader.h"
#include <iostream>
#include <fstream>
DisplayLogFile::DisplayLogFile()
{
}
bool DisplayLogFile::displayLogFile(CsvLogReader &reader)
{
reader.get_file_header();
printf("%d", reader.chad.day);
putchar(' ');
printf("%d", reader.chad.month);
putchar(' ');
printf("%d", reader.chad.year);
putchar(' ');
printf("%c", reader.chad.sector);
putchar(' ');
for (int i = 0; i < 8; i++)
{
printf("%c", reader.chad.custID[i]);
}
putchar(' ');
for (int i = 0; i < 15; i++)
{
printf("%c",reader.chad.custName[i]);
}
// printf("%s",bob);
putchar(' ');
printf("%f", reader.chad.gallons[0]);
putchar(' ');
printf("%f", reader.chad.gallons[1]);
putchar(' ');
printf("%f", reader.chad.gallons[2]);
putchar(' ');
return true;
}
CsvLogReader.cpp
#include "CsvLogReader.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
// prototypes
static void copyString2Array(char *dest, string source, int n); // copy n chars from source string to destination array of chars
CsvLogReader::CsvLogReader(string fName)
{
chad.day = 0;
chad.month = 0;
chad.year = 0;
chad.sector = 'a';
chad.custID[0] = 'f';
chad.custID[1] = 'f';
chad.custID[2] = 'f';
chad.custID[3] = 'f';
chad.custID[4] = 'f';
chad.custID[5] = 'f';
chad.custID[6] = 'f';
chad.custID[7] = 'f';
chad.custName[0] = 'g';
chad.custName[1] = 'g';
chad.custName[2] = 'g';
chad.custName[3] = 'g';
chad.custName[4] = 'g';
chad.custName[5] = 'g';
chad.custName[6] = 'g';
chad.custName[7] = 'g';
chad.custName[8] = 'g';
chad.custName[9] = 'g';
chad.custName[10] = 'g';
chad.custName[11] = 'g';
chad.custName[12] = 'g';
chad.custName[13] = 'g';
chad.custName[14] = 'g';
chad.gallons[0] = 0;
chad.gallons[1] = 0;
chad.gallons[2] = 0;
inFile.open("Water_log.csv");
//int day, month, year; // date of last meter reading
//char sector; // city sector (N, S, E, W)
//char custID[8]; // customer ID (7 characters ending with a trailing 0)
//char custName[15]; // customer name (14 characters ending with a trailing 0)
//double gallons[3]; // array of water usage for each month of the quarter
}
/*
Function: CsvLogReader::~CsvLogReader()
Parameters:
none
Return:
none
Description:
Class destructor. If a file is open, then it is closed.
*/
CsvLogReader::~CsvLogReader()
{
if (inFile.is_open())
inFile.close();
}
/*
Function: CsvLogReader::get_next_field(string &s, char delim)
Parameters:
s - destination string for the data
delim - the delimiter (default = ',' if none is supplied)
Return:
false if file is not open, or the attempted read fails, or eof encountered
else true
Description:
This function reads one field from a text file, from the current file position to the
delimiter specified by the delim parameter.
*/
bool CsvLogReader::get_next_field(string &s, char delim)
{
bool success = !inFile.eof();
//getline inputs data as string s and stops at delim getline(inFile, s, delim);
if (!getline(inFile, s, delim))
{
success = false; return success;
}
cout << "status =" << success << "string =" << s << endl; return success;
}
/*
Function: bool CsvLogReader::get_file_header()
Parameters:
none
Return:
false if file stream is not open or eof encountered
Description:
This function reads the first line in a text file and discards it.
The purpose is simply to get past the CSV log file header.
*/
bool CsvLogReader::get_file_header()
{
string line;
if (inFile.is_open())
{
getline(inFile, line);
if (inFile.eof())
{
return false;
}
return true;
}
return false;
}
/*
Function: bool CsvLogReader::get_next_record(logRecord &record)
Parameters:
record - reference to a data structure to receive the data
Return:
false if file stream is not open or eof encountered before it can be read
Description:
This function reads one record from a CSV file and stores the data
in the referenced data structure. This function calls get_next_field
9 times, once for each field in a log file, and copies the field data into
the appropriate field in the data structure. Note, the delimiter in
the last field of a CSV log record ends with a ' ' and not ','
*/
bool CsvLogReader::get_next_record(logRecord &record)
{
string line;
if (inFile.is_open())
{
for (int x = 0;x < 9; x++)
{
string s;
get_next_field(s);
if (x == 0)
record.day = atoi(s.c_str());
else if (x == 1)
record.month = atoi(s.c_str());
else if (x == 2)
record.year = atoi(s.c_str());
else if (x == 3)
record.sector = (char)s.c_str();
else if (x == 4)
strcpy_s(record.custID, s.c_str());
else if (x == 5)
strcpy_s(record.custName, s.c_str());
else if (x == 6)
record.gallons[0] = strtod(s.c_str(),NULL);
else if (x == 7)
record.gallons[1] = strtod(s.c_str(), NULL);
else if (x == 8)
record.gallons[2] = strtod(s.c_str(), NULL);
}
}
else
return false;
}
/*
Function: static void copyString2Array(char *dest, string source, int n)
Parameters:
dest - destination array
source - source string
n - number of characters to copy
Return:
n/a
Description:
This function copies a string of chars into an array of chars. The number of
characters copied is limited to the smallest of the string length or n. Before
returning, a '' is appended to the destination array.
*/
static void copyString2Array(char *dest, string source, int n)
{
for (int i = 0; i < n; i++)
{
dest[i] = source[i];
}
dest[n] = '';
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.