Thursday , overcast

Today's content is mainly about documents IO

man A section of the manual :

man -f open Look at those volumes that have open
man 1 -- Ordinary command programs
man 2 -- system call
man 3 -- Library function

file : The operating system abstracts the hardware into files
Input : Write data from the device into memory
Output : Write the data in memory to the device

Linux The file system consists of two layers : The first layer is the virtual file system (VFS), The second layer is a variety of specific file systems

posix: Portable operating system interface specification
API: User programming interface

Application passed POSIX and GNU C LIB become system call ( Access to the system ): Call this interface library function

Application direct access system : system call

To access the system, the upper layer needs to call the system first ,

Error handling :

strerror(): mapping errno The corresponding error message :strerror(errno) ,error Global variable
prerror(); Output user information and errno The corresponding error message

printf("file to open:%s\n", strerror(error))
perror("file to open:")
The functions of the above two sentences are similar to that of :file to open: no such file directory ( The following prompt statement is related to the function of the program )

Linux The documents in this paper are mainly divided into 6 Kind of : Ordinary documents , Directory file , Symbolic link file , Pipeline files , Socket files and device files .

Without cache IO: file IO
With cache IO: standard IO
Write out :Output
Read in :Input( Relative to the program )

You need to include the following header files
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

standard IO: Encapsulates file descriptors and caching mechanisms

fseek and ftell == lseek;

file IO Function of :

When the total number of bytes is greater than the number of bytes per read / write , There are two simple ways to judge whether reading and writing is complete ;
1. Add up :( Set the total number of bytes =MAX)

while((nbyte = write(fd, buf, N)) > 0)
sum += nbyte;
if(sum == MAX)

2. Judge whether the actual number of bytes read is equal to the set number of bytes read

i = read(fd, buf, N);
write(fd, buf, i);
if(i != N)

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

offset: Relative to the base point whence The offset , In bytes , A positive number means moving forward , A negative number means moving backward

Get the length of the file :
length = lseek(fd, 0, SEEK_END)// Note a few macros (SEEK_SET , SEEK_CUR , SEEK_END)

standard IO;

Find the length of the file :fseek() and ftell() The program function of the combination of two functions is equivalent to lseek();
FILE *fp;
if(fseek(fp, 0, SEEK_END) < 0)
perror("fseek error");
retrn 1;
length = ftell(fp);

Seek the file length complete procedure :

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>

int main(int atgc, char *argv[])
FILE *fp;
long length;

if(argc < 2)
fprintf(stdout,"usage: %s filename\n",argv[0]);
return 1;

if((fp = fopen(argv[1],"r")) == NULL)
fprintf(stderr,"fopen error: %s",strerror(erron));
return 1;

if(fseek(fp, 0, SEEK_END) < 0)
perror("fseek error");
return 1;

lingth = ftell(fp);

printf("the fiel size is %ld\n",length);

return 0;


file IO And standards IO More articles about

  1. file IO And standards IO The difference between 【 turn 】

    One . Let's first understand what a document is I/O And standards I/O: file I/O: file I/O It's called without cache IO(unbuffered I/O). No cache means every read,write Call a system call in the kernel . That is to say, generally speaking ...

  2. 8 . IO class - standard IO、 file IO、stringIO

    8.1 IO class #include <iostream> // standard IO The header file   8.2 File input and output streams #include <fstream> // Read and write header file std::fst ...

  3. linux file io With the standard io

    file IO the truth is that API,Linux The main process of file operation is : open (open), operation (write.read.lseek), close (close). 1. Open file function open(): The header file involved :  #includ ...

  4. system call IO And standards IO

    Catalog 1. system call IO( No buffer IO) system call Common system calls IO function open close read write lseek ioctl 2. standard IO( With buffer IO) summary Buffer and rinse Common criteria IO ...

  5. standard IO And documents IO The difference between 【 turn 】

    Reprinted from :http://blog.sina.com.cn/s/blog_63f31f3401013jrn.html Let's first understand what standards are IO And documents IO. standard IO: standard I/O yes ANSI C The establishment of a ...

  6. standard IO And documents IO The difference between

    Let's first understand what standards are IO And documents IO. standard IO: standard I/O yes ANSI C A standard established I/O Model , It's a standard function package and stdio.h Definition in header file , It has certain portability . standard IO The library deals with a lot of details . For example, caching ...

  7. Concept of documents 、 standard IO firstly

    1. Concept of documents A file is a file stored on disk ( Power down does not lose the storage device ) On , A way to store data without losing power , Files in the system have the following hierarchical structure to achieve . system call . file IO. standard IO The relationship is as follows : 2.linux The files of the system are divided into ...

  8. [APUE] standard IO library ( On )

    One . Flow and FILE object System IO It's all about file descriptors , When opening a file , That is to return a file descriptor , Then use the file descriptor to do the following , And for standards IO library , They operate around the flow (stream) On going . When you open a ...

  9. linux standard IO buffer (apue)

    Why standards are needed IO buffer ? LINUX The buffer is everywhere , Whether it's hardware . Kernel or application , There's a page cache in the kernel , Memory cache , Hardware, not to mention L1,L2 cache, There are countless applications , Basically write good software ...

Random recommendation

  1. maven Fast image

    <mirrors> <mirror> <id>CN</id> <name>UK Central</name> <url&g ...

  2. standard io And documents io

    A: Duplicate code : Sentence block 1: while( Judge ) { Sentence block 2: Sentence block 1: } It can be rewritten as : while(1) { Sentence block 1: if( Judge ) break: Sentence block 2: } B: standard IO And documents I ...

  3. Docker Instruction set

     FROM            grammar :FROM <image>[:<tag>]         explain : Set which image to make the image based on ,FROM The command has to be the whole Dockerfile ...

  4. A complex disease IIS URL Case sensitive (FF Automatic transformation of URL problem )?

    (II8 VS14 CTP3 Windows7 ASP.NET WEBFORM) In my impression ,IIS Of URL In general, it's case insensitive . So the next two URL It should be the same . http://loca ...

  5. java analysis xml file (dom)

    DOM analysis XML file Read local xml file , adopt DOM To analyze ,DOM The characteristic of analysis is to put the whole xml The file is loaded into memory , Form a DOM A tree structure , Tree structure is easy to traverse and manipulate . DOM The feature of parsing is to read xml file ...

  6. BZOJ 3694 shortest path

    233333 Think simple .... See Synonyms at explanation http://hzwer.com/3710.html #include<iostream> #include<cstdio> #inclu ...

  7. Weinre Remote debugging mobile page

    Weinre Is a remote debugging tool , Use JS To write , It allows us to directly debug the remote page running on the mobile phone on the computer , When your code has been released online , There's a problem ,Weinre Can help you debug . Debugging scenarios Debug page on the phone . ...

  8. How to use all functions promise How to achieve ?

    How to use all functions promise How to achieve ? I think it's a good question . Currently in my company , Just use NodeJS Development , From framework to specific application examples to tools , Have all migrated to promise Develop the way for the center . It brings ...

  9. np.cumsum() The meaning of functions and regular expressions

  10. Bitcoin Use and configuration records

    Common configuration bitcoin-qt.exe -testnet -printtoconsole -conf=D:\Bitcoin\bitcoin.conf -datadir=D:\Bitcoin\Data ...