Skip to content

Commit

Permalink
add function to read whole file into buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Dec 18, 2011
1 parent f4dcb8a commit 7644ddc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/shogun/io/File.cpp
Expand Up @@ -177,3 +177,18 @@ char* CFile::get_variable_name()
{
return strdup(variable_name);
}

char* CFile::read_whole_file(char* fname, size_t& len)
{
FILE* tmpf=fopen(fname, "r");
ASSERT(tmpf);
fseek(tmpf,0,SEEK_END);
len=ftell(tmpf);
ASSERT(len>0);
rewind(tmpf);
char* result = SG_MALLOC(char, len);
size_t total=fread(result,1,len,tmpf);
ASSERT(total==len);
fclose(tmpf);
return result;
}
8 changes: 8 additions & 0 deletions src/shogun/io/File.h
Expand Up @@ -399,6 +399,14 @@ class CFile : public CSGObject
/** @return object name */
inline virtual const char* get_name() const { return "File"; }

/** read whole file in memory
*
* @param fname - file name
* @param len - length of file (returned by reference)
* @return buffer to read file - needs to be freed with SG_FREE
*/
static char* read_whole_file(char* fname, size_t& len);

protected:
/** file object */
FILE* file;
Expand Down

0 comments on commit 7644ddc

Please sign in to comment.