简体   繁体   English

如何从c中的文件读取一行?

[英]How do I read a line from a file in c?

I am having a requirement to read a file line by line through ac program. 我需要通过ac程序逐行读取文件。 I know I can do it very straight by using a FILE pointer and using fgets (if by fixed length string) or fgetc (if by character by character). 我知道我可以通过使用FILE指针和使用fgets (如果按固定长度的字符串)或fgetc (如果按字符一个字符)来非常直接地做到这一点。 The only point here to note is that the length of a line in the file can be different. 这里要注意的唯一一点是文件中一行的长度可以不同。 So , if i am using 所以,如果我正在使用

char *fgets(char *__restrict s, int n, FILE *__restrict stream); 

I do not know the value of "int n" here since it can vary from line to line in the file. 我在这里不知道“ int n”的值,因为它在文件中的每一行都可能不同。

I searched for an answer for this query but i got to know the usage of getline function which is c++ function. 我搜索了此查询的答案,但我知道了getline函数(即c ++函数)的用法。 The only way for me is to read each character until i encounter a '\\n' and get that copied to a string. 对我而言,唯一的方法是读取每个字符,直到遇到“ \\ n”并将其复制到字符串中。

Is there any other way to do this straight in ac program ? 在交流程序中还有其他方法可以直接做到这一点吗?

Check this -> explanation (possible duplicate question) The getline function is documented -> here 检查此-> 说明 (可能重复的问题)记录了getline函数-> 此处

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t myread;

   fp = fopen("/etc/motd", "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);

   while ((myread = getline(&line, &len, fp)) != -1) {
        printf("Retrieved line of length %zu :\n", myread);
        printf("%s", line);
    }

   free(line);
    exit(EXIT_SUCCESS);
}

I do not know the value of "int n" here since it can vary from line to line in the file. 我在这里不知道“ int n”的值,因为它在文件中的每一行都可能不同。

The above and other discussion implies OP wants code that auto-sizes the buffer to whatever the line length may be. 上面的讨论和其他讨论都暗示OP希望代码能够自动将缓冲区的大小调整为任意行长。 fgets() does the job fine if you are willing to accept a max length. 如果您愿意接受最大长度,则fgets()可以很好地完成工作。 Read the line and then copy in to a right-sized buffer. 阅读该行,然后复制到合适大小的缓冲区中。

  #define BUF_SIZE 1000
  char buf[BUF_SIZE];

  while (fgets(buf, sizeof buf, stream)) {

    // Add long line detection here if desired

    char *s = strdup(buf);
    foo(s);
  }

If code does not want to impose any maximum length, IMO, that is a weak design. 如果代码不想强加任何最大长度,IMO,这是一个较弱的设计。 Certainly lines can be very long and BUF_SIZE above should be a very generous value, but to allow code to handle lines of any length is to give control of the program to external forces be it a file or user input. 当然,行可以很长, BUF_SIZE上面的BUF_SIZE应该是一个非常大的值,但是允许代码处理任意长度的行是将程序的控制权交给外力,无论是文件还是用户输入。 That is the basis of a hacker exploit. 这是黑客利用的基础。 The given application should have a liberal upper bound of the acceptable line length. 给定的应用程序应具有可接受的行长的自由上限。 fgets() provides that - although it does have other weaknesses. fgets()提供了-尽管它确实还有其他弱点。 The common *nix getline() allows external forces to cause your program to consume up to SIZE_MAX byte in one call - not defensive code. 通用的* nix getline()允许外力使您的程序在一次调用中消耗多达SIZE_MAX个字节,而不是防御性代码。

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

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