C++ 中的流式处理 I/O
Streaming I/O in c++
我正在尝试让这个文件流程序工作,但是当我运行它时,它输出"写入"而不是输出文件。我做错了什么?
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char str[10];
ifstream b_file ( "ioTest.txt" );
b_file>> str;
cout<< str <<"n";
cin.get();
}
标准输入流使用空格作为输入的分隔符。如果您尝试提取到字符串,它将提取每个字符,直到找到空格字符。如果您需要文件的全部内容,这里有几个选项:
while (in >> word)
while (b_file >> word)
{
std::cout << word;
}
此方法将循环访问输入流中的每个空格分隔标记。
std::getline()
while (std::getline(b_file, line))
{
std::cout << line;
}
std::getline()
检索逐行输入,这意味着它将提取每个字符,直到到达分隔符。默认情况下,分隔符是换行符,但可以指定为第三个参数。
std::istream_iterator<T>
std::copy(std::istream_iterator<std::string>(b_file),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(std::cout, "n"));
std::istream_iterator
是一个特殊用途的流迭代器类,旨在"迭代"输入流中 T
类型的令牌。
rdbuf()
std::cout << b_file.rdbuf();
这是更底层的。重载 std::ostream::operator<<()
将流缓冲区指针作为参数,它将直接从缓冲区中提取字符。
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 基于多个条件处理地图中的所有元素
- 如何用数字处理log(0)
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- 错误处理.将系统错误代码映射到泛型
- 从文本文件中读取时钟时间和事件时间并进行处理
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 用于矢量处理的多个线程
- 对字符串进行排序时,在c++中处理sort()
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 处理除以零会导致<csignal>意外行为
- 是否可以在c++中处理字符串流中的各个元素
- 在多个核心中处理一个HTTP请求