如何知道它是文本中的新行
我想读取文本中的所有行,所以我正在执行此
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]
我怎么知道它从下一行开始读取?
i wanna read all the lines from a text so am doing this
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
so when it reads it, it reads it like this
[t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t] [ ] [t] [h] [i] [s] [ ] [i] [s] [ ] [a] [ ] [t] [e] [s] [t]
how can i know it start reading from the next line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查
\n
以了解该字符是否为换行符。但是,就您的情况而言,我建议您使用高级函数,例如
std::getline
,它一次读取一行,可以节省您手动执行的大量工作。阅读行的惯用方法是:
You can check against
\n
to know if the character is a newline.However, in your case, I would suggest you to high-level function, such as
std::getline
which reads a single line at a time, saves you much of the labor you're doing manually.The idiomatic way to read line would be as: