I \'m unsure of the syntax and need some help with this assignment. Any help wou
ID: 3595731 • Letter: I
Question
I'm unsure of the syntax and need some help with this assignment. Any help would be appreciated. It's reading from a csv file.
The header file is just for context/information nothing needs to be changed. It's the main file I need help with.
#pragma once
#include
#include
using namespace std;
/*
Class name: CvsLogReader
Description: This class reads 'The Water Works' CSV meter log files.
*/
class CsvLogReader
{
public:
struct logRecord // one customer's water usage per three month period
{
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
};
CsvLogReader(string fName); // constructor - opens the file
~CsvLogReader(); // destructor - closes the file
bool get_file_header(); // reads the file CSV file header (i.e. "YEAR,MONTH,DAY,NAME
bool get_next_record(logRecord &); // reads the next record
bool is_open() { return inFile.is_open(); } // true if the file is open
private:
fstream inFile; // the input file stream
bool get_next_field(string &s, char delim = ','); // reads the next delimited field (called from get_next_record)
};
___________________________________________________________________________________________________________________________________________________
Main file
#include "CsvLogReader.h"
// prototypes
static void copyString2Array(char *dest, string source, int n); // copy n chars from source string to destination array of chars
/*
Function: CsvLogReader::CsvLogReader(string fName)
Parameters:
fName - the log file's complete path (e.g. c:Log.dat)
Return:
n/a
Description:
Class constructor. Opens a text file (CSV files are text files)
*/
CsvLogReader::CsvLogReader(string fName)
{
inFile.open("Water_log.csv");
}
/*
Function: CsvLogReader::~CsvLogReader()
Parameters:
none
Return:
none
Description:
Class destructor. If a file is open, then it is closed.
*/
CsvLogReader::~CsvLogReader()
{
}
/*
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;
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()
{
bool success;
return success;
}
/*
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)
{
bool success;
return success;
}
/*
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)
{
}
Explanation / Answer
headerfile
#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;
/*
Class name: CvsLogReader
Description: This class reads 'The Water Works' CSV meter log files.
*/
class CsvLogReader
{
public:
struct logRecord // one customer's water usage per three month period
{
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
};
CsvLogReader(string fName); // constructor - opens the file
~CsvLogReader(); // destructor - closes the file
bool get_file_header(); // reads the file CSV file header (i.e. "YEAR,MONTH,DAY,NAME
bool get_next_record(logRecord &); // reads the next record
bool is_open() { return inFile.is_open(); } // true if the file is open
private:
int i;
fstream inFile; // the input file stream
bool get_next_field(string &s, char delim = ','); // reads the next delimited field (called from get_next_record)
};
source file
#include "CsvLogReader.h"
// prototypes
// copy n chars from source string to destination array of chars
static void copyString2Array(char *dest, string source, int n)
{
for (int i = 0; i < n && i < source.length; i++)
{
dest[i] = source[i];
}
}
/*
Function: CsvLogReader::CsvLogReader(string fName)
Parameters:
fName - the log file's complete path (e.g. c:Log.dat)
Return:
n/a
Description:
Class constructor. Opens a text file (CSV files are text files)
*/
CsvLogReader::CsvLogReader(string fName)
{
inFile.open("Water_log.csv");
i = 0;
}
/*
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=',')
{
string line;
if (inFile.is_open())
{
getline(inFile, line);
stringstream ss(line);
string item;
vector<string> tokens;
while (getline(ss, item, delim)) {
tokens.push_back(item);
}
s = tokens[i++];
return true;
}
else
return false;
}
/*
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);
return true;
}
else
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 k = 0; k < 9; k++)
{
string s;
get_next_field(s);
if (k == 0)
record.day = atoi(s.c_str());
else if (k == 1)
record.month = atoi(s.c_str());
else if (k == 2)
record.year = atoi(s.c_str());
else if (k == 3)
record.sector = (char)s.c_str();
else if (k == 4)
strcpy(record.custID, s.c_str());
else if (k == 5)
strcpy(record.custName, s.c_str());
else if (k == 6)
record.gallons[0] = strtod(s.c_str(),NULL);
else if (k == 7)
record.gallons[1] = strtod(s.c_str(), NULL);
else if (k == 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 < source.length; i++)
{
dest[i] = source[i];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.