简体   繁体   English

如何从char数组中删除换行符?

[英]How to strip newlines from a char-array?

I've put the contents of a file in a char-array using this function: 我使用这个函数将文件的内容放在char数组中:

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. 现在我想从char数组中去掉所有的行结尾。 How do I do this without casting the char-array into a string first? 如何在不首先将char-array转换为字符串的情况下执行此操作?

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: 对于一些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. 这里增加的好处是因为你调用了calloc'ed而不是malloc'ing你的数组,所有的值最初都是零,所以在这种情况下,一旦你完成复制,(len-nlCount)到(len)的数据将全部为零(即:'\\ 0'),因此它会自动以空值终止,就像字符串一样。 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; 
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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