[LC++]geting a "char *" (not const) from a string.data()

Jan Pfeifer janpf at yahoo-inc.com
Fri Jul 27 04:33:03 UTC 2001


hi :)

i need to read a whole file to a string and return it, but i can't imagine how to do this without having to read
the whole file to a char* buffer first and assigning (copying) to a string later. Is there another way around ? 

the code (without error checking):

string file_read( string filename )
{
  unsigned filesize = file_size( filename ); // suppose file_size() defined
  char *buf = new char[ filesize ];
  int fd = open( filename, O_RDONLY );
  read( fd, buf, filesize );
  string ret( buf, filesize );
  delete[] buf;
  return ret;
}

is there a way round without having to allocate and copy buf ? 

thanks for any help

jan



More information about the tuxCPProgramming mailing list