File I/O operations file Open, create, close and locate-based on Linux system file IO

Source: Internet
Author: User

1.

Open File

#include <sys/types.h>#include<sys/stat.h>#include<fcntl.h>int Open (constchar *pathname,int flag);    // Open an existing file int Open (constchar *pathname,int flags,mode_t mode);   // If the file does not exist, create it first // Success Returns the file description pair, otherwise returns-1 // pathname is the relative or absolute pathname of the file

The file descriptor starts at 3 (0,1,2 is occupied by standard IO) and is assigned to the open file in turn

Flags value:

o_rdonly;   Read-only o_wronly;  Write only o_rdwr; Read and write O_creat;o_excl;o_noctty;o_trung;o_append;o_nonbolck;o_nonelay;o_sync;

Mode take value

S_isuid; S_isgid; S_SVTX; S_IRUSR; S_IWUSR; S_IXUSR; S_IRGRP; S_IWGRP; S_IXGRP; S_iroth; S_iwoth; S_ixoth;

Combination mode

S_irwxu; S_IRWXG; S_irwxo;

Example:

Open File

1#include <sys/types.h>2#include <sys/stat.h>3#include <fcntl.h>4 5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8 9 #defineFLAGS o_wronly| O_creat| O_truncTen #defineMODES s_irwxu| s_ixgrp| s_irgrp| s_iroth| S_ixoth One  A intMainvoid) - { -     Const Char*pathname; the     intFD; -     Charpn[ -]; -printf"Please input the pathname <30 strings:\n"); -scanf"%s", PN); +Pathname=PN; -     if((Fd=open (pathname,flags,modes)) ==-1) +     { Aprintf"error can ' t open file! \ n"); atExit255); -     } -printf"OK, file has been open!\n"); -printf"fd=%d\n", FD); -     return 0; -}

2.

Create a file

#include <sys/types.h>#include<sys/stat.h>#include<fcntl.h>int creat (constchar *pathname,mode_t mode);

If successful, returns a file descriptor opened as write-only, otherwise 1

Note: creat is equivalent to

Open (pathname,o_wronly| O_creat| O_trunc,mode);

New ways to create files and open them

Open (pathname,o_rdwr| O_creat| O_trunc,mode);

3.

Close File

#include <unistd.h>int close (int fd);

FD is a file descriptor, successfully returned 0, error returned-1

4.

Locating files

#include <sys/types.h>#include<unistd.h>off_t lseek (int fd,off_t offset, int whence);

The displacement magnitude of the position at offset, which is related to the whence parameter

Seek_set;   // displacement is the offset byte at the beginning of the file. Seek_cur;   // offset byte from current position Seek_end;   // offset byte from end of file

The latter two parameters allow offset negative values

You can use this method to get the current amount of file displacement

off_t offset;offset=lseek (FD,0, seek_cur);

You can also use this method to test whether a file can be set at an offset

File I/O operations file Open, create, close and locate-based on Linux system 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.