如何在C 中使用特定信息创建一个TXT文件

how to create a txt file with specific information in c++

本文关键字:创建 文件 TXT 一个 信息      更新时间:2023-10-16

我的目标是创建一个.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,并在另一个程序中运行该程序!

预先感谢!