在 C 中逐行读取文件 [英] Reading a file line by line in C

查看:30
本文介绍了在 C 中逐行读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些代码来打开一个文件,逐行读取其内容并将这些行中的每一行存储到一个数组中.

I am trying to write some code that will open a file, read its content line by line and store each of these lines into an array.

首先我打开文件并计算行数,每一行的长度都是固定的,所以我只是这样做:

First I open the file and count the number of lines, each of the lines is of a fixed length so I simply do this :

    char buf2[LINE_LENGTH];
    int in2 = open("toSend2", O_RDONLY);
    int number_of_lines = 0;

    for (;;)
 {
  char* p2 = buf2;
  int count = read (in2, p2, LINE_LENGTH);
  if (count < 0)
  {
    printf("ERROR");
    break;
  }
  if (count == 0) break; 

  number_of_lines++;

  printf("count: %d \n",count);
  printf("File 2 line : %s", p2);
  printf("\n");

 }
 close (in2);

到目前为止,这很有效,number_of_lines 确实是文件toSend2"中的行数,而我的每个 printf 都是该文件中包含的行.

So far, this works well, number_of_lines is indeed the number of lines in the file "toSend2" and each of my printf are the lines contained in that file.

现在有了行数,我创建了一个字符串数组,然后我基本上再次遍历了整个文件,但这一次,我想将每一行存储在数组中(可能有更好的方法可以找到出文件中的行数,但我尝试的一切都失败了!)

Now with the number of lines, I create an array of strings and then I basically go through the whole file again but this time, I would like to store each of the lines in the array (there's probably a better way to find out the number of lines in a file, but everything that I tried has failed !)

    char * array[number_of_lines];
    int b=0;
    int in3=0;
    in3 = open("toSend2", O_RDONLY);
    for (;;)
 {
  char* p3 = buf3;
  int count = read (in2, p3, LINE_LENGTH);
  if (count < 0)
  {
    printf("ERRORRRRRR");
    break;
  }
  if (count == 0) break;


  array[b] = p3;
  b++;

  printf("count: %d \n",count);
  printf("FILE 2 LINEEEEEE : %s", p3);
  printf("\n");

 }  
 close(in3);

这当然行不通:我的每个 printf 都是正确的行加上文件的最后一行,例如,第一个 printf 将是:

This, of course, doesn't work : each of my printf are the right line plus the last line of the file, for example, the first printf would be :

FILE 2 LINEEEEEEE:文件的第一行"文件的最后一行"

FILE 2 LINEEEEEEE : "1st line of the file" "last line of the file"

在这个 for 循环之后,当我跟踪数组的内容时,其中的每一项都只是文件的最后一行.我认为这是因为我每次都在数组中简单地放置了相同的指针(当时指向不同的行),但最终它会指向最后一行,因此所有内容都将是最后一行.

And after this for loop, when I trace the contents of my array, every item in it is simply the last line of the file. I think this is because I simply put the same pointer (pointing to a different line at that moment) in the array each time, but in the end it will point to the last line, therefore everything will be the last line.

我将如何解决我的问题?

How would I solve my problem ?

ps:我刚开始使用 C,所以请不要假设我知道它的基本知识 :(

p.s.: I just started C, so please do not assume I know even basic things about it :(

推荐答案

  • 使用 stdio,即 fopen()fgets()fclose() 进行 I/O.您正在使用更低级别的 Posix 样式 I/O,没有充分的理由.
  • 您需要动态分配每个新行以将其存储在数组中.您可以使用 strdup() 来执行此操作.
  • 记住,事情可能会出错;文件可能无法打开,行可能无法读入,内存可能无法分配.检查这一点,并采取相应的行动.
    • Use stdio, i.e. fopen(), fgets() and fclose() to do the I/O. You're using much lower-level Posix-style I/O, for no good reason.
    • You will need to dynamically allocate each new line in order to store it in the array. You can use strdup() to do this.
    • Remember that things can go wrong; files can fail to open, lines can fail to read in, and memory can fail to be allocated. Check for this, and act accordingly.
    • 这篇关于在 C 中逐行读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆