2 // This file is (C) 2002-2003 Royce Mitchell III and released under the BSD license
10 # include <sys/stat.h>
22 File ( const char* filename
, const char* mode
) : _f(0)
24 open ( filename
, mode
);
32 bool open ( const char* filename
, const char* mode
);
34 bool seek ( long offset
)
36 return !fseek ( _f
, offset
, SEEK_SET
);
46 return fputc ( c
, _f
) != EOF
;
49 std::string
getline ( bool strip_crlf
= false );
51 // this function searches for the next end-of-line and puts all data it
52 // finds until then in the 'line' parameter.
54 // call continuously until the function returns false ( no more data )
55 bool next_line ( std::string
& line
, bool strip_crlf
);
60 bool mycallback ( const std::string& line, int line_number, long lparam )
62 std::cout << line << std::endl;
63 return true; // continue enumeration
66 File f ( "file.txt", "rb" ); // open file for binary read-only ( i.e. "rb" )
67 f.enum_lines ( mycallback, 0, true );
70 bool enum_lines ( bool (*callback
)(const std::string
& line
, int line_number
, long lparam
), long lparam
, bool strip_crlf
);
72 bool read ( void* data
, unsigned len
)
74 return len
== fread ( data
, 1, len
, _f
);
77 bool write ( const void* data
, unsigned len
)
79 return len
== fwrite ( data
, 1, len
, _f
);
93 return feof(_f
) ? true : false;
101 static bool LoadIntoString ( std::string
& s
, const char* filename
);
102 static bool SaveFromString ( const char* filename
, const std::string
& s
, bool binary
);
106 const File
& operator = ( const File
& ) { return *this; }