有没有办法在阅读文本时使用右移来识别'n'
Is there a way to recognize ' ' while using right shift on reading text
这是我的文本文件格式
勒尼汉于1995年退休,担任社会学副教授
在约翰杰伊刑事司法学院。他已经加入了教师队伍
1980年,在哥伦比亚大学担任研究员之后
这是我的代码
ifstream afile("sometext.txt");
string line;
while (afile >> line) {
cout<< line <<" "
}
afile.close();
,打印时没有任何新行
是否可以在字符串上只使用右移打印新行?
你可以这样做,但使用字符而不是字符串,使用peek()并扫描输入缓冲区是否包含'n':
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream afile("sometext.txt");
std::string line;
char c;
while ( !afile.eof())
{
afile >> c;
if('n' == afile.peek())
{
c = 'n';
line += c;
}
else
line += c;
}
std::cout << line << std::endl;
afile.close();
std::cout << std::endl;
return 0;
}
相关文章:
- 右移布尔值在C++
- 何时右移操作>>移位符号位,何时不?
- 这个右移是如何工作的:字符串流>>无符号的int>>无符号的int?
- C++中的负数右移运算符
- C++ 尝试将数字右移 32 会得到两种不同的结果
- 对已签名数据进行逻辑右移
- 右移速度与左移相同的速度测试位
- 右移无法正常工作
- 为什么将 int a=1 移到左 31 位然后向右移 31 位,它变成 -1
- 当 b 大于 a 中的位数时右移(a >> b)的未定义行为?
- 为什么无符号的 int 右移总是用"1"填充
- 重载左移和右移运算符(cin和cout)
- 在c++中,取一个整数数组(arr[])、其长度(N)和元素数右移(M):
- 右移g++时出现意外结果
- 如果计数大于类型的宽度,则为右移未定义的行为
- 使用左移或右移计算对数基数 n
- static_assert签名的右移具有两个补码行为是否合法?
- 在C/C++中,左移或右移零点实际上会生成一条指令
- C++中的右移产生异常结果(无符号64位)
- 有没有办法在阅读文本时使用右移来识别'n'