In fact, I want to remove all NULLS and EOFs (0x1a)
EOF is a macro defined in <stdio.h>. It's required to have a negative
value, which 0x1A does not, so they can't be the same. EOF very
commonly, though not universally, has a value of -1.
There have been systems where 0x1A was used to indicate the end of a
file. However, such systems are far from universal. I'd recommend making
sure that this value is indeed being used that way in all of the
contexts in which you want to use this code.
from a string then replace them all with spaces. The way I do it
now is by using a for() loop:
for(temp=0;temp<=strlen(buffer);temp++)
{
if(buffer[temp]== '\0' || buffer[temp]==0x1A)
{buffer[temp]=' ';}
}
Is there a faster / more efficient way of doing this?
By definition, strlen(buffer) gives you the offset of the very first
null character in buffer (or, if there is none, it keeps searching past
the end of buffer until if finds one; this often results in memory
access violations - make VERY sure that your buffer is in fact null
terminated before calling strlen). Therefore, there's no point in
checking for null characters before you reach then end of the loop; and
you're guaranteed to find one once you reach that end.
I suspect that you have some kind of misunderstanding, that led you to
think that your code could find a null character in some other locations
as well. However, for the rest of this message I'll assume you intended
it to handle null characters exactly the way it actually does.
You have strlen() scanning sequentially through buffer looking for the
first null character, and then you have your for loop scanning
sequentially through buffer looking for null characters and 0x1A. Why
not do it in a single pass?
Your code sets the final terminating null character to blank. This
guarantees that strlen(buffer) can no longer be used to tell you where
that character used to be. If you're planning to do anything further
with that portion of buffer, you'd better do something to keep track of
where it ends.
You don't say what the element type of buffer is; I'll assume it's char;
make appropriate adjustments below if it's something else.
for(char *p = buffer; *p; p++)
if(*p == 0x1A)
*p = ' ';
*p++ = ' ';
ptrdiff_t length = p - buffer;