我已使用此函数将文件内容放入字符数组中:
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;
}
现在我想从字符数组中去除所有的行尾。 如果不先将字符数组转换为字符串,我该怎么做?
顺便说一句。这是我们不允许使用字符串库的家庭作业的一部分。
最佳答案
#include <algorithm>
size = std::remove(pData, pData + size, '\n') - pData;
pData[size] = 0; // optional
对于一些 C++11 lambda 的乐趣:
#include <algorithm>
size = std::remove_if(pData, pData + size, [](char c) { return c == '\n'; }) - pData;
pData[size] = 0; // optional
关于c++ - 如何从字符数组中去除换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10490367/