#include <string.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
FILE *fp;
char txt[500];
int cmd;
int i;
time_t rawtime;
struct tm * timeinfo;
fp = fopen("msg.txt", "a+");
if (NULL==fp)
{
printf("open file error!\n");
return;
}
while (1)
{
printf("輸入命令(1:收短信,2:發短信, 0:退出):");
scanf("%d",&cmd);
switch(cmd) {
case 1:
i = 0;
fseek(fp, 0, SEEK_SET); //跳到文件開頭
while(NULL !=fgets(txt, 500, fp))
{
i++;
printf("msg[%d]: %s", i, txt);
}
break;
case 2:
fseek(fp, 0, SEEK_END); //從文件結尾開始寫入
time ( &rawtime );
timeinfo = localtime ( &rawtime );
asctime (timeinfo);
fprintf (fp, "[%4d-%02d-%02d %02d:%02d:%02d] ",1900+timeinfo->tm_year, 1+timeinfo->tm_mon, \
timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
printf("\n輸入命令短信內容(不超過500個字符):");
scanf("%s", txt);
fprintf(fp, "%s\n", txt);
break;
case 0:
fclose(fp);
return; //退出
default:
printf("未定義命令!\n");
break;
}
}
}