Linux system programming File IO

Source: Internet
Author: User
Tags file copy

1. Opening the file's function open, the first parameter represents the file path name, the second is the open tag, and the third one is the file permission

Code:

#include <sys/types.h><sys/stat.h><fcntl.h>#include<stdio.h> int Main () {        int  fd;         = Open ("testopen1", O_creat,0777);        printf ("fd =%d\n", FD);         return 0 ;}

Effect test: Print Open File Returns a descriptor of 3 while creating a file Testopen1

2. Create file functions creat and close functions close

Using code

#include <fcntl.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>intMainintargcChar*argv[]) {        intFD; if(ARGC <2) {printf ("Please run./app filename\n"); Exit (1); } FD= Creat (argv[1],0777); printf ("FD =%d\n", FD);        Close (FD); return 0;}

Test results:

3. Write the file function write, the first parameter represents the descriptor of the file to be written, the second parameter represents the first memory address of the content to be written, and the third parameter represents the number of bytes to write to the content;

Write successfully returns the number of bytes written, failure returns-1

Test code:

#include <sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>intMainintargcChar*argv[]) {        intFD; if(ARGC <2) {printf ("Please run./app filename\n"); Exit (1); } FD= Open (argv[1],o_creat | O_RDWR,0777); if(FD = =-1) {printf ("Open File failed!"); Exit (0); }        Char*con ="Hello world!"; intnum =Write (Fd,con,strlen (con)); printf ("write%d byte to%s", num,argv[1]);        Close (FD); return 0;}

Result: Hello world! Write Hello file successfully

View the maximum number of open files in the current system

4. Reading the file read, the first parameter is the file descriptor, the second parameter is the memory to read the file contents of the first address, the third parameter is the number of bytes read at a time

Test code, complete file copy

#include <sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>intMainintargcChar*argv[]) {        intFd_src,fd_des; Charbuf[1024x768]; if(ARGC <3) {printf ("Please run./app srcfilename desfilename\n"); Exit (1); } fd_src= Open (argv[1],o_rdonly); Fd_des= Open (argv[2],o_creat | o_wronly | O_trunc,0777); if(Fd_src! =-1&& Fd_des! =-1)        {                intLen =0;  while(len = Read (Fd_src,buf,sizeof(BUF)))                {Write (Fd_des,buf,len);                } close (FD_SRC);        Close (fd_des); }        return 0; }

Effect view, visible to achieve a copy of the file open.c

Linux system programming File IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.