如何知道它是文本中的新行

发布于 2024-12-28 16:09:16 字数 802 浏览 3 评论 0原文

我想读取文本中的所有行,所以我正在执行此

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2025-01-04 16:09:16

您可以检查 \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;
}

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:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文