c - 我如何在不包含 "unistd.h"的情况下使用 read() 和 write() ?

标签 c file-io header-files system-calls unistd.h

我在我的程序中使用了系统调用 read() 和 write(),但没有在程序中包含“unistd.h”头文件。但该程序仍然有效并给出了预期的结果。

运行程序后,我想我会阅读 read() 和 write() 的手册页。

在 read() 和 write() 的 man 2 页面中,SYNOPSIS 部分提到我需要包含 unistd.h 头文件才能使用 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);

虽然我没有包含 unistd.h,但我的程序如何运行,我很惊讶?

下面是我的程序。这是一个使用 read() 和 write() 系统调用将源文件的内容复制到目标文件的程序。

#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;
}

最佳答案

你的程序因为隐式函数声明而工作,read()write() 都返回 ssize_t 并且编译器假定编译器 int 当隐式声明函数时,它可能会像您所知的那样工作。

如果您在启用警告的情况下编译您的程序,那么编译器会使用 gcc 向您发出警告

gcc -Wall -Wextra -Werror

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

关于c - 我如何在不包含 "unistd.h"的情况下使用 read() 和 write() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28549909/

相关文章:

c - c!='\n'在for语句中做什么?

c - 如何从文本文件中的制表符分隔字段中正确获取数据

objective-c - 包含 .h 文件时出现重复符号

java - FileUpload引发异常-帮助

java - 是否可以从小程序读取/写入文件

C++ 到 Delphi 头文件转换 : function with pointer to void

header-files - 用automake将头文件放在/usr/include的子目录中?

c - C:int函数返回错误值

c - 一个进程的文件,mm_struct和files_struct中文件的关系?

c - 在 C 中给定开始和结束内存地址的意外行为遍历数组