LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: menglianjing

请教大家问题

[复制链接]
发表于 2003-9-21 17:34:18 | 显示全部楼层
我邮箱一般都不用的
有什么问题在这里问好了
 楼主| 发表于 2003-9-22 18:06:11 | 显示全部楼层

re

/* 撼绦蛎17bserver */
/*  程序目的:观察LINUX行为 */
/*  程序作者:倩倩 */
#include <stdio.h>
#include <sys/time.h>
#include <getopt.h>
#include <stdlib.h>
#define BUF_SIZE 1024
const char *program_name;
void print_usage (FILE *stream, int exit_code)
{
        fprintf(stream,"Usage:%s options\n",program_name);
        fprintf(stream,"-h --help Display this usage information.\n");
        exit(exit_code);
}
int main(int argc, char *argv[])
{
        FILE procFile;
        char lineBuf[BUF_SIZE];
        struct timeval{
                long tv_sec;
                long tv_usec;
        };
        struct timeval tv;
        int next_option;
        const char *const short_options="h1:";
        const struct option long_options[]={
                {"help",1,NULL,'h'},
                {"load",1,NULL,'1'},
                {NULL,0,NULL,0}
        };
        program_name=argv[0];
        /* 处理命令行参数 */
        do
        {
                next_option=getopt_long(argc,argv,short_options,long_options,NULL);
                switch(next_option) {
                        case 'h':
                                print_usage(stdout,0);
                        case '1':
                                break;
                        case '?':
                                print_usage(stderr,1);
                        case '-1':
                                break;
                        default:
                                abort();
                }
        }while (next_option!=-1);
        /* 获取porc文件系统提供的信息 */
        gettimeofday(&tv,NULL);
        printf("内核初始报告 %s",ctime(&(tv.tv_sec)));
        procFile=fopen("/proc/sys/kernel/hostname","r");
        fgets(lineBuf,BUF_SIZE+1,procFile);
        printf("主机名为: %s",lineBuf);
        fclose(procFile);
        return 0;
}      
谢谢无双版主^-^,请多多指教。谢谢!!!也请大家多多指教。
这个程序的目的是检测CPU的类型和型号,所使用的LINUX内核版本,从系统最后一次启动以来已经经历了多长时间等等。
发表于 2003-9-22 21:37:24 | 显示全部楼层

  1. switch(next_option) {
  2. case 'h':
  3.   print_usage(stdout,0);
  4. case '1':
  5.   break;
  6. case '?':
  7.   print_usage(stderr,1);
  8. case '-1':
  9.   break;
  10. default:
  11.   abort();
  12. }
复制代码

这一段是什么意思呀?能不能说说你是怎么想的?
 楼主| 发表于 2003-9-22 21:53:56 | 显示全部楼层

re

是这样的,用于接收命令行的参数。看这
int main(int argc, char *argv[])
argc和argv是用于接收命令行的两参数,比如我输入了./oberver -l 10 600
则argc=4,argv[0]=oberver,argv[1]=-1,argv[2]=10,argv[3]=600.然后用函数getopt_long()接收进行处理。
发表于 2003-9-22 21:58:23 | 显示全部楼层
我知道是处理选项的,不过这段代码很怪,h打印,没有break,l什么都不做,还有?和-l是什么?long_options[]里没有。
 楼主| 发表于 2003-9-22 22:31:31 | 显示全部楼层

re

我是这样想的,用case '1'下的break退出,这样可以少写一个break,这样算偷懒吗?那在你的机子上出现什么提示信息呢?谢谢!
如果为1说明我只输入了oberver.后面没带参数。请多多指教。
 楼主| 发表于 2003-9-22 22:33:57 | 显示全部楼层

re

'-1'是函数getopt_long接收一个为定义的选项,处理完后返回的结果。
发表于 2003-9-28 10:53:17 | 显示全部楼层

回复: re

最初由 menglianjing 发表
/* 撼绦蛎?bserver */
/*  程序目的:观察LINUX行为 */
/*  程序作者:倩倩 */
#include <stdio.h>
#include <sys/time.h>
#include <getopt.h>
#include <stdlib.h>
#define BUF_SIZE 1024
const char *program_name;
void print_usage (FILE *stream, int exit_code)
{
        fprintf(stream,"Usage:%s options\n",program_name);
        fprintf(stream,"-h --help Display this usage information.\n");
        exit(exit_code);
}
int main(int argc, char *argv[])
{
        FILE procFile;
        char lineBuf[BUF_SIZE];
        struct timeval{
                long tv_sec;
                long tv_usec;
        };
        struct timeval tv;
        int next_option;
        const char *const short_options="h1:";
        const struct option long_options[]={
                {"help",1,NULL,'h'},
                {"load",1,NULL,'1'},
                {NULL,0,NULL,0}
        };
        program_name=argv[0];
        /* 处理命令行参数 */
        do
        {
                next_option=getopt_long(argc,argv,short_options,long_options,NULL);
                switch(next_option) {
                        case 'h':
                                print_usage(stdout,0);
                        case '1':
                                break;
                        case '?':
                                print_usage(stderr,1);
                        case '-1':
                                break;
                        default:
                                abort();
                }
        }while (next_option!=-1);
        /* 获取porc文件系统提供的信息 */
        gettimeofday(&tv,NULL);
        printf("内核初始报告 %s",ctime(&(tv.tv_sec)));
        procFile=fopen("/proc/sys/kernel/hostname","r");
        fgets(lineBuf,BUF_SIZE+1,procFile);
        printf("主机名为: %s",lineBuf);
        fclose(procFile);
        return 0;
}      
谢谢无双版主^-^,请多多指教。谢谢!!!也请大家多多指教。
这个程序的目的是检测CPU的类型和型号,所使用的LINUX内核版本,从系统最后一次启动以来已经经历了多长时间等等。


這裡欠了*
        FILE procFile;
這個根本不用
        struct timeval{
                long tv_sec;
                long tv_usec;
        };
這個有什麼用? 看?硪彩菦]有用的東西
                        default:
                                abort();

這是自己寫的嗎? 用kdevelop 吧,不錯的
 楼主| 发表于 2003-9-28 16:25:58 | 显示全部楼层

re

你说的是什么东西呀?我这里看到都是乱码^-^能在说一遍吗?谢谢你
发表于 2003-9-28 17:46:29 | 显示全部楼层
please change your character coding to bg18030
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表