简体   繁体   English

从 C 编程中的文本文件打印特定行

[英]Printing a specific line from a text file in C programming

I was wondering if there was any way for me to print a specific line from a text file in C?我想知道是否有任何方法可以从 C 的文本文件中打印特定行? Do I need to create an array?我需要创建一个数组吗?

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
  
int main() {
    FILE *fin = fopen("1.txt", "r");
    FILE *fout = stdout; // for testing
  
    char line[1000];
    bool print_line = false;
  
    while (fgets(line, sizeof line, fin) != NULL) {
        if (print_line) {
            fputs(line, fout);
            print_line = false;
        }
        if (strstr(line, "PROLIST") != NULL)
            print_line = true;
    }
  
    fclose(fin);
    return 0;
}

To print a specific line from a text file in C, you can use an array but you do not have to.要从 C 中的文本文件打印特定行,可以使用数组,但不是必须的。

The easiest method involves reading one line at a time while counting them and output the required line when the count is right.最简单的方法是在计数时一次读取一行,在计数正确时读取所需的行。 To read a line, you would define an array of char and use fgets() .要读取一行,您将定义一个char数组并使用fgets()

There are some potential problems if some of the lines are very long as arrays must be defined or allocated with a specific length and lines from the file can always be longer than this arbitrary length.如果某些行非常长,则存在一些潜在问题,因为 arrays 必须定义或分配特定长度,并且文件中的行总是可以长于该任意长度。 This problem can be addressed by testing if the line read by fgets() does end with a newline.这个问题可以通过测试fgets()读取的行是否以换行符结尾来解决。

You could use getline() , available on many systems, that reallocates the array to accommodate for the length of the line it reads from the file.您可以使用许多系统上可用的getline() ,它重新分配数组以适应它从文件中读取的行的长度。 It remains possible to run out of memory if the line is longer than available memory, which would be the case for /dev/zero .如果该行比可用的 memory 长,那么 memory 仍然可能用完, /dev/zero就是这种情况。

An alternative approach where no array is needed is this: read one byte at a time, counting newlines and output the byte if the line count is right.不需要数组的另一种方法是:一次读取一个字节,计算换行符和 output 如果行数正确,则读取字节。

Here is a simple method using an array of char and fgets() :这是一个使用charfgets()数组的简单方法:

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

#define LINE 12   // output line number 12

int main() {
    long line = 1;
    char line[256];  // arbitrary length

    while (line < LINE && fgets(line, sizeof line, stdin)) {
        if (line == LINE)
            fputs(line, stdout);
        // only increase the line count if line is complete
        if (strchr(line, '\n'))
            line++;
    }
    return 0;
}

Here is a simpler alternative without an array:这是一个没有数组的更简单的替代方案:

#include <stdio.h>

#define LINE 12   // output line number 12

int main() {
    long line = 1;
    int c;

    while (line < LINE && (c = getchar()) != EOF) {
        if (line == LINE)
            putchar(c);
        if (c == '\n')
            line++;
    }
    return 0;
}

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

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