Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fastest way to read a file into a char*

Status
Not open for further replies.

jsitke

Programmer
Jun 28, 2001
7
US
I have been looking into the fastest way to read a file into a
Code:
char*
. I have seen some code implementing
Code:
istreambuf_iterator
. Any one else have any experiences with this?

- julian

phaedra.jpg
 
Here was some code submitted by Dietmar Kuehl...
Code:
Just read the file when constructing the string:

  int main() {
    std::ifstream                  in(filename);
    std::istreambuf_iterator<char> beg(in), end;
    std::string                    str(beg, end);
  }
 
Why are the buffers only a sizeof 4 after the code is excuted? I need to fill the buffer completely.
Code:
    long lSize;
    char * buffer;
    lSize = 10504120
    ifstream in;
    in.open (fileName);
    filebuf *pbuf;
    pbuf=in.rdbuf();
    pbuf->sgetn (buffer,lSize);
    cout << &quot;Buffer Size:&quot; << sizeof(buffer) << endl;
    cout << &quot;PBuffer Size:&quot; << sizeof(pbuf) << endl;
 
you return there size of pointer, not the size of allocated memory it points. John Fill
1c.bmp


ivfmd@mail.md
 
OK. But when I dump the buffer to out:
Code:
cout << &quot;Buffer Contents:&quot; << buffer
I only get a few chars. How can I dump the entire buffer?

-j
 
I am now trying this code block:
Code:
#include <string>

    std::ifstream      in(fileName);
    std::ostringstream tmp;
    tmp << in.rdbuf();
    std::string str = tmp.str();

And I get these of compilation errors:

D:\DOCs\inetConsole\inetConsole.cpp(307) : error C2079: 'in' uses undefined class 'basic_ifstream<char,struct std::char_traits<char> >'
D:\DOCs\inetConsole\inetConsole.cpp(307) : error C2440: 'initializing' : cannot convert from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\DOCs\inetConsole\inetConsole.cpp(308) : error C2079: 'tmp' uses undefined class 'basic_ostringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
D:\DOCs\inetConsole\inetConsole.cpp(309) : error C2228: left of '.rdbuf' must have class/struct/union type
D:\DOCs\inetConsole\inetConsole.cpp(310) : error C2228: left of '.str' must have class/struct/union type
D:\DOCs\inetConsole\inetConsole.cpp(324) : error C2228: left of '.eof' must have class/struct/union type
 
maybe change the size of rdbuf like this example:

const int BuffSize = 4096; // or whatever you want
char FileBuffer[BuffSize];

in.rdbuf()->pubsetbuf(FileBuffer, BuffSize);
// returns a streambuf


However, I am not sure how the destructor for your fstream deals with your rdbuf, you might have to save the old buffer as a streambuf and restore it before the fstream goes out of scope.
 
I need to contain the entire file stream in the buffer, therefore the buffer should be the same size as the file. I am running the code below, but it seems to hang. However if I output to a file
Code:
ofstream out (&quot;test.txt&quot;,ofstream::binary)
it does not hang...
Code:
 {
      char *buff;
      long size;
      ifstream infile (fileName);

      infile.seekg(0,ifstream::end);
      size=infile.tellg();
      infile.seekg(0);

      buff = new char [size];
      infile.read(buff,lSize);

      std::ostringstream out;
      out.write (buff,lSize);
      std::string mystr = out.str();
    }
 
Code:
#include <string>

    std::ifstream      in(fileName);
    std::ostringstream tmp;
    tmp << in.rdbuf();
    std::string str = tmp.str();


And I get these of compilation errors:

D:\DOCs\inetConsole\inetConsole.cpp(307) : error C2079: 'in' uses undefined class 'basic_ifstream<char,struct std::char_traits<char> >'
D:\DOCs\inetConsole\inetConsole.cpp(307) : error C2440: 'initializing' : cannot convert from 'char *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\DOCs\inetConsole\inetConsole.cpp(308) : error C2079: 'tmp' uses undefined class 'basic_ostringstream<char,struct std::char_traits<char>,class std::allocator<char> >'
D:\DOCs\inetConsole\inetConsole.cpp(309) : error C2228: left of '.rdbuf' must have class/struct/union type
D:\DOCs\inetConsole\inetConsole.cpp(310) : error C2228: left of '.str' must have class/struct/union type
D:\DOCs\inetConsole\inetConsole.cpp(324) : error C2228: left of '.eof' must have class/struct/union type

Don't forget to
Code:
#include <fstream.h>
:)
 

Use memory-mapped files. The operating system (Windows NT 3.1 or later, Windows 95 or later) handles the paging for you. It's pretty sweet.

Open a file for reading as usual with CreateFile(). Then call:
[tt]
hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, &quot;MyFileMappingObject&quot;);
[/tt]
Then call:
[tt]
LPVOID lpMapAddress;
lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
[/tt]
You would then access the file through the lpMapAddress pointer. Be sure to call UnMapViewOfFile() and CloseHandle() when you're done.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top