Jan 4, 2014 at 5:03am UTC
Thanks to read this message. I want load the data from a file to a dynamic array.
I dont know how to determine the size of the file. I can post the code if you want.
Jan 4, 2014 at 5:06am UTC
1 2 3 4 5 6
inline std::ios::pos_type filesize( const char * location )
{
std::ifstream fin( location, std::ios::binary );
fin.seekg( 0, std::ios::end );
return fin.tellg();
}
or (from stack overflow)
1 2
ifstream file( "example.txt" , ios::binary | ios::ate);
return file.tellg();
Last edited on Jan 4, 2014 at 5:11am UTC
Jan 4, 2014 at 5:39am UTC
Why not use a vector and use the push_back() function to add elements to the vector until all data is stored and then use .size() function to determine the size of the vector
Jan 4, 2014 at 11:46am UTC
Last edited on Jan 4, 2014 at 11:48am UTC
Jan 5, 2014 at 6:53am UTC
@ne555 , you don't need to close the file in order to read it after you find the size. Consider this code:
1 2 3 4 5 6
string contents;
cin.seekg(0, ios::end);
contents.resize(cin.tellg());
cin.seekg(0, ios::beg);
cin.read(&contents[0], contents.size());
istringstream iss(contents);
The above code will read an entire file content into a string. The file needs to be redirected to the program
Last edited on Jan 5, 2014 at 6:54am UTC
Jan 5, 2014 at 8:51am UTC
but you can't avoid to close it if you use your shadow fiend `filesize()' function
> cin.read(&contents[0], contents.size());
don't forget the '\0'
terminator
edit: nvm, looks fine.
Last edited on Jan 5, 2014 at 8:53am UTC