简体   繁体   中英

Converting head program to tail C

I just got a semi function head program running and I need help making it into a tail program where it will display x number of lines at the end of the file. Here is my head code. I'm having trouble thinking of how to get the counter to read the entire file then start at the bottom and show the last lines of the file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1024


int numLines = 0;
int linecount = 0;
FILE *src = NULL;
char ch[MAX];


void GetArgs (int argc, char **argv){
if(argc != 4 || argc != 2) {
    printf("Error not enough arguments to continue \n", argv[0]);
    exit(-1);

}// end if argc doenst = 4 or 2

if(argc == 2){
    src = fopen( argv[1], "r:");
    numLines=10;

}// end argc = 2

if(argc == 4){
    if (strcmp (argv[1], "-n") !=0 ){
        numLines = atoi (argv[2]);
        src = fopen (argv[3], "r");
        if ( src == NULL){
            fputs ( "Can't open input file." , stdout);
            exit (-1);
        }
        while (NULL != fgets(ch, MAX, src)){
            linecount++;
            fputs(ch, stdout);
            if (linecount == numLines){
                break;
            }
        }

    }//end of nested if
     else if (strcmp (argv[2], "-n") !=0 ){
        numLines = atoi (argv[3]);
        src = fopen (argv[1], "r");
        if ( src == NULL){
            fputs ( "Can't open input file." , stdout);
            exit (-1);
        }
        while (NULL != fgets(ch,MAX, src)){
            linecount++;
            fputs(ch, stdout);
            if (linecount == numLines){
                break;
            }
        }            


    }//end of else
}//end if argc == 4

}// end GetArgs


int main(int argc, char **argv){



GetArgs(argc, argv);

   fclose( src );
}

You can keep a counter to pass through the whole file and count the total number of lines (Only call fgets to read one line at a time). Suppose N is the total number of lines and n is the number of last lines to be shown on the screen. Then start again at the beginning of the file pass through N - n lines without showing them on the screen (ie dont call fputs), after that call fputs for n number of times.

I used your variables and style. If I understand, you want to printout last [X] lines from file. [X] - parameter from CLI

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX 1024

    FILE *src = NULL;
    FILE *src_end = NULL;
    int linecount = 0;
    char ch[MAX];

    int main(int arc, char **argv) {
        int i = 0;
        int letter = 0;

        if (arc != 4) {
            puts("Wrong parameters");
            return 1;
        }

        do {
            src = fopen(argv[1], "r");
            if (src == NULL) {
                puts("Can't open input file.");
                break;
            }

            if (0 != strcmp(argv[2], "-n")) {
                puts("Wrong 2nd parametr");
                break;
            }

            sscanf(argv[3], "%u", &linecount);
            printf("Printout last %u lines, from file:%s\n", linecount,
                    argv[1]);

            src = fopen(argv[1], "r");
            if (src == NULL) {
                fputs("Can't open input file.", stdout);
                return 1;
            }

            for (i = 2; linecount; i++) {
                fseek(src, -i, SEEK_END);
                letter = fgetc(src);
                if (letter == 0x0a) { // 0x0a == "\n"
                    fgets(ch, MAX, src);
                    fputs(ch, stdout);
                    linecount--;
                }
            }
        } while (0);

        fclose(src);
        return 1;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM