|
-
March 31st, 2010, 03:02 AM
#1
how to detect NULL bytes in a char array ?
Hi All,
I want to detect if any NULL bytes exist in a char array.
Consider the following code snippet:
Code:
char buffer[1000000];
ifstream fstr("myfile", ios::binary);
fstr.read(buffer, 1000000);
//check if buffer has any NULL characters
for(int i = 0; i < 1000000; i++)
{
if(!buffer[i]) detected = true;
}
Assuming that fstr has 1000000 bytes, constructing a for loop like above seems inefficient (in terms of speed) to me to detect NULL characters.
Buffer length could be much higher.
Which methodology do you suggest to detect NULL characters in the fastest way in a char array ?
Thanks.
-
March 31st, 2010, 03:36 AM
#2
Re: how to detect NULL bytes in a char array ?
Just use strlen!
Victor Nijegorodov
-
March 31st, 2010, 03:47 AM
#3
Re: how to detect NULL bytes in a char array ?
I'm not sure that you've got much choice other than to iterate through the chars. Even if you were to have an array of the appropriate length and treated it as an array of int or long you would only end up having to compare each one against four masks.
Better than using an explicit loop, use something like the following.
Code:
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
char buffer[1000000];
ifstream fstr("myfile", ios::binary);
fstr.read(buffer, 1000000);
const char *BEGIN = buffer;
const char *END = buffer + 1000000;
bool detected = (find(BEGIN, END, 0) != END);
}
You can adapt it for finding any value.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 31st, 2010, 03:53 AM
#4
Re: how to detect NULL bytes in a char array ?
Again:
Code:
char buffer[1000000];
....
bool detected = strlen(buffer) < 1000000;
Victor Nijegorodov
-
March 31st, 2010, 04:12 AM
#5
Re: how to detect NULL bytes in a char array ?
 Originally Posted by VictorN
Again:
Code:
char buffer[1000000];
....
bool detected = strlen(buffer) < 1000000;
What if I want to count for the number of NULL characters in a char array ?
Given the following value for buffer with no terminating null character:
Code:
buffer[0] = 'a'
buffer[1] = 0
buffer[2] = 0
buffer[3] = 'a'
How can I calculate the number of NULL characters (e.g., 2 in above example) without iterating through the array members ?
-
March 31st, 2010, 04:15 AM
#6
Re: how to detect NULL bytes in a char array ?
 Originally Posted by aryan1
How can I calculate the number of NULL characters (e.g., 2 in above example) without iterating through the array members ?
Is this case you have to iterate...
Victor Nijegorodov
-
March 31st, 2010, 04:15 AM
#7
Re: how to detect NULL bytes in a char array ?
1) your original loop is OK, except you should break out once a NULL
is detected.
2) I would use std::find
3) strlen assumes that there is a NULL ... if there is not a NULL in
the buffer, it would continue checking untill it finds one.
4) I would expect that the read takes up more time than the
checking for NULL. You might want to break the read up into
blocks (512 or 1024) and check each block after you read it.
If a NULL is found early, it would save having to do a lot of reads.
-
March 31st, 2010, 04:16 AM
#8
Re: how to detect NULL bytes in a char array ?
If you want to count all the NULLs in the array, use std::count ...
-
March 31st, 2010, 04:23 AM
#9
Re: how to detect NULL bytes in a char array ?
 Originally Posted by Philip Nicoletti
3) strlen assumes that there is a NULL ... if there is not a NULL in
the buffer, it would continue checking untill it finds one.
Good point, Philip!
I missed the fact that buffer might be full filled with data without terminated NULL!
Victor Nijegorodov
-
March 31st, 2010, 04:34 AM
#10
Re: how to detect NULL bytes in a char array ?
 Originally Posted by Philip Nicoletti
If you want to count all the NULLs in the array, use std::count ...
So the following code snippet will do the job:
Code:
const char *BEGIN = buffer;
const char *END = buffer + 1000000;
size_t numberOfNullChars = count(BEGIN, END, 0);
-
March 31st, 2010, 04:51 AM
#11
Re: how to detect NULL bytes in a char array ?
If you were to use std::vector then you can do this.
Code:
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
vector<char> buffer(1000000);
ifstream fstr("myfile", ios::binary);
fstr.read(&buffer[0], buffer.size());
vector<char>::size_type null_count = count(buffer.begin(), buffer.end(), 0);
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 31st, 2010, 05:10 AM
#12
Re: how to detect NULL bytes in a char array ?
Also, note ... if you are only interested in the number of NULLs
in the file and are looking at the entire file, you do not need a buffer:
Code:
#include <algorithm>
#include <fstream>
#include <iterator>
using namespace std;
int main()
{
ifstream fstr("myfile", ios::binary);
size_t null_count = count(istreambuf_iterator<char>(fstr), istreambuf_iterator<char>(), 0);
}
-
March 31st, 2010, 05:36 AM
#13
Re: how to detect NULL bytes in a char array ?
Isn't the STL wonderful!
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
March 31st, 2010, 05:49 AM
#14
Re: how to detect NULL bytes in a char array ?
It's also probably worthy of note that every one of the posted solutions except for Philip's latest solution have the same horrible bug. That is that the number of bytes read from the file may be far less than the number of bytes in the buffer, yet the check for null characters covers the whole buffer. So what about the values found in the remainder of the buffer? The data may have no null characters but the remainder of the buffer might, thus an incorrect result may be produced.
-
March 31st, 2010, 05:57 AM
#15
Re: how to detect NULL bytes in a char array ?
Point taken, but I worked on the assumption that this is a very simplified example to demonstrate the algorithm for finding nulls, not robust file handling.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|