以列表格式读取文件时如何忽略换行符

发布于 2024-11-27 23:22:21 字数 1492 浏览 1 评论 0原文

我正在尝试读取包含标题和作者列表的文件,并且我需要能够忽略分隔文件中每一行的换行符。

例如,我的 .txt 文件可能有一个如下所示的列表:

The Selfish Gene
Richard Dawkins
A Brave New World
Aldous Huxley
The Sun Also Rises
Ernest Hemingway

我必须使用并行数组来存储此信息,然后能够像这样格式化数据:

The Selfish Gene (Richard Dawkins)

我试图使用 getline 来读取数据,但是当我去格式化标题和作者时,我得到这个:

The Selfish Gene
(Richard Dawkins
)

当我从文件中读取列表时,如何忽略换行符?

这是我到目前为止所拥有的:

    int loadData(string pathname)
    {
    string bookTitle[100];
    string bookAuthor[100];
    ifstream inFile;
    int count = -1; //count number of books
    int i; //for variable

    inFile.open(pathname.c_str());
    {
        for (i = 0; i < 100; i++) 
        {
            if(inFile)
            {
                getline(inFile, bookTitle[i]);
                getline(inFile, bookAuthor[i]);
                count++;
            }
        }
        inFile.close();
        return count;
    }

编辑:

这是我的输出函数:

    void showall(int count)
    {
        int j; //access array up until the amount of books
        for(j = 0; j < count; j++)
        {
             cout << bookTitle[j] << " (" << bookAuthor[j] << ")";
             cout << endl;
        }    
    } 

我在这里做错了什么吗?

I am trying to read a file with a list of titles and authors, and I need to be able to ignore the newline character that separates each line in the file.

For example, my .txt file might have a list like this:

The Selfish Gene
Richard Dawkins
A Brave New World
Aldous Huxley
The Sun Also Rises
Ernest Hemingway

I have to use parallel arrays to store this info, and then be able to format the data like so:

The Selfish Gene (Richard Dawkins)

I was trying to use getline to read the data, but when I go to format the title and author, I get this:

The Selfish Gene
(Richard Dawkins
)

How do I ignore the newline character when I read in the list from the file?

This is what I have so far:

    int loadData(string pathname)
    {
    string bookTitle[100];
    string bookAuthor[100];
    ifstream inFile;
    int count = -1; //count number of books
    int i; //for variable

    inFile.open(pathname.c_str());
    {
        for (i = 0; i < 100; i++) 
        {
            if(inFile)
            {
                getline(inFile, bookTitle[i]);
                getline(inFile, bookAuthor[i]);
                count++;
            }
        }
        inFile.close();
        return count;
    }

EDIT:

This is my output function:

    void showall(int count)
    {
        int j; //access array up until the amount of books
        for(j = 0; j < count; j++)
        {
             cout << bookTitle[j] << " (" << bookAuthor[j] << ")";
             cout << endl;
        }    
    } 

Am I doing something wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一抹微笑 2024-12-04 23:22:21

正如 @Potatoswatter 所说, std::getline 通常会删除换行符。如果换行符仍然通过,您可能使用的系统使用 \n 作为换行符,但您的文件具有 \r\n 换行符。

只需在将多余的换行符添加到字符串后将其删除即可。你可以用类似的东西来做到这一点:

s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::isspace)).base(), s.end());

或类似的东西。您将在 中找到 std::find_if,在 中找到 std::isspace > 和 中的 std::not1

As @Potatoswatter says, std::getline normally does strip the newline character. If newlines are still getting through, you're probably using a system which uses \n for it's newlines, yet your file has \r\n newlines.

Just remove the extra newlines after they get added to the string. You can do that with something like:

s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::isspace)).base(), s.end());

or similar. You'll find std::find_if in <algorithm>, std::isspace in <clocale>, and std::not1 in <functional>.

作妖 2024-12-04 23:22:21

我得到了它!问题出在我正在阅读的文件上。我将包含讲师给我们的 .txt 中的标题和作者的文件复制并粘贴到一个新的 .txt 文件中,现在它按照我的方式正常工作!谢谢大家的帮助!!

I GOT IT! The problem was the file that I was reading. I copy and pasted the file with the titles and authors from the .txt my instructor gave us into a new .txt file, and now it works fine the way I had it! Thank you everyone for the help!!

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