Copying file lines into an array [ newbie ]

Discussion in 'C Programming' started by jj, May 24, 2004.

  1. jj

    jj Guest

    Hi,

    I would like to read a file 1 line at a time and storing the lines
    into a char array. (using fgets).
    I thought something like the code below would work but I am having
    problems.

    Any help would be appreciated.

    Thanks.

    jj

    -- code start --

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>

    #define LINESIZE 100
    #define MAXLINE 100

    int
    main(int argc, char **argv)
    {
    /* i would like to store the file there */
    char *lineptr[MAXLINE];

    FILE *fp;
    fp = fopen("testfile", "r");

    if ( fp == NULL ) {
    printf("Unable to openfile");
    exit(1);
    }

    char line[LINESIZE];
    int i;
    for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
    printf("%s",line);
    /* here i would like to do something */
    /* like */
    /* strcpy(lineptr, line); */
    /* or */
    /* lineptr = line; */
    }

    /* to be able to use it like this */
    for(i=0; i<MAXLINE;i++)
    printf("%s",lineptr);

    return 0;
    }
    -- code end --
     
    jj, May 24, 2004
    #1
    1. Advertising

  2. jj wrote:

    > Hi,
    >
    > I would like to read a file 1 line at a time and storing the lines
    > into a char array. (using fgets).
    > I thought something like the code below would work but I am having
    > problems.
    >
    > Any help would be appreciated.
    >
    > Thanks.
    >
    > jj
    >
    > -- code start --
    >
    > #include <stdio.h>
    > #include <string.h>
    > #include <errno.h>

    #include <stdlib.h> /* for EXIT_FAILURE and EXIT_SUCCESS */

    >
    > #define LINESIZE 100
    > #define MAXLINE 100
    >
    > int
    > main(int argc, char **argv)
    > {
    > /* i would like to store the file there */
    > char *lineptr[MAXLINE];


    You are creating an array of pointers here.
    Not necessarily a huge block of memory to store the
    content of the strings in.

    >
    > FILE *fp;

    unsigned int lines_in_memory = 0;

    > fp = fopen("testfile", "r");
    >
    > if ( fp == NULL ) {
    > printf("Unable to openfile");
    > exit(1);

    return EXIT_FAILURE;

    > }
    >
    > char line[LINESIZE];
    > int i;


    These definitions need to be moved up.
    Remember that "line" is a temporary.

    > for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
    > printf("%s",line);
    > /* here i would like to do something */
    > /* like */
    > /* strcpy(lineptr, line); */
    > /* or */
    > /* lineptr = line; */


    You need to allocate space from dynamic memory for the
    new string, then copy the data from the temporary
    variable, "line" into the new string:
    lineptr = malloc(strlen(line) + 1);
    if (!lineptr)
    {
    /* Handle memory allocation error here. */
    }
    strcpy(lineptr, line);

    > }

    lines_in_memory = i;

    >
    > /* to be able to use it like this */
    > for(i=0; i<MAXLINE;i++)
    > printf("%s",lineptr);

    for (i = 0; i < lines_in_memory; ++i)
    {
    printf("%5d: %s\n", i, lineptr);
    }

    >
    > return 0;

    /* A proper program cleans up after itself. */
    for (i = 0; i < lines_in_memory; ++i)
    {
    free(lineptr); /* every malloc needs a free. */
    }
    return EXIT_SUCCESS;
    > }
    > -- code end --



    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, May 24, 2004
    #2
    1. Advertising

  3. jj

    Al Bowers Guest

    jj wrote:

    > Hi,
    >
    > I would like to read a file 1 line at a time and storing the lines
    > into a char array. (using fgets).
    > I thought something like the code below would work but I am having
    > problems.
    >
    > Any help would be appreciated.
    >
    > Thanks.
    >


    For a simple, quick fix:

    >
    > -- code start --
    >
    > #include <stdio.h>
    > #include <string.h>

    Remove string.h. You will not need it.
    > #include <errno.h>

    Remove errno.h. Your code does not require it.
    Add header stdlib.h for function exit.
    >
    > #define LINESIZE 100
    > #define MAXLINE 100
    >
    > int
    > main(int argc, char **argv)

    int main(void) would do since you are not using
    any command line arguments in the code shown.
    > {
    > /* i would like to store the file there */
    > char *lineptr[MAXLINE];


    Make this:
    char lineptr[MAXLINE][MAXSIZE];
    >
    > FILE *fp;
    > fp = fopen("testfile", "r");
    >
    > if ( fp == NULL ) {
    > printf("Unable to openfile");
    > exit(1);

    exit(EXIT_FAILURE);
    > }
    >
    > char line[LINESIZE];

    You will not need and the array line. You can copy, using fgets,
    directly into the lineptr array. So you also be able to remove
    strcpy from the code.
    > int i;

    Add a variable to keep track of the number of lines copied.
    int numlines;
    > for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
    > printf("%s",line);
    > /* here i would like to do something */
    > /* like */
    > /* strcpy(lineptr, line); */
    > /* or */
    > /* lineptr = line; */
    > }
    >

    Replace the above for expression with one that copies
    directly into lineptr. ie.
    for(numlines = 0; numlines < MAXLINE &&
    (fgets(lineptr[numlines],LINESIZE,fp)); numlines++) ;

    Then close the file.

    > /* to be able to use it like this */
    > for(i=0; i<MAXLINE;i++)
    > printf("%s",lineptr);


    for(i = 0; i < numlines; i++)
    puts(lineptr);
    >
    > return 0;
    > }
    > -- code end --


    Corrected, the code becomes.

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

    #define LINESIZE 100
    #define MAXLINE 100

    int main(void)
    {
    char lineptr[MAXLINE][LINESIZE];
    int i, numlines;
    FILE *fp;

    fp = fopen("test.txt", "r");
    if ( fp == NULL )
    {
    printf("Unable to openfile");
    exit(1);
    }
    for (numlines = 0; numlines < MAXLINE &&
    (fgets(lineptr[numlines], LINESIZE, fp)); numlines++) ;
    fclose(fp);
    for(i=0; i<numlines;i++)
    puts(lineptr);
    return 0;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, May 24, 2004
    #3
  4. jj

    Luis Diego Guest

    > char *lineptr[MAXLINE];

    With this declaration you have a pointer to an array and each field of the
    array is an "char *"

    The problem is that each field in lineptr array must be allocated (because
    they are pointers).
    So you need to change your code:

    > char *lineptr[MAXLINE];


    > ...
    > ...
    > ...


    > for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
    > printf("%s",line);
    > /* here i would like to do something */
    > /* like */
    > /* strcpy(lineptr, line); */
    > /* or */
    > /* lineptr = line; */
    > }



    char lineptr[MAXLINE][LINESIZE];

    for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++)
    {
    printf("%s",line);
    strcpy(lineptr, line);

    /*The following commente line can't be used, would be a big mistake*/
    /* lineptr = line; */
    }

    I don't promise it would works AS IS.

    I strongly recommend "The C Programming Language" book from BW Kernighan and
    DM Ritchie, great book to start with C.


    Ing. Luis Diego Bolanos Quiros
    NPY & PE Dept.

    Inet 8-256-6536
    Office(506) 298-8939
    Cell (506) 817-6566


    "jj" <> wrote in message
    news:...
    > Hi,
    >
    > I would like to read a file 1 line at a time and storing the lines
    > into a char array. (using fgets).
    > I thought something like the code below would work but I am having
    > problems.
    >
    > Any help would be appreciated.
    >
    > Thanks.
    >
    > jj
    >
    > -- code start --
    >
    > #include <stdio.h>
    > #include <string.h>
    > #include <errno.h>
    >
    > #define LINESIZE 100
    > #define MAXLINE 100
    >
    > int
    > main(int argc, char **argv)
    > {
    > /* i would like to store the file there */
    > char *lineptr[MAXLINE];
    >
    > FILE *fp;
    > fp = fopen("testfile", "r");
    >
    > if ( fp == NULL ) {
    > printf("Unable to openfile");
    > exit(1);
    > }
    >
    > char line[LINESIZE];
    > int i;
    > for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
    > printf("%s",line);
    > /* here i would like to do something */
    > /* like */
    > /* strcpy(lineptr, line); */
    > /* or */
    > /* lineptr = line; */
    > }
    >
    > /* to be able to use it like this */
    > for(i=0; i<MAXLINE;i++)
    > printf("%s",lineptr);
    >
    > return 0;
    > }
    > -- code end --
     
    Luis Diego, May 24, 2004
    #4
  5. jj

    Luis Diego Guest

    Upps, I almost forget it.

    It is a very ineficent solution, there is a better way to do this.
    First
    Like using malloc function and eliminate the matrix of char's.

    Again, read the bible of C

    "Luis Diego" <> wrote in message
    news:c8taos$ch3$...
    > > char *lineptr[MAXLINE];

    >
    > With this declaration you have a pointer to an array and each field of the
    > array is an "char *"
    >
    > The problem is that each field in lineptr array must be allocated (because
    > they are pointers).
    > So you need to change your code:
    >
    > > char *lineptr[MAXLINE];

    >
    > > ...
    > > ...
    > > ...

    >
    > > for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
    > > printf("%s",line);
    > > /* here i would like to do something */
    > > /* like */
    > > /* strcpy(lineptr, line); */
    > > /* or */
    > > /* lineptr = line; */
    > > }

    >
    >
    > char lineptr[MAXLINE][LINESIZE];
    >
    > for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++)
    > {
    > printf("%s",line);
    > strcpy(lineptr, line);
    >
    > /*The following commente line can't be used, would be a big mistake*/
    > /* lineptr = line; */
    > }
    >
    > I don't promise it would works AS IS.
    >
    > I strongly recommend "The C Programming Language" book from BW Kernighan

    and
    > DM Ritchie, great book to start with C.
    >
    >
    > Ing. Luis Diego Bolanos Quiros
    > NPY & PE Dept.
    >
    > Inet 8-256-6536
    > Office(506) 298-8939
    > Cell (506) 817-6566
    >
    >
    > "jj" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I would like to read a file 1 line at a time and storing the lines
    > > into a char array. (using fgets).
    > > I thought something like the code below would work but I am having
    > > problems.
    > >
    > > Any help would be appreciated.
    > >
    > > Thanks.
    > >
    > > jj
    > >
    > > -- code start --
    > >
    > > #include <stdio.h>
    > > #include <string.h>
    > > #include <errno.h>
    > >
    > > #define LINESIZE 100
    > > #define MAXLINE 100
    > >
    > > int
    > > main(int argc, char **argv)
    > > {
    > > /* i would like to store the file there */
    > > char *lineptr[MAXLINE];
    > >
    > > FILE *fp;
    > > fp = fopen("testfile", "r");
    > >
    > > if ( fp == NULL ) {
    > > printf("Unable to openfile");
    > > exit(1);
    > > }
    > >
    > > char line[LINESIZE];
    > > int i;
    > > for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
    > > printf("%s",line);
    > > /* here i would like to do something */
    > > /* like */
    > > /* strcpy(lineptr, line); */
    > > /* or */
    > > /* lineptr = line; */
    > > }
    > >
    > > /* to be able to use it like this */
    > > for(i=0; i<MAXLINE;i++)
    > > printf("%s",lineptr);
    > >
    > > return 0;
    > > }
    > > -- code end --

    >
    >
     
    Luis Diego, May 24, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Row
    Replies:
    0
    Views:
    515
  2. Joe Wright
    Replies:
    0
    Views:
    554
    Joe Wright
    Jul 27, 2003
  3. inyc163

    Copying file lines into an array [ newbie ]

    inyc163, May 25, 2004, in forum: C Programming
    Replies:
    2
    Views:
    450
    Darrell Grainger
    May 25, 2004
  4. Shuch
    Replies:
    15
    Views:
    564
    Shuch
    Apr 2, 2006
  5. Jared Wiltshire
    Replies:
    8
    Views:
    432
    Ian Collins
    Aug 7, 2006
Loading...

Share This Page