How to find the lenght of a char array

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <string.h>
using namespace std;
int main ()
{
ifstream file;
file.open("index.html");
if(!file.is_open()){exit(EXIT_FAILURE);}
char word[2000];
file >>word;
cout<<strlen(word);
return 0;
}


and here is the file which words I am trying to count:
1
2
<center><h3>Hello World!</h3></center>
<p>This is an example page that I am currently trying to show on my Iceweasel browser</p>



The index.html file is way beyond 17 characters but the output is 17 (This is where the first interval character in the index.html file is found)!

How should I work around this?

Thanks in advance!
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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <string.h>
using namespace std;
int main ()
{
	ifstream file;
	file.open("index.html");
	string str;
	if(!file.is_open()){exit(EXIT_FAILURE);}
	while(!file.eof())
	{
		getline(file, str);
		cout<<str.length();
		cout << endl;
	}
	cout << endl;
	return 0;
}

Could do a if statement to drop off empty lengths if you wanted
1
2
3
4
5
if(str.length() != 0)
{
      cout << str.length();
      cout << endl;
}

Didn't notice, you shouldn't need the .h at the end of #include <string>.
Last edited on by closed account z6A9GNh0
Thank you very very much!
Your code didn't output the whole file's string length but the string length of each line separately.

And... here is how I got it working exactly the way I wanted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <string.h>
using namespace std;
int main ()
{
ifstream file;
file.open("index.html");
string str;
int length=0;
while(!file.eof())
{
getline(file, str);
length=length+str.length();
}
cout<<endl<<length<<endl;
return 0;
}

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.
Curious why they didn't keep strlen in the standard library.

Which one? Mwhuahahaha!
Curious why they didn't keep strlen in the standard library.

They did!
http://www.cplusplus.com/reference/clibrary/cstring/strlen/
Last edited on
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.
I used string.h and left my code as is and it still compiled fine.

On some implementations <iostream> includes <string>.
Was just curious why they didn't include strlen in the C++ standard string header.


It's not in <string> because it's in <cstring>. Putting it in two different headers doesn't really make sense.

<string> has the std::string class and related stuff.
<cstring> has C-style sting (char array) utility functions.

strlen falls into the 2nd category, so it's in <cstring>
Disch wrote:
It's not in <string> because it's in <cstring>. Putting it in two different headers doesn't really make sense.

I agree, but the reason I asked is because this code compiles just fine with either header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;

int main ()
{
	ifstream file;
	file.open("index.html");
	string str;
	
	while(!file.eof())
	{
		getline(file, str);
		if(str.length() != 0)
		{
			cout<<str.length();
			cout << endl;
		}
	}
	cout << endl;
	
	return 0;
}

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>
using namespace std;

int main ()
{
  string a;
}

Last edited on
To elaborate on what Peter is saying....

<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.
Topic archived. No new replies allowed.