Hello,
        Let's see if I can do this right... The following code should work
(standard disclaimers apply)

#include <stdio.h>

int main()
{
    FILE *fil;
    char line[50];
    char **all_lines
    int c;

    if((fil=fopen("file.1","r")) == NULL)
    {
       printf("Couldn't open \"file.1\".\n");
       exit(1);
    }

    all_lines = malloc(10*sizeof(char **));

    for(i=0; i<10; i++) {
        fgets(line, sizeof(line), fil);
        all_lines[i] = strdup(line);
        printf("%s" line);
    }
}

/* end code */

change the 10 to however many lines are in your file. You can also use
realloc to make the array bigger dynamically.

Joseph

--
[EMAIL PROTECTED]         **************
http://users.exis.net/    * Powered by *
                          * GNU Linux  *
  "Think different"       **************

On Tue, 4 Aug 1998, Torbjørn S. Kristoffersen wrote:

::How can I make a C program that reads line by line from a file?
::I want to pass each line into a string.
::
::Here's my code so far:
::
::---------------------------
::#include <stdio.h>
::
::int main()
::{
::    FILE *fil;
::    char line[50];
::    int c;
::
::    if((fil=fopen("file.1","r")) == NULL)
::    {
::       printf("Couldn't open \"file.1\".\n");
::       exit(1);
::    }
::
::    fgets(line, sizeof(line), fil);
::    printf("%s",line);
::}
::---------------------------
::
::
::Thanks,
::Torbjørn Kristoffersen <[EMAIL PROTECTED]>
::

Reply via email to