1
edit
Changes
no edit summary
<br>'''Answer Submitted by:''' [[Team 42 Contributions | Team0x2Au]] <br>
'''Q:''' How to get size of an array without storing the size anywhere? <br>
'''A:''' There are several ways to get the length of an array without explicitly knowing its length. The C method is as follows
<source lang="cpp">
#define SIZEOF_ARRAY( a ) (sizeof( a ) / sizeof( a[ 0 ] ))
</source>
The above define macro can be called to find the size of an array in a C manner. This function exists in the std namespace.<br>
One could also use the below code for a more effective (but c++ only) solution.
<source lang="cpp">
#include <iostream> using namespace std;
template <typename T, size_t N>
inline
size_t SizeOfArray( const T(&)[ N ] ) {
return N;
}
int main() {
const char s[] = "Hello world!";
cout << "s[] is " << SizeOfArray( s );
}
</source>
The good thing about the above method is not only its improved efficiency (most compilers will optimize the template function out of existance upon compilation) but the fact that it will not work with a pointer to an array, such as
<source lang="cpp">
const char* s = "Hello world!"; cout << "s is " << SizeOfArray( s )
</source>
Lastly, with C++11 there is an even better method of doing this: std::extent. More info on this trait class can be found [http://www.cplusplus.com/reference/std/type_traits/extent/ here].<br>Submitted by Team42.<br><br>
'''Q:''' How do you get the length of a file / read the entire file without explicitly knowing its length, and how do you use that data afterwards? <br>
'''A:''' There are several ways to read the contents of a file without knowing it's length and then split the result into usable parts.
First, you could use a string class and load the contents of the entire file into the string object. At this point you can manipulate it as any other string object (not to be confused with a char array) by using methods such as find, substr, erase, etc as outlined [http://www.cplusplus.com/reference/string/string/ here]
<source lang="cpp">
std::ifstream in("myfile", ios::binary);
std::stringstream buffer;
buffer << in.rdbuf();
std::string contents(buffer.str());
</source>
You could also use
<source lang="cpp">
inMyStream.seekg(0,std::ios_base::end);
std::ios_base::streampos end_pos = inMyStream.tellg();
return end_pos;
</source>
to get the length of the file without actually reading it, and then if needed read / append / modify it as required using a char array.. This method of getting a file's length can be used with char arrays / strings / vectors without problems.
Lastly, you could also load the file into a vector and manipulate it as a vector object from that point onwards (more advanced topic) like so
<source lang="cpp">
std::ifstream ifs("foobar.txt", ios::binary);
ifs.seekg(0, std::ios::end);
std::ifstream::pos_type filesize = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<char> bytes(filesize);
ifs.read(&bytes[0], filesize);
</source>
For any of these methods, to be able to work with the data afterwords simply use a common delimiter when originally generating the file, and then you can use methods like[http://www.cplusplus.com/reference/clibrary/cstring/strtok/ strtok] (for char array), [http://www.cplusplus.com/reference/string/string/find/ .find] combined with[http://www.cplusplus.com/reference/string/string/substr/ .substr] (string class) or use it as a vector (if that's required).<br>Submitted by Team42.<br><br>
==[[Assignment 2 (Release 0.1): Q & A | Possible/Challenging Enhancements]]==