如何在C 中使用特定信息创建一个TXT文件
how to create a txt file with specific information in c++
我的目标是创建一个.txt文件:
598.1#开尔文温度
3.49#ATM中的压力
h_g#系统中允许的物种列表
h2_ref
但是,前两个值应该是2个双重值,我在程序中使用的值
我使用C
如果有人为我有解决方案或提示吗?
那太棒了!
预先感谢您!
最好的问候
schwing
这应该将您的双写入文本文件。有关更多信息,请阅读http://www.cplusplus.com/doc/tutorial/files/
// basic file operations
#include <iostream>
#include <fstream>
void writeFile() {
double myTempInK = 0;
double myPresInAtm = 0;
ofstream myfile;
myfile.open ("example.txt");
myfile << myTempInK << "# temperature in Kelvinn";
myfile << myPresInAtm << "# pressure in atmn";
myfile << "H_g # list of species allowed in the systemn";
myfile << "H2_ref n";
myfile.close();
}
插入数据并创建文件使用std::ifstream f;
并打开它,然后使用f.write(...);
@freshd和graham最好:非常感谢...现在它可以正常使用此代码:
std::ofstream myfile;
myfile.open("C:/Users/schwing/temp/example.txt");
myfile << p << "n " << T << "asdf";
myfile.close();
只剩下一个问题:我必须指定确切的文件目录。程序将文件放在哪里,如果我不使用c:/users/schwing ....是否有可能使用"相对"路径?
我不得不提到,我正在生成DLL,并在另一个程序中运行该程序!
预先感谢!
相关文章:
- 如何从Windows控制台调用.exe(C++)以在不同的目录(或任何目录)中创建文件夹?
- 避免使用 boost::进程间::消息队列创建文件
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- 如何在软件代码中使用ofstream创建文件
- 在特定 Unicode 路径中创建文件
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 即使使用 FILE_FLAG_DELETE_ON_CLOSE 属性创建文件,文件也会保留在磁盘上
- C++ 为每个结构{人}条目创建文件
- "Access is Denied" U盘上的创建文件()
- 在c++编程中的文件夹中创建文件
- 创建文件夹结构
- 在C++中创建文件夹
- 创建文件函数是否可以打开仅在WinObj实用程序中的全局目录下列出的设备的句柄?
- 使用 C++ 或 SDL 在 Android 中创建文件
- 如何检测哪些进程更改,重命名或创建文件?
- 如何在 Linux 中通过 C/C++ 以其他用户身份创建文件?
- 挂钩创建文件抛出异常:读取访问冲突
- UWP 创建文件2 ERROR_ACCESS_DENIED "NUL"
- 如何指定创建文件函数获取未缓存的结果?
- Open() 用于创建文件和打开现有文件.有人能分辨出其中的区别