这是linux下的C编程 将系统调用改为函数调用 希望可以学习一些细节和方法 麻烦各位高手了

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUF_SIZE 1024
#define READ_LEN 10
/**************************************************************
name:test_lseek
description:This function is use to test lseek function
par:fd/
offset/
where/
read_len/
return:
***************************************************************/
void test_lseek(int fd,off_t offset,int where,int read_len)
{
off_t result;
char temp[20],len;
result = lseek(fd,offset,where);
printf("lseek function options is SEEK_SET and return value is %d\n",result);
if (result != -1)
{
memset(temp,'\n',sizeof(temp));
if ((len=read(fd,temp,read_len))>0)
{
write(1,temp,(len+1));
}
}
else
{
perror("lseek function seek error occured:");
}
}

int main(int argc,char *argv[])
{
char buf[BUF_SIZE];
int in,out,len;

in = open("./main.c",O_RDONLY);
if (in == -1)
{
perror("Error occured when open file main.c:");
exit(1);
}

test_lseek(in,5,SEEK_SET,10);//Call test_lseek to test lseek function
test_lseek(in,5,SEEK_CUR,10);
test_lseek(in,-10,SEEK_END,10);

out = open("./mainnew.c",O_WRONLY | O_CREAT | O_EXCL,S_IRUSR | S_IWUSR | S_IRGRP);
if (out == -1)
{
out = open("./mainnew.c",O_WRONLY | O_TRUNC);
if (out == -1)
{
close(in);
perror("Error occured when open and create file mainnew.c:");
exit(1);
}
}

lseek(in,0,SEEK_SET) ;
while((len=read(in,buf,READ_LEN))!=0)
{
write(out,buf,len);
}
if ((close(in)==-1) || (close(out)==-1))
{
perror("Error occured when close file:");
exit(1);
}
exit(0);
}

这不就是函数调用吗,你是要改成标准c库函数来实现吗,那要损失不少平台相关的东西
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜