2016-06-27 1 views
-1

カレンダーリマインダとして機能する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 「TV 「表示」はリマインダ自体であり、1は「毎日」のリピートフラグ値を意味するため、時間('12 ')と分('00')のみを考慮に入れます。

誰かが問題を正確に指摘できますか?すべての訂正、最適化、提案、建設的な批判は大いに感謝しています。どうもありがとうございました!

+0

建設的な批評#1は、コードの壁と問題の最小限の詳細を掲示せず、人々が自分自身でそれを再現することを期待してはいけません。あなたは[最小完全な検証可能な例](http://stackoverflow.com/help/mcve)が必要です。 –

+0

右私は非常に申し訳ない!私は進行中です。 –

答えて

1

最初のループはファイル全体を読み取り、2番目のループは何も読み取られません。

おそらくdb_in.seekg(0)にします。

+0

たわごと!最初のループは、多くのEventFormatオブジェクトを作成する行数をカウントします。他の方法はありますか?ありがとう! –

+0

Brainwave! seekp(0、ios :: beg)を単に追加することはできますか?それは働くだろうか? –

+0

@AnandSはい、ファイルを最初に巻き戻します。代わりに 'Std :: vector'を使って' Event'sを保持することもできます。正しいサイズを割り当てることを心配する必要はありません。 – kfsone