我想將資料寫入.txt檔案,由函式收集,main.cpp
然后通過MyFunction.h檔案傳遞,并使用MyFunction.cpp檔案進行記錄。
但是我找不到在一個函式中以 float 形式傳遞字串的方法。甚至不能傳遞一個字串。
我目前只是在學習 C ,所以請提供一些建議。
主程式

MyFunction.h

我的功能檔案

uj5u.com熱心網友回復:
我建議你看看這些功能:
- std::to_string : 將數值轉換為字串
- std::stoi / std::stof:將字串轉換為整數/浮點值
uj5u.com熱心網友回復:
首先,要將資料傳遞給檔案,您應該了解指令 fstrem 的作業原理應該包括什么
#include <fstream>
#include <cstdlib> // for exit function
首先我們應該創建輸出流,在我的例子中是一個陣列,它將被傳遞給檔案。
int main()
{
ofstream outdata; // outdata is like cin
int i; // loop index
int num[5] = {4, 3, 6, 7, 12}; // list of output values
outdata.open("example.txt"); // opens the file
if( !outdata ) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
for (i=0; i<5; i)
outdata << num[i] << endl;
outdata.close();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399286.html
上一篇:我如何讓正則運算式在某事之前取這個詞,但不抓住這個詞后面的模式后面的所有東西?[復制]
下一篇:在r中提取分號之間的字符
