1

Hi I want read from a hung text file data directly to a string I know that i can use the following:

ifstream infile("myfile");
string str(istreambuf_iterator<char>(infile), istreambuf_iterator<char>());

but this way read whole file in one step. I want read it in several step beacuse this is very larg file about 50GB. How I can do it? thanks for your advice Herzl.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49
  • But you do want the entire file in a string? – Fred Foo Mar 30 '11 at 10:56
  • not whole file in once. each time I want to read 1MB data.Process it then read remiand data from file. – herzl shemuelian Mar 30 '11 at 10:59
  • 1
    possible duplicate of [Reading directly from an std::istream into an std::string](http://stackoverflow.com/questions/1816319/reading-directly-from-an-stdistream-into-an-stdstring) – Fred Foo Mar 30 '11 at 11:06
  • It's duplicate of [this question](http://stackoverflow.com/questions/3540183/how-can-i-read-exactly-128-bytes-from-an-fstream-into-a-string-object). – maverik Mar 30 '11 at 11:02

2 Answers2

1

I'd do something like this (bufSize can be modified to fit your needs):

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void ReadPartially(const char *file)
{
    const unsigned bufSize = 10;
    ifstream is(file);
    string str(bufSize, '\0');
    unsigned count=0;
    while(!is.eof())
    {
        count++;
        is.read(&str[0], bufSize);
        cout << "Chunk " << count << ":[" << endl << str  << "]" << endl;
    }
}
INS
  • 10,594
  • 7
  • 58
  • 89
  • 1
    I don't think that there's a strong guarantee that `&str[0]` will return a contiguous chunk of memory (at least not in the current standard)... – Eugen Constantin Dinca Mar 31 '11 at 08:41
  • You are right. Only the vector class guarantees the continuity of the buffer where data is stored. But in practice I haven't had any problems with string class also. It's good this was mentioned. – INS Apr 14 '11 at 20:53
0

I'd go with good old fopen and fread, they give you more control in such situations.

Corvin
  • 990
  • 11
  • 17