>Hi all,
>
>I need to read the whole content of a file in a string object.
>Up to now I don't manage to do that quickly since I read the file
>character by character.
Scott Meyer's covers this exact question rather thoroughly in his book
"Effective C++" Item 29.
ifstream fin(nom_filcher); // the binary isn't necessary
string contenu(istreambuf_iterator<char>(fin), istreambuf_iterator<char>());
the two pages of explanation are worth reading
>here is the code I use (i want to open a PDF file):
>
> bool LoadPDF2String(const char * nom_fichier, std::string & contenu)
> {
> unsigned long size=GetFileLength(nom_fichier); // function that
>returns the number of char of the file
> contenu.resize(size+1,'0');
>
> std::ifstream fin(nom_fichier,std::ios::binary); // open in
>binary mode
> if(fin)
> {
> char ch;
> unsigned long str_i = 0;
> while(fin.get(ch))
> contenu[str_i++] = ch;
>
> contenu[str_i] = 0;
> return true;
> }
> else
> return false;
> }
>
>
>thanks a lot for your help
>rapha�l
>
>
>
>To unsubscribe, send a blank message to
><mailto:[EMAIL PROTECTED]>.
>Yahoo! Groups Links
>
>
>
>
Victor A. Wagner Jr. http://rudbek.com
The five most dangerous words in the English language:
"There oughta be a law"
To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.
| Yahoo! Groups Sponsor | |
|
|
Yahoo! Groups Links
- To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
