2013-01-20 125 views
0
#include <string.h> 
#include <stdio.h> 
#include <windows.h> 
#include <wchar.h> 
#include <unistd.h> 
#define sleep(x) Sleep(1000 * x) 

int checkTime(); 

int main (int argc, char *argv[]) { 

    char *getFirstArgument = argv[1]; 
    char *getSecondArgument = argv[2]; 
    char getCheckTime; 

    checkTime(&getCheckTime); 

    if(*getFirstArgument != getCheckTime) { 
     sleep(1); 
     main(*getFirstArgument); 
    } else if(*getFirstArgument == getCheckTime && *getSecondArgument == 'r') { 
     system("shutdown /r"); 
    } else if(*getFirstArgument == getCheckTime) { 
     system("shutdown /s"); 
    } 

    return 0; 

} 

int checkTime() { 

    char getConvertedTime[5] = {}; 

    SYSTEMTIME localTime; 
    GetLocalTime (&localTime); 

    sprintf(getConvertedTime, "%d:%d", localTime.wHour, localTime.wMinute); 
    printf("%s\n", getConvertedTime); 

    return 0; 

} 

嗨!當我回想起主要功能時,我不知道我需要提出什麼論點,而且我真的找不到答案,我知道它存在於某個地方。 :)這裏是錯誤,告訴我MinGW編譯器。調用帶參數的主函數

$ gcc -Wall test.c -o test.exe 
test.c: in function 'main': 
test.c:20:3: error: to few arguments to function 'main' 
test.c:10:5: note: declared here 

對不起,我的英語不好!謝謝!

+1

C標準原則上禁止調用'main'的程序。 'main'函數應該被定義,但不會被調用.. –

+2

@Basile:這是C++,而不是C(除非在C11中進行了更改)。 – Mat

+0

正如你所看到的,從原型main接受一個int和一個字符串數組(char *),其中int應該是數組的元素個數 – x539

回答

2

而不是在1秒後調用main()創建某種循環等待指定的時間量,您可以使用實際循環!試試這樣的:

checkTime(&getCheckTime); 
while(*getFirstArgument != getCheckTime) { 
    sleep(1); 
    checkTime(&getCheckTime); 
} 

// Do something after the provided time 
if(*getSecondArgument == 'r') { 
    system("shutdown /r"); 
} else { 
    system("shutdown /s"); 
} 

此外,我真的不明白你打算做什麼。所以上面的代碼片段不可能修復你的完整程序。

+0

如果第一個參數描述的時間遠遠晚於程序啓動的時間,那麼這具有更重要的優點,即它不會耗盡堆棧空間。最初提出的遞歸解決方案在某些時候無疑會耗盡堆棧空間。 – Simon

+0

@Veger,現在我正在學習C語言,並且我正在嘗試做一個簡單的關閉程序,以瞭解程序的真正工作原理。謝謝你的回答,在修復後沒有更多的錯誤,但無論如何,該方案不起作用。無論如何,謝謝你的回答,我必須明白爲什麼我不能關閉電腦! :) – tourniquet