如何在C++中将文本重定向到文件
How to redirect text to a file in C++
我正在尝试编写一个小型自动化程序,为我计算一些值,并将一些文本输出到一个简单的.txt文件中。执行重定向符号<><<>>在C++中的工作与在批处理脚本的命令行中的工作相同吗?当我尝试在C++中搜索如何重定向到.txt文件时。我发现的所有示例和教程都是以如下方式在控制台上假设IO的。
cout::<<"show this text on the console";
cin::>> whatever you would call here to accept user input.
我想知道的是这样做行吗?
#include <string>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652n";
if {
(X=0)
zero>>C:test.txt;
x+5;
} return 0;
}
您的代码不起作用。我不完全确定所需的行为是什么,但这段代码将字符串zero
写入一个文件:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int X = 0;
string zero = "touchPress 0 483 652n";
ofstream myFile("C:\Data\test.txt");
//a condition which is always true
if (X==0)
{
myFile<<zero;
X + 5; //this is valid but useless
}
return 0;
}
#include <fstream>
int main(){
string zero = "touchPress 0 483 652n";
std::ofstream fout("test.txt"); // creates new test.txt in folder where .exe is
fout << zero; //same as cout << zero;//but in the file
return 0;
}
作为cout,我刚刚重新设计了你勉强活着的程序。这是你想要的吗?
相关文章:
- 如何将stdout重定向到stderr
- 编写时C++中的输入重定向问题
- 获取通过重定向传入的输入文件的名称?
- Windows C++重定向库
- 写入文件 a.txt,而终端重定向到文件 b.txt
- 使用 dup2() 进行输入重定向
- 将 clangd stderr 重定向到文件而不是控制台
- 如何在C++中获取重定向网页的URL
- 将标准输出重定向到ostream
- 如何撤消此代码上的 cin 重定向?
- 使用重定向标准处理子进程中的 kbhit
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- 重定向输入和输出时如何处理主文件I/O?
- 如何将HTTPS流量重定向到本地主机:443上的代理?
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- 外壳追加重定向替换现有文本
- 如何在C++中将文本重定向到文件
- 从Qt内部运行c++二进制文件,并将二进制文件的输出重定向到Qt应用程序上的文本框
- 从控制台窗口(c++应用)重定向到文本框(c#应用)
- 是否有重定向(cin)输入从文本文件在控制台两次,为c++