分页: 1 / 1

[问题]Ubuntu里写C语言程序出错。

发表于 : 2005-08-19 20:20
林杰杰
小弟在写程序的时候,出现了一个错误,源程序如下:
#include <fcnt1.h>
#include <sys/stat.h>
#include <sys/types.h>

#define NEWFILE (O_WRONLY | O_CREAT | O_TRUNC)
#define MODE600 (S_IRUSR | S_IWUSR)
#define SIZE 1

void fatal(char* s)
{
printf("error when %s\n",s);
exit(-1);
}

void filecopy(char* infile,char* outfile)
{
char buf[SIZE];
int infd,outfd,count;

infd = open(infile,O_RDONLY);
if (infd == -1)
{
fatal("opening infile.\n");
}

outfd = open(infile,NEWFILE,MODE600);
if (outfd == -1)
{
close(infd);
fatal("opening outfile.\n");
}

while ((count = read(infd,buf,sizeof(buf))) > 0)
{
if (write(outfd,buf,count) != count)
{
fatal("writing data.\n");
}
}

if (count == -1)
{
fatal("reading data.\n");
}
close(infd);
close(outfd);
}

int main(int argc,char* argv[])
{
if (argc < 3)
{
printf("usage:filecopy <s-file> <d-file>");
return 1;
}
filecopy(argv[1],argv[2]);
return 0;
}

编译时出现这样的错:
lin@LIN:~/C++$ cc 2.c
2.c:1:19: fcnt1.h: No such file or directory
2.c: In function `filecopy':
2.c:20: error: `O_RDONLY' undeclared (first use in this function)
2.c:20: error: (Each undeclared identifier is reported only once
2.c:20: error: for each function it appears in.)
2.c:26: error: `O_WRONLY' undeclared (first use in this function)
2.c:26: error: `O_CREAT' undeclared (first use in this function)
2.c:26: error: `O_TRUNC' undeclared (first use in this function)
这是为什么呢?我是照着书打的。

发表于 : 2005-08-19 20:29
yonsan
#include <fcnt1.h>

将你的fcnt1.h改为fcntl.h就可以了! 那个是字母L的小写l而不是数字1

发表于 : 2005-08-19 21:29
林杰杰
yonsan 写了:#include <fcnt1.h>

将你的fcnt1.h改为fcntl.h就可以了! 那个是字母L的小写l而不是数字1
Faint...
谢谢这位兄弟了。