Call library functions in linux to read and write files

Source: Internet
Author: User
1 Linux system call. the FILE descriptor uses an integer. the library function access FILE uses a FILE pointer to point to the description FILE. 2. the library function does not change with the system platform, that is

1. for Linux system calls, the FILE descriptor uses an integer, and the library function access FILE uses a FILE pointer to point to the description FILE;

2. database functions do not change with the system platform, that is, both Windows and Linux are applicable;

Library functions-read files

Size_t fread (void * ptr, size_t size, size_t n, FILE * stream)

Function:Read n fields from the file pointed to by stream, each field is size byte, and put the read data into the character array pointed to by ptr, return the actual number of bytes read. (The read data volume is size * n)

Library functions-write files

Size_t fwrite (const void * ptr, size_t size, size_t n, FILE * stream)

Function:Write n fields from the array pointed to by the buffer ptr to the file pointed to by stream. each field is in size and returns the number of actually written fields.

Library functions-create and open

FILE * fopen (const char * filename, const char * mode)

Filename:Open file name (including path, default: current path)

Mode:Open Mode

Instance code

Root @ wl-MS-7673:/home/wl/desktop/c ++ # cat-n file_lib_copy.cpp 1 2 # include
 
  
3 # include
  
   
4 # include
   
    
5 # define BUFFER_SIZE 1024 6 7/* 8 * program entry 9 **/10int main (int argc, char ** argv) 11 {12 FILE * from_fd; 13 FILE * to_fd; 14 long file_len = 0; 15 char buffer [BUFFER_SIZE]; 16 char * ptr; 17 18/* judge input parameter */19if (argc! = 3) 20 {21 printf ("Usage: % s fromfile tofile \ n", argv [0]); 22 exit (1 ); 23} 24 25/* open the source file */26if (from_fd = fopen (argv [1], "rb") = NULL) 27 {28 printf ("Open % s Error \ n", argv [1]); 29 exit (1 ); 30} 31 32/* create the target file */33if (to_fd = fopen (argv [2], "wb") = NULL) 34 {35 printf ("Open % s Error \ n", argv [2]); 36 exit (1 ); 37} 38 39/* measured file size */40 fseek (from_fd, 0L, SEEK_END); 41file_len = ftell (from_fd); 42 fseek (from_fd, 0L, SEEK_SET); 43 printf ("form file size is = % d \ n", file_len); 44 45/* copy the file */46 while (! Feof (from_fd) 47 {48 fread (buffer, BUFFER_SIZE, 1, from_fd); 49if (BUFFER_SIZE> = file_len) 50 {51 fwrite (buffer, file_len, 1, to_fd ); 52} 53 else 54 {55 fwrite (buffer, BUFFER_SIZE, 1, to_fd); 56file_len = file_len-BUFFER_SIZE; 57} 58 bzero (buffer, BUFFER_SIZE); 59} 60 fclose (from_fd ); 61 fclose (to_fd); 62 exit (0); 63} 64 65root @ wl-MS-7673:/home/wl/desktop/c ++ # g ++ file_lib_copy.cpp-o file_lib_copyfile_lib_copy.cpp: in the 'Int main (int, char **) 'function: file_lib_copy.cpp: 43: 41: Warning: format' % d' expects argument of type 'int ', but argument 2 has type 'long Int' [-Wformat] root @ wl-MS-7673:/home/wl/desktop/c ++ #. /file_lib_copy file_lib_copy.cpp test2.cform file size is = 1030root @ wl-MS-7673:/home/wl/desktop/c ++ #
   
  
 

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.