Reading a file line by line in C

前端 未结 4 846
长情又很酷
长情又很酷 2020-12-12 01:34

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

4条回答
  •  囚心锁ツ
    2020-12-12 02:17

    • 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.

提交回复
热议问题