Changes

Jump to: navigation, search

C/C++ FAQ

2,052 bytes added, 20:03, 19 November 2012
no edit summary
<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 ] ) {
</source> return N;  }
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 int main(but c++ only) solution.{
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]. 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>
std::ifstream in("myfile", ios::binary);
#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!"::stringstream buffer;
cout buffer << "s[] is " << SizeOfArrayin.rdbuf( s );
}std::string contents(buffer.str());
</source>
The good thing about the above method is not only its improved efficiency You could also use  <source>  inMyStream.seekg(0,std::ios_base::end);  std::ios_base::streampos end_pos = inMyStream.tellg(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"> return end_pos;
const char* s = "Hello world!"; cout << "s is " << SizeOfArray( s )
</source>
Lastlyto get the length of the file without actually reading it, with C++11 there is an even better and then if needed read / append / modify it as required using a char array.. This method of doing thisgetting 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> std::ifstream ifs("foobar.txt", ios::binary); ifs.seekg(0, std::ios: :end); std::extentifstream::pos_type filesize = ifs. More info on this trait class 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 be found 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/stdstring/type_traitsstring/extentsubstr/ here.substr](string class) or use it as a vector (if that's required).

Navigation menu