如何使用read()和write()而不包含“unistd.h”?

时间:2021-05-27 15:08:51

I have used syscalls read() and write() in my program WITHOUT including "unistd.h" header file in the program. But still the program works and gives expected results.

我在程序中使用了syscalls read()和write(),但不包括“unistd”。在程序的头文件。但该计划仍然有效,并给出了预期的结果。

After running the program, i thought i will read the man page for read() and write().

运行这个程序之后,我想我将读取read()和write()的手册页。

In the man 2 page for read() and write(), in the SYNOPSIS section it is mentioned that I need to include unistd.h header file to use read() or write().

在read()和write()的man 2页面中,在大纲部分中提到我需要包括unistd。使用read()或write()的头文件。

SYNOPSIS
   #include <unistd.h>

   ssize_t read(int fd, void *buf, size_t count);


SYNOPSIS
   #include <unistd.h>

   ssize_t write(int fd, const void *buf, size_t count);

So I am surprised how did my program work although I had not included unistd.h ?

所以我很惊讶我的程序是如何工作的,尽管我没有包括unistd。h ?

Below is my program. It's a program to copy contents of a source file to target file using read(), and write() syscalls.

下面是我的计划。它是一个使用read()和write() syscalls将源文件的内容复制到目标文件的程序。

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

int main()
{
    /* Declaring the buffer. */
    /* Data read by read() will be stored in this buffer. */
    /* Later when write() is used, write() will take the contents of this buffer and write to the file.*/

    char buffer[512];

    /* Decalring strings to store the source file and target file names. */

    char source[128], target[128];

    /* Declaring integer variables in which integer returned by open() will be stored. */
    /* Note that this program will open a source file, and a target file. So, 2 integers will be needed. */

    int inhandle, outhandle;

    /* Declaring integer variable which will specify how much bytes to read or write.*/

    int bytes;

    /* Taking source filename from keyboard.*/

    printf("\nSource File name: ");
    scanf("%s",source);

    /* Open the source file using open().*/

    inhandle = open(source, O_RDONLY);

    /* If there is error while opening source file.*/

    if (inhandle == -1)
    {
            perror("Error opening source file.\n");
            exit(1);
    }

    /* Taking target filename from keyboard.*/

    printf("\nTarget File name: ");
    scanf("%s",target);

    /* Open the target file using open().*/

    outhandle = open(target, O_CREAT | O_WRONLY, 0660);

    /* If there is error while opening target file.*/

    if (outhandle == -1)
    {
            perror("Error opening target file.\n");
            close(inhandle);
            exit(2);
    }

     /* Below code does following:
       1. First reads (at most) 512 bytes from source file
       2. Then copies them to buffer
       3. If bytes read is greater than 0, write the content stored in buffer to target file.
    */

    while((bytes = read(inhandle, buffer, 512)) > 0)
    {
                    write(outhandle, buffer, bytes);
    }

    /* Close both source and target files. */
    close(inhandle);
    close(outhandle);

    return 0;
}

1 个解决方案

#1


0  

Your program worked because of implicit function declaration, read() and write() both return ssize_t and compiler the compiler assumes int when implicitly declaring functions, so it might work as you know.

由于隐式函数声明、read()和write()都返回ssize_t和编译器,所以编译器在隐式声明函数时假定为int,因此它可以工作。

If you compile your program with warnings enabled, then the compiler would warn you about that, using gcc

如果您在编译程序时启用了警告,那么编译器将使用gcc来警告您这一点

gcc -Wall -Wextra -Werror

would stop compilation if it finds implicitly declared functions, i.e. functions without a prototype.

如果发现隐式声明的函数,即没有原型的函数,将停止编译。

#1


0  

Your program worked because of implicit function declaration, read() and write() both return ssize_t and compiler the compiler assumes int when implicitly declaring functions, so it might work as you know.

由于隐式函数声明、read()和write()都返回ssize_t和编译器,所以编译器在隐式声明函数时假定为int,因此它可以工作。

If you compile your program with warnings enabled, then the compiler would warn you about that, using gcc

如果您在编译程序时启用了警告,那么编译器将使用gcc来警告您这一点

gcc -Wall -Wextra -Werror

would stop compilation if it finds implicitly declared functions, i.e. functions without a prototype.

如果发现隐式声明的函数,即没有原型的函数,将停止编译。