我的程序会提示用户输入数字,然后程序会计算数字的乘法。我想将我的数据存储到这种格式的 csv 文件中 ""00:01AM/02/05/14.csv""
这是为了确保每次我终止我的程序时,我都可以将我的数据存储到一个新的 .csv 文件中,而不是现有的文件中。但是由于我可能遇到的一些错误,我的程序没有输出到 csv 文件中没有修复。我不确定是什么导致了这个问题。 这是我的代码:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>
#include <string>
#include <sstream>
#include <time.h>
using namespace std;
string get_time();
string get_date();
int main()
{ //I can compile the codes but it is not outputting into a csv file
string date,time,out;
float a,b,c;
ifstream indata;
ofstream outdata;
date=get_date();
time=get_time();
time=time+'/';
out=time+date;//combines data&time into 12:01AM/02/05/14.csv string form
cout<<out<<endl;
//outputs the data into a csv file--but it is not working
outdata.open(out.c_str());
outdata << "Num1,Num2,Answer" << endl;
while (!(a ==-100))
{
cout<<"Enter Num1:";
cin>>a;
if(a==-100)break;
cout<<"Enter Num2:";
cin>>b;
c=a*b;
outdata<<a<<","<<b<<","<<c<< endl;
}
system("pause");
return 0;
}
string get_date()//converts date to string
{
time_t now;
char the_date[15];
the_date[0] = '\0';
now = time(NULL);
if (now != -1)
{
strftime(the_date,15, "%d/%m/%y.csv", gmtime(&now));
}
return string(the_date);
}
string get_time()//converts time to stirng
{
time_t currtime;
struct tm * timeinfo;
char the_time [12];
time (&currtime);
timeinfo = localtime (&currtime);
strftime (the_time,12,"%I:%M%p",timeinfo);
return string(the_time);
}
最佳答案
您知道斜杠是不能在文件名中使用的字符吗?斜杠又名“/”是目录分隔符。
即使在 Windows 上也是如此,因为 Windows 试图至少与 Unix 兼容一点点。
此外,这对您来说是一个很好的教训。始终检查错误代码。您应该检查一下您的 outdata.open
是否成功。
关于c++ - 使用 C++ 程序输出到 .csv 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23413442/