2013-01-20 123 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