简体   繁体   中英

How to strip newlines from a char-array?

I've put the contents of a file in a char-array using this function:

void Read::readFile(){
FILE * fp = fopen(this->filename,"rt");
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *pData = new char[size + 1];
fread(pData, sizeof(char), size, fp);
fclose(fp);
this->data = pData;
}

Now I want to strip all line-endings from the char-array. How do I do this without casting the char-array into a string first?

btw. this is part of a homework where we aren't allowed to use the string-library.

#include <algorithm>
size = std::remove(pData, pData + size, '\n') - pData;
pData[size] = 0; // optional

For some C++11 lambda fun:

#include <algorithm>
size = std::remove_if(pData, pData + size, [](char c) { return c == '\n'; }) - pData;
pData[size] = 0; // optional

The easiest approach is to make a second buffer the size of the original array.

int len = size;

char* newBufer = calloc(len,sizeof(char));
int i = 0;
int j = 0;
int nlCount = 0;

for(i=0; i<len; i++) {
  if(pData[i] != '\n') {
    newBuffer[j++] = pData[i];
  } else {
    nlCount++;
  }
}

printf("Finished copying array without newlines. Total newlines removed: %d",nlCount);

The added benefit here is since you calloc'ed instead of malloc'ing your array, all values are zero initially, so in this case, once you are done copying, the data at (len-nlCount) through to (len) will all be zero (ie: '\\0') so it is automatically null-terminated, like a string would be anyways. Don't forget to free() the array when you are done.

In place removal:

void strip_newlines(char* p) {
    char* q = p;
    while (p != 0 && *p != '\0') {
        if (*p == '\n') {
            p++;
            *q = *p;
        } 
        else {
            *q++ = *p++;
        }
    }
    *q = '\0';
}

Something like this:

void Read::readFile()
{ 
    FILE * fp = fopen(this->filename,"rt"); 
    if (fp)
    {
        char *pData = NULL;

        fseek(fp, 0, SEEK_END); 
        long size = ftell(fp); 
        if (size != -1L)
        {
            pData = new char[size];
            if (size > 0)
            {
                fseek(fp, 0, SEEK_SET); 
                size = fread(pData, sizeof(char), size, fp);
            }
        }
        fclose(fp);

        if (size < 0)
        {
            delete[] pData;
            pData = NULL;
        }
        else if (size > 0)
        {
            char *start = pData;
            char *end = start + size;

            char *ptr = (char*) memchr(pData, '\n', size);
            while (ptr)
            {
                int len = 1;
                if ((ptr > start) && ((*ptr-1) == '\r'))
                {
                    --ptr;
                    ++len;
                }

                memmove(ptr, ptr+len, end - (ptr+len));
                end -= len;

                ptr = (char*) memchr(ptr, '\n', end - ptr);
            }

            size = (end - start);
        }

        this->data = pData; 
        this->size = size; 
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM