Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序使用Unix系统调用进行I/O_C_Unix - Fatal编程技术网

C程序使用Unix系统调用进行I/O

C程序使用Unix系统调用进行I/O,c,unix,C,Unix,我的教授让我写一个简单的C程序,然后让我使用Unix系统调用进行转换。我尝试改变周围的值,但没有任何效果 要求: 编写一个新的C程序newcat,它的性能与oldcat完全相同,但使用以下UNIX系统调用进行I/O int read(int fd, char *buf, int n); int write(int fd, char *buf, int n); int open(char *name, int accessmode, int permission); int close(int f

我的教授让我写一个简单的C程序,然后让我使用Unix系统调用进行转换。我尝试改变周围的值,但没有任何效果

要求: 编写一个新的C程序newcat,它的性能与oldcat完全相同,但使用以下UNIX系统调用进行I/O

int read(int fd, char *buf, int n);
int write(int fd, char *buf, int n);
int open(char *name, int accessmode, int permission);
int close(int fd);
要打开文件进行读取,可以使用fcntl.h头文件中定义的符号常量O_RDONLY来指定accessmode。只需传递0以获得权限。也就是说,代码将显示如下:

fd = open (filename, O_RDONLY, 0); 
您将需要以下头文件:sys/types.h、unistd.h和fcntl.h

#include <stdio.h>
/* oldcat: Concatenate files */
int main(int argc, char *argv[])
{
   void filecopy(FILE *, FILE *); /* prototype for function */
   int fd = open(*fp, O_RDONLy,0)
   char *prog = argv[0]; /* program name for errors */
   if (argc == 1) /* no args; copy standard input */
      filecopy(0, 1);
  else 
      while (--argc > 0)
         if (fd == -1) {
            fprintf(stderr, "%s: can't open %s\n", prog, *argv);
            return(-1);
         } else {
            filecopy(fp, 1);
            fclose(fp);
     }
  return(0);
}
/* filecopy: copy file ifp to ofp */
void filecopy(FILE *ifp, FILE *ofp)
{
   int c;
   while ((c = getc(ifp)) != EOF)
      putc(c, ofp);
}
#包括
/*oldcat:连接文件*/
int main(int argc,char*argv[])
{
作废文件副本(文件*,文件*);/*函数原型*/
int fd=打开(*fp,仅O_RDONLy,0)
char*prog=argv[0];/*错误的程序名*/
如果(argc==1)/*无args;复制标准输入*/
文件副本(0,1);
其他的
而(--argc>0)
如果(fd==-1){
fprintf(stderr,“%s:无法打开%s\n”,prog,*argv);
返回(-1);
}否则{
文件副本(fp,1);
fclose(fp);
}
返回(0);
}
/*filecopy:将文件ifp复制到ofp*/
无效文件副本(文件*ifp,文件*ofp)
{
INTC;
而((c=getc(ifp))!=EOF)
putc(c,ofp);
}
这就是写作的想法吗?它仍然无法编译:

#include <stdio.h>
/* oldcat: Concatenate files */
int main(int argc, char *argv[])
{
   void filecopy(int ifp, int ifo); 

   int fd = open(*File,O_RDONLY,0);  //is this correct?
   char *prog = argv[0]; 
   if (argc == 1) /* no args; copy standard input */
      filecopy(0, 1); //is this correct?
  else 
      while (--argc > 0)
         if ((fd == -1)  //is this correct?{
            fprintf(stderr, "%s: can't open %s\n", prog, *argv); 
            return(-1);
         } else {
            filecopy(*FILE, 1);//is this correct?
            close(*FILE);//is this correct?
     }
  return(0);
}
/* filecopy: copy file ifp to ofp */
void filecopy(FILE *ifp, FILE *ofp)//NO CLUE HOW THIS SHOULD BE
{
   int c;
   while (c = read(fd ,&something,1)//What is &ch/&something?
      putc(c, ofp);
}
#包括
/*oldcat:连接文件*/
int main(int argc,char*argv[])
{
无效文件副本(int ifp、int ifo);
int fd=open(*文件,O_RDONLY,0);//这是否正确?
char*prog=argv[0];
如果(argc==1)/*无args;复制标准输入*/
filecopy(0,1);//是否正确?
其他的
而(--argc>0)
if((fd==-1)//是否正确{
fprintf(stderr,“%s:无法打开%s\n”,prog,*argv);
返回(-1);
}否则{
filecopy(*FILE,1);//是否正确?
关闭(*文件);//这是否正确?
}
返回(0);
}
/*filecopy:将文件ifp复制到ofp*/
void filecopy(FILE*ifp,FILE*ofp)//不知道该怎么做
{
INTC;
while(c=read(fd,&something,1)//什么是&ch/&something?
putc(c,ofp);
}

假设您的
oldcat
使用C标准库调用(如
fopen
),只需将这些调用映射到UNIX调用即可

在高级别:

fopen  -> open
fread  -> read
fwrite -> write
fclose -> close
例如,使用以下命令打开输入文件时:

FILE *fIn = fopen ("jargon.txt", "r");
您可以改为使用:

int inFd = open ("jargon.txt", O_RDONLY, 0);
其他调用非常相似,在C标准库和UNIX系统调用级别具有类似的功能。通常可以从手册页中获取这些调用的详细信息,方法是在shell中输入类似于
man 2 open
的内容,或者将
man open
插入您喜爱的搜索引擎

唯一“棘手”的映射是,如果您使用了
getchar/putchar
样式的调用来执行实际的读写操作,但是当您意识到(例如)读取字符在功能上与读取大小为1的块相同时,这也变得很容易:

int c = getc (fIn);
或:


关于你补充的问题:

所以打开一个文件:if(fd=open(fp,O_RDONLY,0);)=NULL)

不完全正确。
fopen
函数在出错时返回NULL,因为它返回指向
文件
结构的指针

较低级别的调用使用文件描述符而不是文件句柄,前者是一个小的整数值。因此,不是:

FILE *fp = fopen ("nosuchfile", "r");
if (fp == NULL) doSomethingIntelligent();
您可以执行以下操作:

int fd = open ("nosuchfile", O_RDONLY, 0);
if (fd == -1) doSomethingIntelligentUsing (errno);

就您需要更改的内容而言,以下内容出自我的脑海(因此可能不完全详尽,但应该是一个很好的开始):

  • 添加所需的标题
  • 完全停止使用
    FILE*
    ,改用
    int
  • fopen/fclose
    调用转换为
    open/close
    。这包括函数名、不同参数和不同返回类型
  • 修改
    filecopy
    以使用文件描述符而不是文件句柄
  • 调用
    filecopy
    时,请使用
    1
    而不是
    stdout
    (后者是
    文件*

作为如何执行此操作的示例,以下程序
testprog.c
将读取自身并将每个字符回显到标准输出:

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

int main (void) {
    int num, ch, inFd;

    // Open as read only.

    inFd = open ("testprog.c", O_RDONLY, 0);
    if (inFd == -1)
        printf ("\n**Error %d opening file\n", errno);

    // Get and output esach char until EOF/error.

    while ((num = read (inFd, &ch, 1) != 0) == 1)
        putchar (ch);

    // Detect error.

    if (num != 0)
        printf ("\n**Error %d reading file\n", errno);

    // Close file and exit.

    close (inFd);

    return 0;
}
#包括
#包括
#包括
内部主(空){
int num,ch,inFd;
//以只读方式打开。
inFd=打开(“testprog.c”,Ordonly,0);
如果(inFd==-1)
printf(“\n**打开文件时出现错误%d”,errno);
//获取并输出esach char,直到EOF/error。
while((num=read(inFd,&ch,1)!=0)==1)
putchar(ch);
//检测错误。
如果(num!=0)
printf(“\n**读取文件时出现错误%d”,errno);
//关闭文件并退出。
关闭(inFd);
返回0;
}

请注意,
linux系统调用的文档
出现在
手册
中,称为
手册
,您可以在
linux
系统的bash shell中使用
man
命令访问该手册。因为UNIX和linux非常相似(可能相当)对于您感兴趣的系统调用,可以在Linux中查看这些系统调用的手册页

所有四个
读取
写入
打开
关闭
linux系统调用在
手册页
中进行了说明。您可以通过在shell中键入以下命令来访问这些系统调用的手册页:

man 2 read
man 2 write
man 2 open
man 2 close
这些可能会引导您走向正确的方向。

#包括
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
/* newcat: Concatenate files */
int main(int argc, char *argv[])
    {
   void filecopy(int ifp, int ofp); /* prototype for function */

   int fd;
   char *prog = argv[0]; /* program name for errors */
   if (argc == 1) /* no args; copy standard input */
  filecopy(0,1);
   else 
      while (--argc > 0)
         fd = open(*++argv , O_RDONLY,0);
         if ( fd == -1) {
            fprintf(stderr, "%s: can't open %s\n", prog, *argv);
            return(-1);
         } else {
            filecopy(fd, 1);
           close(fd);
         }
   return(0);
}
/* filecopy: copy file ifp to ofp */
void filecopy(int ifp, int ofp)
{
   int c;
   while (read(ifp,&c,ofp ) != 0)
      write(ofp,&c,ofp);
}
#包括 #包括 #包括 /*newcat:连接文件*/ int main(int argc,char*argv[]) { void filecopy(int-ifp,int-ofp);/*函数原型*/ int-fd; char*prog=argv[0];/*错误的程序名*/ 如果(argc==1)/*无args;复制标准输入*/ 文件副本(0,1); 其他的 而(--argc>0) fd=打开(*++argv,仅限O_rdo,0); 如果(fd==-1){ fprintf(标准,“%s:无法打开%s\n”,程序,
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
/* newcat: Concatenate files */
int main(int argc, char *argv[])
    {
   void filecopy(int ifp, int ofp); /* prototype for function */

   int fd;
   char *prog = argv[0]; /* program name for errors */
   if (argc == 1) /* no args; copy standard input */
  filecopy(0,1);
   else 
      while (--argc > 0)
         fd = open(*++argv , O_RDONLY,0);
         if ( fd == -1) {
            fprintf(stderr, "%s: can't open %s\n", prog, *argv);
            return(-1);
         } else {
            filecopy(fd, 1);
           close(fd);
         }
   return(0);
}
/* filecopy: copy file ifp to ofp */
void filecopy(int ifp, int ofp)
{
   int c;
   while (read(ifp,&c,ofp ) != 0)
      write(ofp,&c,ofp);
}