Standard I/O common functions and Linux folder action functions __oracle

Source: Internet
Author: User

Standard I/O (#include <stdio.h>)

1. file* fopen (const char *filename,const char *mode);

Parameters:

FileName: filename;

Mode: Pattern:

Text file

binary files

Significance

R

Rb

Read-only

W

Wb

Write only

A

Ab

Additional

r+

rb+

Change Mode Open

w+

wb+

Change the way to open and set the file length to zero

A +

ab+

Change the way to open, append after file

2. Reading and writing

size_t fread (void *ptr, size_t size, size_t nitem, file* stream);

size_t fwrite (void *ptr, size_t size, size_t nitem, file* stream);

Parameters:

PTR: string to store data

Size: Yes. Read length

Nitem: Number of operations

Stream: File pointer

3. Close the file

int Flose (file* stream);

4. Write data that is not written in the file stream to the file immediately

int fflush (FILE *stream);

5. Lseek in I/O

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

Parameters:

Stream: File pointer;

Offset: The location where the file pointer is to be set;

Whence: Defines the use of offset values to remove the face value:

Seek_set

Offset is an absolute article

Seek_cur

Offset is a relative position relative to the current

Seek_end

Offset is a relative position relative to the end of the file

6. single fetch, input character

int fgetc (FILE *steam);

int getc (FILE *steam);

int GetChar ();

int FPUTC (FILE *steam);

int PUTC (FILE *steam);

int Putchar ();

7. Get String function

Char *fgets (char *s, int n, file* stream);

Char *gets (char *s);

Returns a pointer to S.

8. formatted input and output

int fprintf (FILE *stream, const char *format ...);

int printf (const char *format ...);

Sprint Int (char *s, const char*format ...);

int scanf (const char *format ...);

int fscanf (FILE *stream, const char *format ...);

int sscanf (char *s, const char*format ...);

9. File Stream Error

#include <errno.h>

int ferror (FILE *stream);

int feof (FILE *stream);

void clearer (FILE *stream);

String Copy

#include <string.h>

void*memcpy (void *dest, const void *SRC, size_t n);

function returns the value of the dest. directory operations (#include <sys/types.h> #include <dirent.h>)

Folder structure Body:

struct dirent{

ino_t D_ino; D_ino This directory into the point of the Inode

ff_t D_off; D_off the beginning of the directory file to the entry point in this directory

signed short int d_reclen; The length of the D_reclen _name, which does not contain null characters

unsigned char d_type; D_type D_name refers to the file type D_name file name

Char d_name[256]; Filename

};

In this section, refer to the file: "folder operation. docx"

1. Open, read, and close the directory

Dir*opendir (const char *name);

Structdirent *readdir (DIR *dirp);

Intclosedir (DIR *dirp);

2. return to current position

Long int telldir (DIR *dirp);

3. Set folder pointer

Void Seekdir (DIR *dirp, long int loc);

4. examples

#include <sys/types.h>

#include <dirent.h>

#include <unistd.h>

Main ()

{

dir * DIR;

struct dirent * PTR;

int i;

dir = Opendir ("/etc/rc.d");

while (ptr = Readdir (dir))!= NULL)

{

printf ("D_name:%s\n", ptr->d_name);

}

Closedir (dir);

}

5. Get folder details

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

int stat (const char *path, Structstat *buf);

int fstat (int fd, struct stat*buf);

Intlstat (const char *path, struct stat *buf); When the path is a link, get the information for the link itself, not the file that the link points to.

Stat Structure Body:

struct STAT {

dev_t St_dev; /* ID of device containing file */

ino_t St_ino; /* Inode Number * *

mode_t St_mode; * Protection * *

nlink_t St_nlink; /* Number of hard links */

uid_t St_uid; /* User ID of owner/*

gid_t St_gid; /* Group ID of owner/*

dev_t St_rdev; /* Device ID (if special file) * *

off_t st_size; /* Total size, in bytes * *

blksize_t st_blksize; /*blocksize for file system I/O *

blkcnt_t st_blocks; /* Number of 512Bblocks allocated * *

time_t St_atime; /* Time of last access */

time_t St_mtime; /* Time of last modification*/

time_t St_ctime; /* Time of last status change * *

};

Attached: Judge file type:

Structstat Statbuf;

S_isreg (Statbuf.mode): Is it a regular file?

S_isdir (statbuf.mode): directory?

S_ISCHR (Statbuf.mode): Character device?

S_ISBLK (statbuf.mode): Block device?

S_isfifo (Statbuf.mode) FIFO: (nastatbuf.modeedpipe)?

S_islnk (statbuf.mode): Systatbuf.modebolic link? (Not inposix.1-1996.)

S_issock (Statbuf.mode): Socket? (not in posix.1-1996.)

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.