在文件中写入而不是在 c++ 中使用 "<<" 的替代方法?

Alternate way to write in a file instead of using "<<" in c++?

本文关键字:lt 方法 文件 c++      更新时间:2023-10-16

当我们使用 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