以列表格式读取文件时如何忽略换行符
我正在尝试读取包含标题和作者列表的文件,并且我需要能够忽略分隔文件中每一行的换行符。
例如,我的 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @Potatoswatter 所说, std::getline 通常会删除换行符。如果换行符仍然通过,您可能使用的系统使用
\n
作为换行符,但您的文件具有\r\n
换行符。只需在将多余的换行符添加到字符串后将其删除即可。你可以用类似的东西来做到这一点:
或类似的东西。您将在
中找到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:
or similar. You'll find
std::find_if
in<algorithm>
,std::isspace
in<clocale>
, andstd::not1
in<functional>
.我得到了它!问题出在我正在阅读的文件上。我将包含讲师给我们的 .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!!