刚刚收买的书,迫不亟待的把第一代码敲进去,竟然一大堆错误。#include <sys/types.h>
第一个代码如下,我将其命名ls1.c
#include "apue.h"
#include <dirent.h>
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s/n", dirp->d_name);
closedir(dp);
exit(0);
}
出现错误后我就将这段代码复制到百度,找到了一个参考方案
http://blog.youkuaiyun.com/wfwd/archive/2008/02/19/2105111.aspx
将代码改成如下竟然运行成功:
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
{
printf("You need input the directory name./n");
exit(1);
}
if((dp = opendir(argv[1])) == NULL)
{
printf("cannot open %s/n", argv[1]);
exit(1);
}
while ((dirp = readdir(dp)) != NULL)
printf("%s/n", dirp->d_name);
closedir(dp);
exit(0);
}
我将这段代码命名为ls2.c
编译 bcc -Wall ls2.c -o ls2
然后允许 ./ls2 /home
运行成功
----------------
但是我想保持原有代码,我在这本书的附录里找到了原代码,并从网上找到了这本书的代码
apue.h 对应代码中figB.1,将其命名apue.h
输入输出错误对应figB.3,将其命名error.h
dirent.h的定义在网上搜
static DIR *opendir (const char *dirname);
static struct dirent *readdir (DIR *dirp);
static int closedir (DIR *dirp);
最后将这些文件保存在目录中,假如目录page6
ls1.c最终源文件
#include "apue.h"
#include "error.h"
#include <dirent.h>
int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL)
printf("%s/n", dirp->d_name);
closedir(dp);
exit(0);
}
我将这个包发给大家,将后坠gif去掉,晕,我的firefox不能上传
想要的话给我发站内信,
成功刚刚走出了第一步!后面的路还有很常!