The system interface function is used to read and write files in Linux platform.

Source: Internet
Author: User

Requirements: Support large file (1M) One-time read-in

The source code is as follows:

#include <stdio.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #define Rwmode 2//    Clear-screen command function void Clear () {char clscode[] = {0x1B, 0x5b, 0x48, 0x1B, 0x5b, 0x4A}; printf ("%s", Clscode);}   Open file int openFile () {int fd;   Fd=open ("/root/wcy/test1/test.txt", Rwmode); if (fd==-1) {printf ("Open file failed, file path is incorrect, or the file does not exist!")       \ n ");   Exit (0); }else return FD;   }//Read file void readFile (int fd,char buffer[],int len) {int n=read (fd,buffer,len);   buffer[n]= ' + ';   printf ("The contents of the file is:");   printf ("%s", buffer); printf ("\ n");} void lseekfile (int fd) {if (Lseek (fd,0l,seek_end) ==-1) {clear ();p rintf ("failed to locate read-write file!   "); } else{char block[512]= "Lseek file"; Write (block);p rintf ("Locate and read files successfully!   \ n ");  }}int Main () {int fd,n,select;  Char buffer[1024*1024];  Clear ();   while (1) {printf ("******************************\n");  printf ("****** read-write file system ******\n");  printf ("* * * 1 shows specific file contents: ****\n");  printf ("* * * 2 Locating and Reading file contents: ****\n");  printf ("* * * * 0 Exit this system *****\n"); Printf ("******************************\n");  printf ("Please enter the function number:");  scanf ("%d", &select);   Switch (SELECT) {case 0:clear (); close (FD); exit (0);   Case 1:clear (); Fd=openfile (); ReadFile (fd,buffer,sizeof (buffer)-1); close (FD);        Case 2:clear (); Fd=openfile (); Lseekfile (FD); close (FD);  Break Default:clear ();p rintf ("you entered the function number is wrong, please re-enter!")  \ n "); }} exit (0);}

Note: When running this program, be sure to have the file "/root/wcy/test1/test.txt", if there is no such file in this directory, please modify the directory where your files are located.

The system interface function is used to read and write files in Linux platform.

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.