I used to do this simple task with strlen() or finding the first '\0' character in the char array with a while loop. Today I found that this causes a problem when there is an interval somewhere in the char array (It sees the first interval and thinks it is the end of the char array).
Here is the code:
The input stream only reads til whitespace or new line so it is saying their is 17 characters between <center><h3>Hello. You will want to use getline(); if I remember right to get the full line. Though I use string in my example (since you include the string header file).
Oh, didn't see that you wanted the whole file, just thought you wanted per line. Glad my code helped though. Also, as a side note, #include <string.h> should work fine with just #include <string> (though I suppose it is a personal preference for the most part).
<string.h> is not the same as <string>. <string.h> (or <cstring>) is the header that declares the strlen function. <string> is for things related to std::string.
Keep forgetting that, though didn't realize they had so much overlap. I used string.h and left my code as is and it still compiled fine. Curious why they didn't keep strlen in the standard library.
The C++ Standard Library. Though like I said it appears to have overlap between the two libraries. My example code compiles just fine with string.h/cstring and lets me use strlen() while string for std::string compiles fine and won't let me use strlen(). Was just curious why they didn't include strlen in the C++ standard string header.
Doesn't make sense, but that compiling with both headers makes it appear at the surface that is what they pretty much did. Not trying to start a fight, just trying to really understand this.
Standard headers are allowed to include other standard headers. Remove both of <string> and <cstring> and it will probably still compile because some other header already includes <string>.
This does not compile, because <cstring> does not define the string object. If your code appears to compile without <string> and uses the string object, one of the other header files you're including is dragging in <string> without you knowing (as Peter says above).
1 2 3 4 5 6 7
#include <cstring>
usingnamespace std;
int main ()
{
string a;
}
<iostream> is likely including <string> on your implementation, as that is common. <string> might in turn include <cstring>, by by including <iostream> you end up getting all 3 of them.
But that is totally implementation dependent. If you try to compile that code on a different compiler, you might get errors due to the headers not being included.
It's best to explicitly include the headers you know you are using and don't rely on other headers including them.
Okay, I didn't know that. I thought they were all mostly self contained header files. Thanks for clearing that up for me. Wondered how that was compiling under each header.