我尝试使用C ++创建一个后台实用程序,它可以作为日历提醒。该程序编译并运行没有任何障碍,但它不会发出警报,当它应该...时,这是代码:
#include<Windows.h>
#include<fstream>
#include<conio.h>
#include<string>
#include<ctime>
char Caption[] = "AshTech EventSentinel";
struct EventFormat
{
char *notes; // Notes for the event
int repeat; // Repeat flag (0-No, 1-Daily, 2-Weekly, 3-Monthly, 4-Yearly)
int day; // Day of week (0-Sunday, 1-Monday,..., 6-Saturday)
int hour; // Hour of day (0-23 hours)
int min; // Minute of hour (0-59 minutes)
int date; // Date of month (1-31 days)
int mon; // Month of year (0-January, 1-February,..., 11-December)
int year; // Year of event
};
void Alert(char*);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char *filepath = "C:\\Windows\\EventSentinel_db.esdb";
ifstream db_in(filepath);
// If file exists, enter program.
if(db_in)
{
int RecordNum, i;
string FileRecord;
i = RecordNum = 0;
// Calculate number of lines in the file...
while(getline(db_in, FileRecord))
if (!FileRecord.empty())
RecordNum++;
EventFormat *Event;
time_t rawtime;
struct tm* timeinfo;
Event = new EventFormat[RecordNum];
/********* EDITED ********/
// Initializes all objects created to -1
for(i=0;i<RecordNum;i++)
{
Event[i].date =
Event[i].day =
Event[i].hour =
Event[i].min =
Event[i].mon =
Event[i].repeat =
Event[i].year = -1;
}
db_in.seekg(0); // Reposition get pointer at the 0th byte...
/*************************/
// Load struct array.
while (!db_in.eof())
{
// EXCEPTION THROWN IN THIS STATEMENT BELOW...
db_in.getline(Event[i].notes,','); // Assign input to Event[i].notes until ',' is encountered.
db_in >> Event[i].repeat;
switch (Event[i].repeat)
{
case 0: // No repeat - single instance.
{
db_in >> Event[i].date;
db_in >> Event[i].mon;
db_in >> Event[i].year;
db_in >> Event[i].hour;
db_in >> Event[i].min;
}break;
case 1: // Repeat daily
{
db_in >> Event[i].hour;
db_in >> Event[i].min;
}break;
case 2: // Repeat weekly
{
db_in >> Event[i].day;
db_in >> Event[i].hour;
db_in >> Event[i].min;
}break;
case 3: // Repeat monthly
{
db_in >> Event[i].date;
db_in >> Event[i].hour;
db_in >> Event[i].min;
}break;
case 4: // Repeat yearly
{
db_in >> Event[i].mon;
db_in >> Event[i].date;
db_in >> Event[i].hour;
db_in >> Event[i].min;
}
}
}
// Close file.
db_in.close();
// Main Loop.
while (1)
{
time(&rawtime);
timeinfo = localtime(&rawtime);
for (i = 0; i < RecordNum; i++)
{
switch (Event[i].repeat)
{
// No Repeat
case 0:
{
if (
timeinfo->tm_year == Event[i].year&&
timeinfo->tm_mon == Event[i].mon&&
timeinfo->tm_mday == Event[i].date&&
timeinfo->tm_hour == Event[i].hour&&
timeinfo->tm_min == Event[i].min
)
Alert(Event[i].notes);
}break;
// Repeat Daily
case 1:
{
if (
timeinfo->tm_hour == Event[i].hour&&
timeinfo->tm_min == Event[i].min
)
Alert(Event[i].notes);
}break;
// Repeat Weekly
case 2:
{
if (
timeinfo->tm_wday == Event[i].day&&
timeinfo->tm_hour == Event[i].hour&&
timeinfo->tm_min == Event[i].min
)
Alert(Event[i].notes);
}break;
// Repeat Monthly
case 3:
{
if (
timeinfo->tm_wday == Event[i].date&&
timeinfo->tm_hour == Event[i].hour&&
timeinfo->tm_min == Event[i].min
)
Alert(Event[i].notes);
}break;
// Repeat Yearly
case 4:
{
if (
timeinfo->tm_mon == Event[i].mon&&
timeinfo->tm_wday == Event[i].date&&
timeinfo->tm_hour == Event[i].hour&&
timeinfo->tm_min == Event[i].min
)
Alert(Event[i].notes);
}break;
}
}
Sleep(10000);
}
delete[] Event;
}
// If file doesn't exist, create new file, and exit program.
else
{
ofstream db_new(filepath);
db_new.close();
MessageBox(
NULL,
"File did not exist. New file has been created. Please add some events to it, for EventSentinel to handle.",
"EventSentinel - Error detected!",
MB_ICONEXCLAMATION | MB_OK);
}
return 0;
}
void Alert(char* notes)
{
strcat(notes, "\r\n\n\nPress <ESC> to stop...");
int KeyPress = -1;
MessageBox(NULL, notes, Caption, MB_ICONINFORMATION);
while (KeyPress != VK_ESCAPE)
{
if (_kbhit())
KeyPress = getch;
Beep(4000, 100);
Sleep(25);
Beep(4000, 100);
Sleep(25);
Beep(4000, 100);
Sleep(25);
Beep(4000, 100);
Sleep(525);
}
}
以下是数据库文件的示例输入:TV Show,1 12 00
其中“电视节目”是提醒本身,1表示“每日”重复标志值,因此只需要小时('12')和分钟('00')。
有人可以在这里查明问题吗?所有更正,优化,建议和建设性批评都非常感激。非常感谢你!
答案 0 :(得分:1)
while(getline(db_in, FileRecord))
if (!FileRecord.empty())
RecordNum++;
...
// Load struct array.
while (!db_in.eof())
第一个循环读取整个文件,第二个循环不读任何内容。
您可能想要db_in.seekg(0)
。