简体   繁体   English

C:从文件中读取最后 n 行并打印它们

[英]C: Reading last n lines from file and printing them

I'm trying to read last n lines from file and then print them.我正在尝试从文件中读取最后 n 行,然后打印它们。 To read the lines I'm using fgets() and it seems to work fine.要阅读我正在使用fgets()的行,它似乎工作正常。 However, when I try to print the last n lines that I have stored in the array, it only prints the last line n times.但是,当我尝试打印存储在数组中的最后 n 行时,它只打印最后一行 n 次。 It seems like there is something wrong with the way I store strings in an array.我在数组中存储字符串的方式似乎有问题。 Here's my code:这是我的代码:

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

int main(int agrc, char** agrv) {
    FILE* input;
    input = fopen(agrv[1], "r");

    int n = *agrv[2]-'0';
    int line = 0;
    char text[11];
    char** tab = malloc(1000000*sizeof(text));

    while(fgets(text, sizeof(text), input) != 0) {
        tab[line] = text;
        line++;
    }

    fclose(input);

    int jump = line-n;
    
    for(int i=jump; i<line; i++) {
        printf("%s\n", tab[i]);
    }
}

Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance!提前致谢!

EDIT:编辑:

I've changed my while loop to this.我已将我的 while 循环更改为此。 However, it still doesn't work.但是,它仍然不起作用。

    while(fgets(text, sizeof(text), input) != 0) {
        char text2[11];
        strcpy(text2, text);
        tab[line] = text2;
        line++;
    }

tab[line] = text; sets tab[line] to point to the start of text .tab[line]设置为指向text的开头。 So you end up with all the tab[i] pointing to the same place, the start of text .所以你最终所有的tab[i]都指向同一个地方,即text的开头。

You need to copy each line read from the file to a different place in memory.您需要将从文件中读取的每一行复制到 memory 中的不同位置。

It may increase your understanding if you see working code that does what you hope to achieve.如果您看到可以实现您希望实现的工作代码,它可能会增加您的理解。 Because your OP could only show 1-9 "last lines", this code doesn't try to go beyond that "meager" amount.因为您的 OP 只能显示 1-9“最后一行”,所以此代码不会尝试 go 超出“微薄”的数量。 Further, this code is meant for files whose lines "are of moderate length".此外,此代码适用于行“中等长度”的文件。 It should be clear where changes can be enacted.应该清楚在哪里可以进行更改。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> // for 'isdigit()'

// Factor processing into functions for clarity
void tail( FILE *ifp, size_t n ) {
    char text[ 9 ][ 126 + 1 + 1 ]; // 9 'rows' up to 128 bytes each
    size_t lnCnt = 0;

    while( fgets( text[ lnCnt % 9 ], sizeof text[0], ifp ) != NULL )
        lnCnt++;

    // Do the math to workout what is wanted and what's available
    if( lnCnt < n )
        n = lnCnt, lnCnt = 0;
    else
        lnCnt += 9 - n;

    // output
    while( n-- )
        printf( "%s", text[ lnCnt++ % 9 ] );

}

int main( int argc, char *argv[] ) {
    // Check parameters meet requirements
    if( argc != 3 || !isdigit( argv[ 2 ][ 0 ] ) ) {
        fprintf( stderr, "Usage: %s filename #lines (1-9)\n", argv[ 0 ] );
        exit( EXIT_FAILURE );
    }

    // Check functions didn't fail
    FILE *ifp = fopen( argv[ 1 ], "r" );
    if( ifp == NULL ) {
        fprintf( stderr, "Cannot open '%s'\n", argv[ 1 ] );
        exit( EXIT_FAILURE );
    }

    // do processing
    tail( ifp, argv[ 2 ][ 0 ] - '0' );

    // clean up
    fclose( ifp );

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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