我有一个c ++程序来获取issuance.csv文件的最后一行数据(有id,日期,时间)并在其上写新记录 但是当我尝试在其上写入新数据(如id,时间,日期)时,保存空白行,而不在issu.c.c文件上存储数据,我该怎么办?
#define DEFAULT_ID "2000"
#define ISSUE_FILE_ADDRESS "/home/pi/file/issuance.csv"
int WriteNewRecord(string NewID, string NewCustomerID, string NewIssueDate)
{
fstream FileStream;
FileStream.open(ISSUE_FILE_ADDRESS, fstream::app | fstream::out);//Just for write into file
if(!FileStream.is_open())
{
cout << "File Open Error1" << endl;
return -1;
}
FileStream << NewID << "," << NewCustomerID << "," << NewIssueDate << endl;//Just for write into file
FileStream.close();
return 0;
}
int GetLastData(string& LastID, string& LastCustomerID, string& LastDate, string& LastTime)
{
fstream FileStream;
FileStream.open(ISSUE_FILE_ADDRESS, fstream::in);
if(!FileStream.is_open())
{
cout << "File Open Error2" << endl;
return -1;
}
string Line;
int Indices[3];
getline(FileStream, Line);
if(Line.length())
{
do {
Indices[0] = Line.find(",");
Indices[1] = Line.find(",", Indices[0] + 1);
Indices[2] = Line.find(",", Indices[1] + 1);
LastID = Line.substr(0, Indices[0]);
LastCustomerID = Line.substr(Indices[0] + 1, Indices[1] - Indices[0] - 1);
LastDate = Line.substr(Indices[1] + 1, Indices[2] - Indices[1] - 1);
LastTime = Line.substr(Indices[2] + 1, Line.length() - Indices[2] - 1);
getline(FileStream, Line);
} while (Line.length());
}
else
LastID = DEFAULT_ID;
FileStream.close();
return 0;
}
int time(){
time_t currentTime;
struct tm *localTime;
time( ¤tTime ); // Get the current time
localTime = localtime( ¤tTime ); // Convert the current time to the local time
int Day = localTime->tm_mday;
int Month = localTime->tm_mon + 1;
int Year = localTime->tm_year + 1900;
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
cout << Day << "/" << Month << "/" << Year << ", "<< Hour << ":" << Min << ":" << Sec<<endl;
//std::cout << std::endl;
return (currentTime);
}
int main()
{
string ID,CustomerID,Date,Time;
if(!GetLastData(ID, CustomerID, Date,Time))
{
cout << ID << " " << CustomerID << " " << Date << " " << Time << endl;
//Issuance Operation...
//...
//...
// if(!WriteNewRecord("2008", "100000", "4/29/2015" , "10:55:15"))//Example for test
string NewID,NewCustomerID,NewIssueDate;
if(!WriteNewRecord(NewID, NewCustomerID, NewIssueDate))
{
unsigned int new_id = atoi(ID.c_str()) + 1;
char tmp[5];
sprintf(tmp, "%d", new_id);
NewID = string(tmp);
tmp[4] = 0;
NewCustomerID=CustomerID;
char write[4];
unsigned int new_date=time();
memcpy(write,&new_date,2);
//cout << write << endl;
sscanf ( NewIssueDate.c_str(), "%d", &new_date );
cout<< NewIssueDate<<"," << NewCustomerID<<","<< NewID <<endl;
//cout << NewIssueDate<<endl;
return 0;
}
}
}
答案 0 :(得分:1)
如果您查看代码,则执行
string NewID,NewCustomerID,NewIssueDate, NewIssueTime;
if(!WriteNewRecord(NewID, NewCustomerID, NewIssueDate))
{
// Initialize variable but don't write them to the file
}
当您声明std::string
变量时,它们被初始化为空,因此将它们写入文件将不会写入任何内容。在将变量写入文件后,然后初始化变量。
答案 1 :(得分:0)
您可以在编写
之前尝试使用div {
width: 150px;
height: 150px;
overflow: scroll;
}