Linux file Operation programming

Source: Internet
Author: User
Tags error handling fread int size

All of the devices and files in Linux are manipulated using file descriptors.

A file descriptor is a non-negative integer that is an index value that points to the record table opened by each process in the kernel.

When a file is opened or a new file is created, the kernel returns a file descriptor to the process, and the file descriptor is passed as a parameter to the corresponding function when it is necessary to read and write the file.

A process will open 3 files: standard input, standard output, and standard error handling. Corresponds to 0, 1, 2 of the file descriptor, respectively.

Rename function

#include <stdio.h>

int rename (const char *oldpath, const char *newpath);

Description: OldPath is the original path to the file, and NewPath is the new path to the file.

Function: File Rename. Return value: Run successfully returns 0.

Remove function

#include <stdio.h>

int remove (const char *pathname);

Description: Pathname is the path to the file. Function: Delete files. Return value: Run successfully returns 0.

Chown function

#include <sys/types.h>

#include <unistd.h>

int chown (const char *path, uid_t owner, gid_t Group);

Note: path is the path to the file to be modified, the owner specified by owners, and group is the set of filegroups. Function: Modify the owner of the file. Return value: Run successfully returns 0, otherwise-1.

The chmod function.

#include <sys/types.h>

#include <sys/stat.h>

int (const char *path, mode_t mode); Description: Modify access permissions for a file

GETCWD function

#include <unistd.h>

Char *getcwd (char *buf, size_t size);

Description: BUF is the name of the directory returned, buf and size can be set at the same time for the 0 return value: Success is the directory name error-1

#include <unistd.h>

#include <sdtio.h>

int main (int argc, char* argv[])

{

ChDir ("/tmp");

printf ("Current working directory:%s\n", GETCWD (Null,null));

}

Perform current working directory:/tmp

chdir function #include <unistd.h> i

NT ChDir (const char *path);

Description: Specifies the directory path. Function: Changes the current working directory.

Return value: Run successfully returns 0, otherwise-1.

Opendir function

#include <sys/types.h>

#include <dirent.h>

DIR *opendir (const char *name); Description: Specifies the directory path.

Function: Open Directory.

Return value: Run the directory stream that successfully returned the directory, otherwise null.

Closedir function

#include <sys/types.h>

#include <dirent.h>

int Closedir (DIR *dir);

Description: The directory that was opened. Function: Closes the Open directory.

Return value: Run the directory stream that successfully returned the directory, otherwise null.

Readdir function

#include <sys/types.h>

#include <dirent.h>

struct dirent * Readdir (DIR *dir);

Description: Reads the name of a file in the directory and moves the directory stream pointer back.

Function: Reads the name of a file in the directory and moves the directory stream pointer back.

Return value: Returns the name of a file, to the end of the directory, or to return NULL if an error occurs.

struct Dirent {

ino_t D_ino;

ff_t d_ff;

signed short int d_reclen;

unsigned char d_type;

Char d_name[256];

};

Telldir function

#include <sys/types.h>

#include <dirent.h>

off_t Telldir (DIR *dir); Description: Specifies the directory stream. Function: Gets the current pointer position of the specified directory stream.

Return value: Run successfully returns the current pointer position, otherwise-1.

Seekdir function

#include <sys/types.h>

#include <dirent.h>

void Seekdir (DIR *dir, off_t offset);

Description: Dir is the specified directory stream, offset is the specified pointer location feature: Sets the pointer location for the specified directory stream.

return value: None.

fopen function

#include <stdio.h>

FILE *fopen (const char *path, const char *mode);

Description: Mode is the file open pattern, R open in read-only form, r+ can read and write form open, the contents of the file is emptied, a, open by appending, the file is not saved is new, A + read-write form open, file does not exist new.

Function: Open file, get file stream pointer. Return value: The stream pointer for the file.

#include <stdio.h>

Main () {

FILE * FP =fopen (0, "w+");

fprintf (FP, "%s\n", "hello!");

Fclose (FP);}

fclose function

#include <stdio.h> int fclose (FILE *stream);

Description: A file stream pointer. Function: Closes a file that has been opened.

Return value: Run successfully returns the current pointer position, otherwise EOF.

Ftell function

#include <stdio.h> long Ftell (FILE *stream);

Description: A file stream pointer. Function: Gets the current read and write location of the file stream pointer.

Return value: Run successfully returns the current read-write location, otherwise-1.

fseek function

#include <stdio.h>

int fseek (FILE *stream, long offset, int whence);

Description: A file stream pointer.

Function: Stream is the specified file stream pointer, offset is the specified read-write location,

Whence is the benchmark for setting the read-write location. Return value: Run successfully returns 0, otherwise-1.

#include <stdio.h>

Main () {

FILE * Stream;long offset;

fpos_t POS;

Stream=fopen ("/etc/passwd", "R");

Fseek (Stream,5,seek_set);

printf ("offset=%d\n", Ftell (stream));

Rewind (stream);

Fgetpos (Stream,&pos);

printf ("offset=%d\n", POS);

pos=10;

Fsetpos (Stream,&pos);

printf ("offset =%d\n", Ftell (Stream)); fclose (stream);

Perform offset = 5offset =0offset=10

feof function

#include <stdio.h>

Int (FILE *stream);

Description: A file stream pointer.

Function: Determines whether the current read and write location of the file stream pointer has reached the end of the file.

Return value: Reaches the end of the file return not 0, otherwise 0.

fgetc function

#include <stdio.h>

int fgetc (FILE *stream);

Description: A file stream pointer.

Function: Reads a character from the specified file stream.

Return value: The read character that returns EOF when the end of the file is reached.

#include <stdio.h>

Main () {

FILE*FP;

int C;

Fp=fopen ("exist", "R");

while ((C=FGETC (FP))!=eof)

printf ("%c", c);

Fclose (FP);

}

FPUTC function

#include <stdio.h>

int FPUTC (int c, FILE *stream);

Description: C is the character to be written, and the stream is the specified file stream pointer.

Function: Writes a single character to the specified file stream. Return value: Run successfully returned parameter C, failed to return EOF

fgets function

#include <stdio.h>

Char *fgets (char *s, int size, FILE *stream);

Description: S is the first address that is stored after the string is read, and the size is the data volume that holds the string (size-1).

Function: Reads a string from the specified file stream. Return value: Run successfully returned S, failed to return null.

fputs function

#include <stdio.h>

int fputs (char *s, FILE *stream);

Description: S is the character to write, and stream is the specified file stream pointer.

Function: Writes a string to the specified file stream.

Return value: Run successfully returns the number of characters written to the file, and the failure returns EOF.

fread function

#include <stdio.h>

size_t fread (void *ptr, size_t size, size_t nmenb, FILE *stream);

Description: PTR is the read data stored in memory address, NMENB is the number of data units to read.

Function: Reads a piece of data from the specified file stream. Return value: The number of data units actually read.

fwrite function

#include <stdio.h>

size_t fwrite (void *ptr, size_t size, size_t nmenb, FILE *stream);

Description: PTR is the read data stored in memory address, NMENB is the number of data units to read.

Function: Writes the specified piece of data to the specified file stream. Return value: The number of data units actually read.

Open function

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int open (const char *pathname, int flags, mode_t mode);

O_rdonly Open the file in read-only form,

O_wronly Open the file as a write-only form,

O_rdwr open files in a readable and writable format;

O_creat If the open file does not exist, the file is automatically created,

O_excl with O_creat, if the file exists, error;

O_trunc the contents of the file when it is open in writable mode.

Description: Pathname is the path to open the file, flags is the flag bit, mode is used to indicate the file's operation permissions.

Function: Opens the specified file, returning the file identifier. Return value: Successful file identifier error-1

creat function

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

int creat (const char *pathname, mode_t mode);

Description: Equivalent to calling open ().

Function: Create a new file.

Return value: Run successfully returns the file identifier, failure returns 0.

mktemp function

#include <stdlib.h>

int mktemp (char *template);

Description: The file name of the temporary file, the last six characters must be xxxxxx.

Function: Creates a temporary memory file.

Return value: Run successfully returns the file identifier, failure returns 0.

Close File

#include <unistd.h>

int close (int fd);

Read function

#include <unistd.h>

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

Description: FD is a file identifier, BUF is an in-memory data buffer, and count is the size of the data to be read.

Function: Reads data from the specified file.

Return value: The length of the read data that was successfully returned by the run (how many bytes), failed return-1.

Write function

#include <unistd.h>

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

Description: FD is a file identifier, BUF is an in-memory data buffer, and count is the size of the data to be read.

Function: Writes data to the specified file.

Return value: The run successfully returned data read data, failed to return-1.

Lseek function

#include <sys/types.h>

#include <unistd.h>

off_t lseek (int fd, off_t offset, int whence);

Description: The FD is the file identifier, offset is the shift.

The whence is the base position for the move.

Function: Modify the read/write location of the file.

Return value: The absolute amount of the file's actual read-write location after the run successfully returns a read-write pointer, and the failure returns-1.

Fcntl function

#include <sys/types.h>

#include <unistd.h>

#include <fcntl.h>

int fcntl (int fd, int cmd, struct flock *lock);

Description: FD is a file identifier, CMD is a command, and lock records the specific state of the lock.

Function: Handles file sharing issues. Return Value: 0 Success-1 error

Select function

#include <sys/types.h>

#include <unistd.h>

#include <sys/time.h>

int select (int Numfds, fd_set *readfds, Fd_set *writefds, Fd_set *errorfds, struct timeval *timeout)

Description: Numfds need to check the number of the highest file description multibyte 1,

Readfds A collection of read file descriptors monitored by SELECT,

Writefds A collection of write file descriptors monitored by SELECT,

Errorfds a collection of exception-handling file descriptors that are monitored by SELECT.

Timeout:null is always waiting, knowing that the signal is caught or the descriptor is ready, specifically, if the wait bit timeout time has not yet been ready to file descriptor, immediately return, 0 never wait, test all the specified descriptor and immediately return. Function: Handles I/O multiplexing issues. Return value: Successfully prepared file descriptor-1 Error

Pipe function #include <unistd.h>

int pipe (int filedes[2]);

Description: The file identifier corresponding to both ends of the pipe, fi

LDES[0] is the identifier for the read end, and Fildes[1] is the identifier for the write end.

Function: Create a nameless pipe. Return value: Run successfully returned 0, failure returned-1.

Mkfifo function

#include <sys/types.h>

#include <sys/stat.h>

int Mkfifo (const char *pathname, mode_t mode);

Description: Pathname is the file name of the pipeline and mode is the permission for the pipeline.

Function: Create a well-known pipeline, which is considered a file creation.

Return value: Run successfully returned 0, failure returned-1.

Linux file Operation programming

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.