用C读取文件一行一行 [英] Reading a file line by line in C

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

问题描述

我试图写一些code,这将打开一个文件,逐行读取其内容线,这些线存储到一个数组。

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 ?

P.S:我刚开始C,所以请不要以为我知道它甚至基本的东西:(

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

推荐答案


  • 使用标准输入输出,即 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天全站免登陆