s the unparsed raw data (use vector here to get the unparsed raw data) Parse:    A external function to Parse the data.  The function accepts the raw data and returns the data parsed by the function.  This type of function is called a "Call-Back Function". A Call-Back is necessary for each file as each file requires different regular expressions to parse. With binary files, need to use RegEx to do the parse(). [My code doesn't have this part] Please refer to the Callback.cpp example on how to setup a Call-Back. CallBack.cpp:

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section: Chapter Questions
Problem 5PP: (Data processing) Write a C++ program that reads the file created in Exercise 4, permits the user to...
icon
Related questions
Question
100%

C++

I cannot run the code. Please help me fix it.

Below is the requirement of the code.

Design and write a C++ class that reads text, binary and csv files.  The class functions:

Size:      Returns the file size
Name:      Returns the file name
Raw:    Returns the unparsed raw data (use vector here to get the unparsed raw data)
Parse:    A external function to Parse the data.  The function accepts the raw data and returns the data parsed by the function.  This type of function is called a "Call-Back Function". A Call-Back is necessary for each file as each file requires different regular expressions to parse.

With binary files, need to use RegEx to do the parse(). [My code doesn't have this part]

Please refer to the Callback.cpp example on how to setup a Call-Back.

CallBack.cpp:

#include <string>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

string ToLower(string s)
{
    string temp;
    for (char c : s)
        temp.push_back(tolower(c));
    temp.push_back(NULL);
    return temp;
}

string ToUpper(string s)
{
    string temp;
    for (char c : s)
        temp.push_back(toupper(c));
    temp.push_back(NULL);
    return temp;
}

struct Append
{
    string operator()(string a, string b)  // function object
    {
        return a + b;
    }
};

class CallBack
{
    function<string(string)> callback; 
public:
    CallBack(function<string(string)> cb) { callback = cb; }
    string useCallBack(string s) { return callback(s); }
};

void embedd(string s)
{
    CallBack cback(ToUpper);
    cout << cback.useCallBack(s) << endl;
}

void functional()
{
    string cat = "cat";
    string dog = "DOG";
    function<string(string)> fn1 = ToUpper; // ToLower;          // function
    function<string(string)> fn2 = &ToUpper;         // function pointer
    function<string(string, string)> fn3 = Append(); // function object
    function<int(string)> fn4 = [](string s) { return s.length(); };  // lambda expression
    function<int(int, int)> fn5 = plus<int>();      // standard function object

    cout << fn1(dog) << endl;
    cout << fn2(cat) << endl;
    cout << fn3(cat, dog) << endl;
    cout << fn4(dog) << endl;
    cout << fn5(123, 456) << endl;
}

int main()
{
    functional();
    embedd("dog");
}

 

myCode.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>

using namespace std;

class FileReader {
private:
    string fileName;
    vector<char> rawData;
    int fileSize;
    function<string(string)> callback;

public:
    FileReader(string name, function<string(string)> cb) {
        fileName = name;
        callback = cb;
    }

    int size() {
        return fileSize;
    }

    string name() {
        return fileName;
    }

    vector<char> raw() {
        return rawData;
    }

    void parse() {
        // Read the file into the raw data vector
        ifstream file(fileName, ios::binary | ios::ate);
        fileSize = file.tellg();
        file.seekg(0, ios::beg);
        rawData.resize(fileSize);
        file.read(rawData.data(), fileSize);

        // Convert the raw data vector to a string and call the callback function to parse the data
        string rawDataStr(rawData.begin(), rawData.end());
        rawDataStr = callback(rawDataStr);
        vector<char> newRawData(rawDataStr.begin(), rawDataStr.end());
        rawData = newRawData;
    }
};

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr