Copy of Linux Standard IO

Source: Internet
Author: User

---restore content starts---

Copy of 1.linux standard IO

#include <stdio.h>

int main (int argc,char **argv)

{

if (argc<3)

{

printf ("Use:mycp file1 file2\n");

return-1;

}

File *src=fopen (argv[1], "R");// Open source file

if (src==null)

{

printf ("No file!\n");

return-1;

}

FILE *dest=fopen (Argv[2], "w");

if (dest==null)

{

printf ("No file!\n");

Fclose (SRC);

return-1;

}

int Ret=fseek (src,0,seek_end);

Long Len=ftell (SRC);

Rewind (SRC);

Char Buffer[len];

Ret=fread (BUFFER,LEN,1,SRC);

Ret=fwrite (buffer,len,1,dest);

Fclose (SRC);

Fclose (dest);

}

2. Open the file f3.txtinread-write mode with standard IOandCreate if the file does not exist. If the file already exists, the length is truncated to 0.

#include <stdio.h>

#include <string.h>

#include <errno.h>

int main (int argc,char argv[])

{

FILE *FP;

if ((Fp=fopen ("F3.txt", "w+") ==null)// Open the file's path and path name, open the file's status

{

fprintf (stderr, "fopen () failed:%s\n", Strerror (errno));

return-1;

}

Fclose (FP);

return 0;

}

1: Standard IO Library operations are carried out around the stream, and when we open a file by fopen the standard io library function, we make a file and a IO stream is associated. Here we equate the io stream with the file pointer file* , because all operations for io streams are file* pointer to the implementation.

2: the standard io Library introduces an io cache, which reduces system calls by accumulating a certain amount of io data and then centrally writing to the actual file Thus improving IO efficiency. The standard IO Library automatically manages internal caches without the need for programmer intervention. However, it is because we do not see the cache of the standard IO libraries:

3: This object is typically a structure that contains all the information that the standard IO Library needs to manage the flow, including: file descriptors for actual io , Points to the pointer used for the stream buffer, the length of the buffer, the number of characters currently in the buffer, and the error flag, and so on.

4: file buffering

Objective: To minimize the number of calls using read/write

Definition: The system automatically creates a buffer in memory for each file being used, and the output from memory to disk must first be sent to the memory buffer and filled with buffers to be sent to disk. Reads data from disk, reads a batch of data from the disk file into the memory buffer one at a time, and then sends the data to the program's data area individually from the buffer.

Classification: Full cache, row cache, no cache.

Full buffer: The actual io operation is not performed until the standard IO buffer is filled . For files residing on disk, they are usually fully buffered by the standard IO Library. When performing the first IO operation on a stream , the relevant standard IO functions typically call malloc to get the buffers that need to be used. Flush, which describes The write operation of the standard IO buffer. The buffer can be flushed automatically by the standard IO process, or the function fflush can be called to flush a stream.

; row cache: In this case, when a newline character is encountered in input and output, the standard io library performs IO operations when it is refreshed once . This allows us to output one character at a time (with the standard io fputs function), but the actual IO operation is performed only after thanking a single line . Row buffering is typically used when a stream involves a terminal, such as standard input and output.

; do not cache: do not buffer characters.

---restore content ends---

Copy of Linux Standard 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.