-2

I have a large char array that is filled with 0's. I read an incoming file from a socket and place it's contents in the buffer. I can't write the buffer with all of the '\0's in it, so I allocate a new buffer with the correct size and to write. I used this approach to do that:

int i = 0;
while(buf[i] != '\0'){
    i++;
}
char new[i];
while(i){
    new[i] = buf[i];
    i--;
}
new[0] = buf[0];

While this approach works, it doesn't seem like the smartest or most elegant way. What is the best way to remove all of the trailing NULL chars from a char array?

DMP
  • 973
  • 1
  • 7
  • 13
  • 1
    What do you mean by "remove"? All array elements have a value. Perhaps `realloc` is what you need? – Oliver Charlesworth Sep 26 '14 at 22:26
  • 3
    I don't think you quite get the point. You can write from the buffer you have, you just need to know the active length of it. – Hot Licks Sep 26 '14 at 22:27
  • I don't mean remove, I'm creating a new buffer with a different length that will hold the file. – DMP Sep 26 '14 at 22:28
  • 1
    I second both comments. First you need to clarify what you mean by "remove". Second, you should consider an alternative, such as writing the buffer to the file up to the first null value. – Code-Apprentice Sep 26 '14 at 22:28
  • 1
    Those bytes are already allocated in memory, you can't "remove" those. You could allocate a new array of the required size... but there's no need to do that. Copy just the number of bytes you need from the array that's already allocated. (Reallocating another array and copying to that is unnecessary, the copy operation is already going to be performed when you "write to a local file". – spencer7593 Sep 26 '14 at 22:30
  • `NULL` is a null pointer constant, *not* a null character. A null character can be referred to as `NUL`, as `'\0'`, or just as a "null character". – Keith Thompson Sep 26 '14 at 22:34
  • I do not understand why you want to copy all that data again into another buffer, which is not even initialized in you code example. Also what about '\0' characters inside the data? If your data contained those, you'd write only half of the file. – St0fF Sep 26 '14 at 22:41

3 Answers3

2

I suppose easier way to do this is:

size_t len = strlen(buf); // will calculate number of non-0 symbols before first 0
char * newBuf = (char *)malloc(len); // allocate memory for new array, don't forget to free it later
memcpy(newBuf, buf, len); // copy data from old buf to new one
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • Yes. But why is there a need to perform that operation at all? – spencer7593 Sep 26 '14 at 22:33
  • @spencer7593 it makes sense if original buffer further re-used for socket operations – Iłya Bursov Sep 26 '14 at 22:35
  • Perfect, I totally forgot about strlen. – DMP Sep 26 '14 at 22:41
  • [Don't cast the result of malloc (and friends)](http://stackoverflow.com/q/605845). – Deduplicator Sep 26 '14 at 22:55
  • 2
    Forgeting about strlen() in network code is usually a good idea. – Martin James Sep 26 '14 at 22:58
  • @Deduplicator I agree that casting is not required in C, but: 1. I'm 99% sure that OP uses C++ compiler. 2. I prefer to avoid problems when newBuf could be `char**` and automatic casting will hide this error. 3. This code can be copy-pasted either in c or cpp file and will work the same without warnings/errors. – Iłya Bursov Sep 26 '14 at 23:04
  • 1. Than make him aware that he's writing bad C++ instead of C. 2. huh? 3. C&P such a snippet from SO is not a great idea, and writing bad C to be C++ compatible is worse. – Deduplicator Sep 26 '14 at 23:08
1

My first idea would be: count the characters you receive from your stream, then write that amount of bytes at once to your file.

If that is not possible, we get to the less smart approach:

int i = BUFFER_SIZE - 1;
while((i>=0) && (buffer[i--] == '\0'));
++i;

After that, i contains the length of your buffer.

St0fF
  • 1,553
  • 12
  • 22
  • As you wrote "I read an incoming file from a socket" - which may be arbitrary data, strlen() wouldn't work correctly. Please be more precise in the future. – St0fF Sep 26 '14 at 22:43
0

You can take the advantage of the NULL property. Just copy the this data without NULL character. Like this len = strlen(buf); // it will make sure you are reading only total_char + 1 Now copy only len-1 char from this buffer to the file. If you want to you can prepare the new buffer too.

ajaym
  • 11
  • 1