在文件中写入而不是在 c++ 中使用 "<<" 的替代方法?
Alternate way to write in a file instead of using "<<" in c++?
当我们使用 fstream header 加载文件时,我们使用>>
和getline()
来获取一整行。
将其写入输出文件时,我们使用<<
有没有其他替代方法可以在输出文件中写入该行而不是<<
?
这很简单。 查看下面的代码
#include <iostream>
using namespace std;
int main()
{
char a('a');
// cout
cout.put(a); // can only be used for characters
string hello;
//cin
cin.getline(hello);
//or
getline(cin,hello);
}
看,这太容易了;
您可以使用此处put
链接和此处write
链接作为替代方法。
// single character
char character;
cout.put(character);
//For string
char * buffer = new char[size];
cout.write(buffer, size);
因此,对于您的情况,请查看C++write
。 希望这对你有帮助。
你也可以#include <cstdio>
并使用C API,即使用fopen/printf...
查看文档 : https://en.cppreference.com/w/cpp/header/cstdio
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- 超载&LT的正确方法;操作员