linux预习 4 linux环境
1.程序参数:int main(int argc , char *argv[])argc参数个数;argv参数本身的字符串数组
2.getopt
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ int opt;
while((opt = getopt(argc, argv, ":if:lr")) != -1) {
switch(opt) {
case 'i':
case 'l':
case 'r':
printf("option: %c\n", opt);
break;
case 'f':
printf("filename: %s\n", optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
for(; optind < argc; optind++)
printf("argument: %s\n", argv[optind]);
exit(0);
}
3.getopt_long
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <getopt.h>
int main(int argc, char *argv[])
{
int opt;
struct option longopts[] = {
{"initialize", 0, NULL, 'i'},
{"file", 1, NULL, 'f'},
{"list", 0, NULL, 'l'},
{"restart", 0, NULL, 'r'},
{0,0,0,0}};
while((opt = getopt_long(argc, argv, ":if:lr", longopts, NULL)) != -1) {
switch(opt) {
case 'i':
case 'l':
case 'r':
printf("option: %c\n", opt);
break;
case 'f':
printf("filename: %s\n", optarg);
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
for(; optind < argc; optind++)
printf("argument: %s\n", argv[optind]);
exit(0);
}
time 函数
time_t the_time;
the_time = time((time_t *)0);
临时文件:tmpnam返回一个不与任何一个已存在的文件同名的有效文件名;tmpfile 在命名的同时打开文件
浙公网安备 33010602011771号