c编程是系统调用吗_C编程中的文件管理系统调用

c编程是系统调用吗

The system call is a way for programs to interact with the operating system. When the program makes a system call at that time it makes a request to the operating system's kernel.

系统调用是程序与操作系统进行交互的一种方式。 当程序在那时进行系统调用时,它将向操作系统的内核发出请求。

There are 5 different categories of system calls:

有5种不同的系统调用类别

  1. Process Control

    过程控制

  2. File Management

    文件管理

  3. Device Management

    设备管理

  4. Information Management

    信息管理

  5. Communication

    通讯

Here, we will discuss about the system calls for file management in Unix system,

在这里,我们将讨论Unix系统中用于文件管理系统调用

There are four system calls for file management,

文件管理有四个系统调用

  1. open ()

    开启()

  2. read ()

    阅读()

  3. write ()

    写()

  4. close ()

    关 ()

1.打开() (1. open ())

open() system call is used to know the file descriptor of user-created files. Since read and write use file descriptor as their 1st parameter so to know the file descriptor open() system call is used.

open()系统调用用于了解用户创建的文件的文件描述符。 由于读写操作使用文件描述符作为第一个参数,因此要使用文件描述符open()系统调用。

Syntax:

句法:

    fd = open (file_name, mode, permission);
    Example:
    fd = open ("file", O_CREAT | O_RDWR, 0777);

Here,

这里,

  • file_name is the name to the file to open.

    file_name是要打开的文件的名称。

  • mode is used to define the file opening modes such as create, read, write modes.

    mode用于定义文件打开模式,例如创建,读取,写入模式。

  • permission is used to define the file permissions.

    权限用于定义文件权限。

Return value: Function returns the file descriptor.

返回值:函数返回文件描述符。

2.阅读() (2. read ())

read() system call is used to read the content from the file. It can also be used to read the input from the keyboard by specifying the 0 as file descriptor (see in the program given below).

read()系统调用用于从文件中读取内容。 通过将0指定为文件描述符,它也可以用于从键盘读取输入(请参见下面给出的程序)。

Syntax:

句法:

    length = read(file_descriptor , buffer, max_len);
    Example:
    n = read(0, buff, 50);

Here,

这里,

  • file_descriptor is the file descriptor of the file.

    file_descriptor是文件的文件描述符。

  • buffer is the name of the buffer where data is to be stored.

    buffer是要在其中存储数据的缓冲区的名称。

  • max_len is the number specifying the maximum amount of that data can be read

    max_len是一个数字,指定可以读取的最大数据量

Return value: If successful read returns the number of bytes actually read.

返回值:如果读取成功,则返回实际读取的字节数。

3.写() (3. write ())

write() system call is used to write the content to the file.

write()系统调用用于将内容写入文件。

Syntax:

句法:

    length = write(file_descriptor , buffer, len);
    Example:
    n = write(fd, "Hello world!", 12);

Here,

这里,

  • file_descriptor is the file descriptor of the file.

    file_descriptor是文件的文件描述符。

  • buffer is the name of the buffer to be stored.

    buffer是要存储的缓冲区的名称。

  • len is the length of the data to be written.

    len是要写入的数据的长度。

Return value: If successful write() returns the number of bytes actually written.

返回值:如果成功,write()返回实际写入的字节数。

4.关闭() (4. close ())

close() system call is used to close the opened file, it tells the operating system that you are done with the file and close the file.

close()系统调用用于关闭打开的文件,它告诉操作系统您已完成文件操作并关闭文件。

Syntax:

句法:

    int close(int fd);

Here,

这里,

  • fd is the file descriptor of the file to be closed.

    fd是要关闭的文件的文件描述符。

Return value: If file closed successfully it returns 0, else it returns -1.

返回值:如果文件成功关闭,则返回0,否则返回-1。

C代码演示系统调用示例 (C code to demonstrate example of system calls)

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

int main()
{
	int n,fd;
	char buff[50];	// declaring buffer 
	
	//message printing on the display
	printf("Enter text to write in the file:\n");
	//read from keyboard, specifying 0 as fd for std input device
	//Here, n stores the number of characters	
	n= read(0, buff, 50);

	// creating a new file using open.
	fd=open("file",O_CREAT | O_RDWR, 0777); 
	
	//writting input data to file (fd)
	write(fd, buff, n);
	//Write to display (1 is standard fd for output device)
	write(1, buff, n);

	//closing the file
	int close(int fd);
	
	return 0;	
}

Output

输出量

Enter text to write in the file:
Hello world, welcome @ IncludeHelp
Hello world, welcome @ IncludeHelp


翻译自: https://www.includehelp.com/c/file-management-system-calls.aspx

c编程是系统调用吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值