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

C++ I could use some help with the syntax for a program I am working on that ope

ID: 3595771 • Letter: C

Question

C++ I could use some help with the syntax for a program I am working on that opens a csv file and stores/reads the records.

#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;

}

Explanation / Answer

#include<iostream>
#include<string.h>
#include <fstream>
using namespace std;

static void copyString2Array(char *dest, string source, int n)
{
int i;
//cout<<"IN";

for(i=0;i<n;i++)
{
*(dest+i)=source[i];
//cout<<*(dest+i);
}
dest[n]='';
}

class CsvLogReader{
private:
ifstream zone1;
public:
CsvLogReader(string fName);
~CsvLogReader();
bool get_next_field(string &s, char delim);
};
  
CsvLogReader::CsvLogReader(string fName)
{
char filename[1000];
copyString2Array(filename,fName,fName.length());
zone1.open(filename);
if(zone1.fail())
cerr << "Error opening input file ";
}

CsvLogReader::~CsvLogReader()
{
if(zone1.is_open())
zone1.close();
}

bool CsvLogReader::get_next_field(string &s, char delim) //scan for delim or new line
{
int i=0;
char str[1000]; // store all the char in a char array until getting delim then copy in a string
char ch;
//cout<<"IN";
if(!zone1.is_open() || zone1.eof())
return false;
while(!zone1.eof())
{
zone1>>ch;
//cout<<ch;
if(ch== delim || ch == ' ')
break;
str[i]=ch;
i++;
}
str[i]='';
//cout<<str;
string st(str);
s = str;
return true;
}

int main()
{
string abc = "abcdef";
int n = 3;
char *str;
str = (char*) calloc(sizeof(char),n);
copyString2Array(str,abc,n);
  
//cout<<str<<endl;
string filepath;
cout<<"Enter the file path"<<endl;
cin>>filepath;
CsvLogReader clr(filepath);
string s;
bool a = clr.get_next_field(s,',');
cout<<s<<endl;
a = clr.get_next_field(s,',');
cout<<s<<endl;
getchar();
getchar();
return 0;
}