gpt4 book ai didi

c++ - 每次启动我的 C++ 程序时,如何将我的数据输出到一个新的 csv 文件中?

转载 作者:行者123 更新时间:2023-11-28 06:53:59 33 4
gpt4 key购买 nike

我要开发一个程序,提示用户输入数字,该程序将进行多次计算并将输出数据存储到 csv 文件中。

我的问题是:

每次之后如何将数据复制并存储到一个新的 .csv 文件而不是现有的文件中:

1) 我退出并重新启动程序2) 当程序在运行中突然终止时(例如:由于外部错误,我的电脑在运行程序时突然关闭)?

P.S:请用示例代码解决或向我解释,而不仅仅是 cz im new to c++。

提前致谢:)

#include <iostream>
#include <fstream>
#include<string>
#include <iomanip>
using namespace std;
int main()
{
float a,b,c;
ifstream indata;
ofstream outdata;
outdata.open("Out.csv", ios::app);//opens the csv file
outdata << "Num1,Num2,Answer" << endl;//'prompt user for numbers

while (!(a ==-100))//calculate the product of the numbers
{
cout<<"Enter Num1:";
cin>>a;
if(a==-100)break;
cout<<"Enter Num2:";
cin>>b;
c=a*b;
outdata<<a<<","<<b<<","<<c<< endl;
}

indata.open("out.csv");//stores data into the csv file
system("pause");
return 0;
}

最佳答案

1) 从当前日期和时间组装文件名。

2) 如果硬件突然停止工作或者进程被 kill -9 或任务管理器强行终止,您不会收到任何通知,因此您无法可靠地实现您似乎想要的。

'请用示例代码解决或向我解释,而不仅仅是单词 cz im new to c++'。通过编写代码并使其运行变得更好:)

关于c++ - 每次启动我的 C++ 程序时,如何将我的数据输出到一个新的 csv 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23404869/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com