Wednesday, February 14, 2018

Reading chunk of data from a file calling a function using C++

#include <iostream>
#include <fstream>
#include <string>
#include<vector>
using namespace std;
bool generate_chunks(ifstream &fp,int chunk_size,string &buffer)
{
    vector<char>data(chunk_size);
    fp.read(&data[0],chunk_size);
    std::streamsize datasize = fp.gcount();
    if(datasize == 0)
    {
        cout<<"here";
        return false;
    }
    buffer =string(data.begin(),data.end());
    return true;

}
int main() {
    std::cout << "Hello, World!" << std::endl;
    ifstream myfile;
    bool test;
    string data;

    myfile.open("../test.dat",ifstream::binary);

   if(myfile.is_open()) {


        if(generate_chunks(myfile, 1024, data))

        {
            cout<<data;
        }

   }


    myfile.close();

    return 0;
}

No comments:

Post a Comment