如何知道它是文本中的新行
how to know it's a new line in a text
我想读取文本中的所有行,所以我正在进行
int main(){
fstream fs("test.txt",fstream::in|fstream::ate);
int length = fs.tellg();
std::vector<char> buffer(length);
fs.seekg(0, ios::beg);
fs.read(buffer.data(), length);
int newlen= 0;
int ptrSeek = 0;
while(buffer.data()[ptrSeek] != 0){
ptrSeek++;
newlen++;
if(ptrSeek == buffer.size()) { break;}
}
std::vector<char> temp(newlen,0);
memcpy(&temp[0],&buffer[ptrSeek-newlen],newlen);
}
test.txt:
this is a test
this is a test
所以当它读它的时候,它读起来就像这个
[t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t] [ ] [t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t]
我怎么知道它从下一行开始读?
您可以对照n
检查字符是否为换行符。
然而,在您的情况下,我建议您使用高级函数,如std::getline
,它一次读取一行,可以节省您手动操作的大量劳动力。
阅读这行的惯用方法是:
int countNewline= 0;
std::ifstream fs("test.txt");
std::string line;
while(std::getline(fs, line))
{
++countNewline;
//a single line is read and it is stored in the variable `line`
//you can process further `line`
//example
size_t lengthOfLine = line.size();
for(size_t i = 0 ; i < lengthOfLine ; ++i)
std::cout << std::toupper(line[i]); //convert into uppercase, and print it
std::endl;
}
相关文章:
- 使用新行和不使用新行读取文件
- 遇到新行时,有没有办法停止istream_iterator
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 新行上的 clang 格式右括号
- 用C++计算输入文本的新行数
- 从文本文件读取时,如何知道是否已到达新行?
- 使用 C++ 中的向量在新行上显示文本文件中的列
- C++将字符串写入文本文件中的行;新行问题不起作用
- C++ 在文本文件中的特定字符后插入新行
- 用新行C 附加文本
- 如何通过 C++ 在读取文本文件时忽略特定的新行
- 如何知道它是文本中的新行
- 如何从文本文件的末尾删除新行
- 将文本文件的内容一个字符一个字符地读入矢量,不跳过空白或新行
- 将文本光标移动到新行