1.more Command function
More commands to view text that is more than one screen (similar to cat)
Basic Features :
1. Input BACKSPACE: Content Turn One screen
2. Enter ENTER: The contents of the flip line
3. Enter Q: Exit
4. Real-time display of read file proportions
2. Realization of Ideas
- Get the file you need to manipulate from the command
- Opening file: Open (filename,o_rdonly);
- Get total rows of files:
Lseek (fd,0,seek_end);//move the file pointer to the end of the file
Page_sum = Lseek (fd,0,seek_cur);//calculation and file header offset. You can get the total number of files
- Command initial read content (defines 10 rows for the entire screen)
Sets a flag to store the number of rows that need to be displayed each time, if n. Then run n operations
With read (fd,str,1); Reads one byte at a time, reads ' \ n ' stop
And you need to use Lseek (fd,0,seek_cur) to get the number of rows currently displayed after each reading. Used to get the file read ratio
- Accept user instructions after each operation
See_more ();
The function accepts user input and returns the number of pages to be flipped according to the input value
- Close (FD);
Process Harvesting
- Anti-Explicit text:
Do this only to realize that the function of printf on Linux is very rich, clear screen. Move the cursor, all readily.
- Ensure that more information is always displayed at the bottom of the terminal. does not show repeatedly
Just started to use, printf ("\033[s") records the cursor position. After the output, then use printf ("\033[u\033[k"), the cursor is returned and clear line;
However, in the output hint message. You also need to accept input from the user, which will result in a newline character, and after the newline, the cursor will be invalidated and recorded here.
Finally, use printf ("\033[1a\033[k") to move the cursor up one line and empty the line to overwrite the cue message
- Implementation of file proportions
This implementation details have been mentioned above, the reason why do not repeat, because the author in the implementation process because this function lag very long
You want to use Fgets () to read text because of the need to read on a line. However, the open file is also used by the system function open (), rather than fopen (), resulting in a mixed situation.
And the result is every time using Lseek () to read the current file pointer offset, the resulting value is the beginning of the file and the end of the file offset, changed to fseek (), the value becomes 0, explore no fruit, do not know its cause, recorded in this, and later talk about
Code
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#define PAGENUM //define a screen as 10 rows voidDo_more (intFD);intSee_more (intSumintNow);intMainintargcChar**ARGV) {intFdif(ARGC = =1)//The number of references is not valid to return{return 0; }Else{ while(argc-->1) {if(FD = open (*++argv,o_rdonly))! =-1) {Do_more (FD); Close (FD); }Else{printf("Error:can ' t open file\n");Exit(1); } } }return 0;}voidDo_more (intFD) {Charstr[3];intPage_num =0;//Save the number of rows you need to jump intRelintPage_now =0;//Save the current number of rows (initially 0) intPage_sum;//Save Total rowsLseek (FD,0, seek_end);//Move the file pointer to the end of the filePage_sum = Lseek (FD,0, seek_cur);//Calculate with file header offset, that is, the total number of file rowsLseek (FD,0, Seek_set);//Move file pointer to file header while(page_now<page_sum) { while(Read (FD,STR,1))//read one line at a time{if(str[0] ==' \ n ') {printf("\ n"); Break; }printf("%c", str[0]); } Page_now = Lseek (FD,0, seek_cur);//real-time update of read rows if(Page_num = = pagenum) {rel = See_more (page_sum, Page_now);//Get user input printf("\033[1a\033[k");//Move the cursor up one line and empty the line to overwrite the cue message if(rel = =0) { Break; }Else{Page_num-= rel; }} page_num++; }}intSee_more (intSumintNow) {intRelprintf("\033[7m--many others--enter backspace turn the page Q exit%.2f%% \033[0m", now*100.0/sum);//ECHO output hint information and file read ratio while(rel = GetChar ()) {if(rel = =' Q ') {return 0; }if(rel = ="') {returnPagenum; }if(rel = =' \ n ') {return 1; } }return 0;}
Linux file Management Summary of its own definition more