At Monday 2004-10-25 04:01, you wrote:

>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
ADVERTISEMENT
click here
Web Bug from http://us.adserver.yahoo.com/l?M=295196.4901138.6071305.3001176/D=groups/S=:HM/A=2128215/rand=607141102


Yahoo! Groups Links

Reply via email to