0
我的程序將提示用戶輸入數字,程序將計算數字的乘法。我想以這種格式將數據存儲到csv文件中 「」00:01 AM/02/05/14.csv「」使用C++程序輸出到.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);
}
Lynk ok ..現在我做了一些適當的改變,以脫身=「02:51 AM_01_05_14.csv」..但我仍然不能生成一個.csv文件..而我的程序創建了一個名稱爲02的文件是一個空白文件(0字節)。 但是,當我使用「outdata.open(date.c_str());」而不是「outdata.open(out.c_str());」我可以用名稱:01_05_14.csv生成一個csv文件。什麼導致這個錯誤? 爲什麼會發生此錯誤? – Thale13
@ Thale13:因爲您在運行Windows NT和NTFS的操作系統上,並且冒號':'被保留用於分隔文件名和文件流。 –
@ Thale13:看到http://msdn.microsoft.com/en-us/library/windows/desktop/aa364404%28v=vs.85%29.aspx –