免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5866 | 回复: 12
打印 上一主题 下一主题

刚学unix环境高级编程,请指导一下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-12 17:50 |只看该作者 |倒序浏览
按照书上 抄了两个函数:
#include <errno.h>
#include <stdarg.h>
#include "ourhdr.h"

static void err_diot(int, const char *, va_list )
char *pname = NULL;
/*caller can set this from argv[0]*/

/*
nonfatal error related to a system call
print a message and return
*/
void err_ret(const char *fmt, ...)
{
    va_lsit ap;
    va_start(ap, fmt);
    err_doit(1, fmt, ap);
    va_end(ap);
    return;
   
}

/*
fatal error related to a system call
print a message and terminate
*/
void err_sys(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    va_doit(1, fmt, ap);
    va_end (ap)
    exit(1);
}

/*
fatal error related to a system call
print a messsage and dump core ,terminate
*/
void err_dump(const char * fmt)
{
    va_list ap;
    va_start(ap, fmt);
    va_doit(1, fmt, ap);
    va_end(ap);
    abort();
    exit(1);
      
}

/*
fatal error related to a system call
print a message and return
*/
void err_msg(char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    va_doit(0, fmt, ap);
    va_end(ap);
    retrun;
}

/*
fatal error related to a system call
print a message and terminate
*/
void err_quit(char *fmt, ...)
{
    va_list ap;
    va_start(ap,  fmt);
    va_doit(0, fmt, ap);
    va_end(ap);
    exit(1);
}

/*
print a message and return to caller
caller specifies "errnoflag"
*/
static void va_doit(int errnoflag, const char *fmt, va_lst ap)
{
    int errno_save;
    char buf[MAXLINE];

    errno_save = errno;/*value caller may want printed*/
    vsprintf(buf, fmt, ap);
    if (errnoflag)
    {
        sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
        
    }
    strcat(buf, "\n");
    fflush(stdout);  /*in case stdout and stderr are the same*/
    fputs(buf, stderr);
    fflush(NULL); /*flushes all stdio output stream*/

    return;
}

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include "myerr.c"

int main(int argc, char *argv[])
{
    DIR *dp;
    struct dirent *dirp;

    if (argc != 2)
    {
        err_quit("a single argument(the dir name )is required!!");
        
    }

    if (dp = opendir(argv[1]) == NULL)
    {
        err_sys("can not open the dir %s" ,argv[1]);
    }

    while (dirp = readdir(dp) != NULL)
    {
        printf("%s\n" , dirp->d_name);
    }

    closedir(dp);
    exit(0);
}


在linux下编译出错如下: 请大家帮助看看

linux:/opt/test # gcc opendir.c
In file included from opendir.c:4:
myerr.h: In function `err_doit':
myerr.h:6: error: parameter `pname' is initialized
myerr.h:14: error: parse error before '{' token
myerr.h:6: error: parm types given both in parmlist and separately
myerr.h:5: error: parameter name omitted
myerr.h:5: error: parameter name omitted
myerr.h:5: error: parameter name omitted
myerr.h:15: error: `va_lsit' undeclared (first use in this function)
myerr.h:15: error: (Each undeclared identifier is reported only once
myerr.h:15: error: for each function it appears in.)
myerr.h:16: error: `ap' undeclared (first use in this function)
myerr.h:16: error: `fmt' undeclared (first use in this function)
myerr.h:16: error: `va_start' used in function with fixed args
myerr.h: In function `err_sys':
myerr.h:33: error: parse error before "exit"
myerr.h: At top level:
myerr.h:41: error: conflicting types for `err_dump'
ourhdr.h:90: error: previous declaration of `err_dump'
myerr.h: In function `err_dump':
myerr.h:43: error: `va_start' used in function with fixed args
myerr.h: At top level:
myerr.h:56: error: conflicting types for `err_msg'
ourhdr.h:91: error: previous declaration of `err_msg'
myerr.h: In function `err_msg':
myerr.h:61: error: `retrun' undeclared (first use in this function)
myerr.h: At top level:
myerr.h:69: error: conflicting types for `err_quit'
ourhdr.h:92: error: previous declaration of `err_quit'
myerr.h:81: error: parse error before "va_lst"
myerr.h:82: warning: `va_doit' was declared implicitly `extern' and later `static'
myerr.h:72: warning: previous declaration of `va_doit'
myerr.h:82: warning: type mismatch with previous implicit declaration
myerr.h:72: warning: previous implicit declaration of `va_doit'
myerr.h:82: warning: `va_doit' was previously implicitly declared to return `int'
myerr.h: In function `va_doit':
myerr.h:87: error: `fmt' undeclared (first use in this function)
myerr.h:87: error: `ap' undeclared (first use in this function)
myerr.h:88: error: `errnoflag' undeclared (first use in this function)
opendir.c: In function `main':
opendir.c:17: warning: assignment makes pointer from integer without a cast
opendir.c:22: warning: assignment makes pointer from integer without a cast

[ 本帖最后由 yinian9826 于 2006-9-12 17:51 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-09-12 19:17 |只看该作者
楼主,你这是一个程序还是两个呀?

论坛徽章:
0
3 [报告]
发表于 2006-09-12 19:21 |只看该作者
头不见了...:wink:

论坛徽章:
0
4 [报告]
发表于 2006-09-12 20:03 |只看该作者
这么多错???

论坛徽章:
0
5 [报告]
发表于 2006-09-12 21:27 |只看该作者
#include "ourhdr.h"


这个头文件你有吗?

论坛徽章:
0
6 [报告]
发表于 2006-09-12 22:34 |只看该作者
楼上各位:
    这么多单词一般用什么词典查好呢,基本上看不懂。。。。:wink:

论坛徽章:
0
7 [报告]
发表于 2006-09-13 00:41 |只看该作者
天啦
无语...........
这孩子照书抄都抄错要是让作者知道了他肯定几天吃不下饭......

论坛徽章:
0
8 [报告]
发表于 2006-09-13 05:28 |只看该作者
偶刚看这本书时,也被这个头文件搞得头都大,不知去问谁。。。

后来细心些看看,原来作者精简了代码。。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2006-09-13 09:25 |只看该作者
照抄书是个不好的习惯,因为很容易抄错——只有自己理解了,按照作者的思路再做出来,才不会出错。
#include <errno.h>
#include <stdarg.h>
#include "ourhdr.h"

static void err_diot(int, const char *, va_list );
char *pname = NULL;

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2006-09-13 09:27 |只看该作者
至于把 va_list 写成了 va_lsit 和 va_lst 这种错误,都是因为没搞懂就照抄书造成的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP