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.