- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个程序,该程序从文件中读取每一行并使用特定的分隔符从该行中提取单词。
基本上我的代码是这样的
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argv, char **argc)
{
ifstream fin(argc[1]);
char delimiter[] = "|,.\n ";
string sentence;
while (getline(fin,sentence)) {
int pos;
pos = sentence.find_first_of(delimiter);
while (pos != string::npos) {
if (pos > 0) {
cout << sentence.substr(0,pos) << endl;
}
sentence =sentence.substr(pos+1);
pos = sentence.find_first_of(delimiter);
}
}
}
但是我的代码没有读取行中的最后一个字。例如,我的文件看起来像这样。 Hello World
程序的输出只是单词“hello”而不是“world”。我已经使用 '\n' 作为分隔符,但为什么它不起作用?
如有任何提示,我们将不胜感激。
最佳答案
getline 不保存字符串中的换行符。例如,如果您的文件包含以下行“ Hello World \n”getline 将读取此字符串“ Hello World \0”所以你的代码错过了“世界”。
忽略那句话没有定义,你可以改变你的代码这样工作:
#include<iostream>
#include<fstream>
using namespace std;
int main(int argv, char *argc)
{
ifstream fin(argc[1]);
char delimiter[]="|,.\n ";
while (getline(fin,sentence)) {
sentence += "\n";
int pos;
pos = find_first_of(sentence,delimiter);
while (pos != string:: npos) {
if (pos > 0) {
cout << sentence.substr(0,pos) << "\n";
}
sentence =sentence.substr(pos+1);
pos = find_first_of(sentence,delimiter);
}
}
}
请注意,我借用了 Bill the Lizards 更优雅的附加最后一个定界符的解决方案。我以前的版本有一个循环退出条件。
关于c++ - 未读行中的最后一个字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/872656/
我正在尝试实现类似于电子邮件中的已读/未读的功能。我如何通过使用 jquery、php 和 mysql 来实现。我想分步执行的操作: 1- 当我单击 html 表中的一行时,我更改了此消息在数据库中的
如何确定消息状态(已读/未读)。聊天是通过 XMPP 协议(protocol)实现的。 最佳答案 XEP-0184 : Message Delivery Receipts 支持在消息已送达时通知发件人
上下文:我们正在实现一个新闻应用程序。现在,您可以假设所有用户的新闻都是相同的,并根据我们设置的参数(根据趋势和日期)维护订单。 问题:我们不确定跟踪用户阅读内容的最佳实现是什么。我们希望能够配置一种
好吧,另一个有趣的问题是 50 号公路。 我们想实现一个真正的论坛灯泡系统,其中用户未读的帖子(在创建用户帐户后)显示为未读,直到该状态被清除或直到用户阅读它们。 我们认为最好和最简单的方法是实现一个
我正在使用来自 google reader's API 的数据开发应用程序并使用 GData用于登录。 我希望能够将表格单元格内的帖子标记为已读/未读,但我发现这主要是未记录的,很难找到解决方案,有什
我的 C 程序已从 Linux 上的 TCP 套接字读取(使用 read(2) 或 recv(2))几个字节。是否可以将这些字节推回,以便后续的 read(2) 和 recv(2) 调用(在我无法控制
我是一名优秀的程序员,十分优秀!