请教各位大虾关于TIME()函数在C语言的用法
小弟遭遇编程难题,老师让看一个代码,居然编译通不过,很是苦恼,请教大家!
以下是源代码,编译时总是告诉我有错误,总是说“time_t”: 将此类型用作表达式非法”,问题之处我已经标识了绿色,希望大虾们帮小弟解答一下这个问题,再次感谢!
#ifndef _TIME_T_DEFINED
typedef long time_t; /* 时间值 */
#define _TIME_T_DEFINED /* 避免重复定义 time_t */
#endif
#include <stdio.h>
#include <time.h>
#include <string.h>
FILE *scorefile;
int get_score(char *name, char *ssn, char *score);
char* str_prefix(char *prefix, char *str);
int main(int argc, char *argv[])
{
int ruid, euid;
char score[128];
if (argc != 3) {
printf("Usage: getscore name SSN\n");
exit(1);
}
time_t current_time = time(NULL);
ruid = getuid ();
euid = geteuid ();
// This is to make sure the logging command will have
// sufficient privilege.
if (setreuid(euid, euid)){
perror("setreuid");
}
scorefile = fopen("score.txt", "r");
if (scorefile == NULL){
printf ("failed to open score file\n");
}
else{
if (get_score(argv[1], argv[2], score)){
char command[256];
printf("Invalid user name or SSN.\n");
sprintf(command, "echo \"%s: Invalid user name or SSN: %s,%s\"|cat >> error.log",
ctime(¤t_time), argv[1], argv[2]);
if (system(command)){
perror("Logging");
}
exit(-1);
}
printf("Your score is %s\n", score);
}
}
int get_score(char *name, char *ssn, char *score)
{
char matching_pattern[128];
char line[128];
char *match_point;
strcpy(matching_pattern, name);
strcat(matching_pattern, ":");
strcat(matching_pattern, ssn);
while (fgets(line, 128, scorefile)!=NULL){
if (match_point=str_prefix(matching_pattern, line)){
if (*match_point++==':'){
while (*match_point!=':'){
*score++=*match_point++;
}
*score=0;
return 0;
}
}
}
return -1;
}
char* str_prefix(char *prefix, char *str){
while (*prefix && *str){
if (*prefix != *str)
return NULL;
prefix++;
str++;
}
return *prefix==0?str:NULL;
}