I have this minor problem which I''m sure is fairly straight forward but I just can''t see it.
char *buffer;
long size;
// Read in the data
fstream File ("maps\\g5614\\g5614c_r.shp", ios::in | ios::binary | ios::ate);
// Check to see if file opened
if (!File.is_open())
return false;
//size = File.tellg(); // get length of file
size = 100;
File.seekg(0, ios::beg); //go to the beginning of the file
// sizing the array
buffer = new char[size];
// Read in the entire contents into the buffer.
if (!File.read(buffer, size))
// An error occurred!
File.close();
delete[] buffer;
return false;
Ok, basically everything is working and I''m not getting any error messages. However after I read in the data into the buffer, the buffer becomes ''empty''.. thats the best way to describe it.
If I make the buffer a set array size then everything works fine but for what I want to do that isn''t acceptable.
As I said, I''m sure its something quite simple but I cannot figure it out. Any suggestions appreciated.
Thanks.
tellg gets you the CURRENT position of the file pointer in the file. When you open your file the file pointer usually points to the begin, 0 (unless you''re in append mode).
Use File.seekg(0, ios::end ), and do tellg after that.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
[edited by - Fruny on April 30, 2004 11:16:05 AM]
Endurian, ios::ate flag has it so when its first opened it points to the end of the file/stream.
Fruny, thanks I'll check it out.
edit:
Fruny, that code errors out on
std::copy( begin, end, std::back_inserter(buf) );
with errors C2664 (cannot convert to *char) and C2782 (no bloody idea) in VC++
[edited by - darjk on April 30, 2004 10:49:45 AM]
Ug, I couldn''t let myself write it like that Fruny, even though it makes gratuitous use of functors
.
Fruny: While you''re at it, you might also want to change
std::istreambuf_iterator<char> end();
to:
std::istreambuf_iterator<char> end;
Due to one C++''s more annoying parsing rules, the first one declares the function end() which takes no parameters and returns a istreambuf_iterator. The second one declares a variable called end of type istreambuf_iterator<char>.
Thanks everyone, still playing with it a bit but it appears to be working.
I''ll let you know how I go. Thanks again.
Thank you everyone for code snippets and info.
Have it integrated and working perfectly without any hassles at all. Haven''t used vectors (or C++ for that matter) much so its going to be a fun learning curve.