Thread: print a text-file in linked list

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    11

    print a text-file in linked list

    I need to get a C program to read a text-file (with aprox. 4 lines) to a linked list. and print it out.

    A simple method of doing this:
    Code:
    char c[10];
      FILE *file;
      file = fopen(argv[1], "r"); 
      if(file==NULL) {
        printf("Can not open file.\n");
        return 1;
      }
    while(fgets(c, 10, file)!=NULL) { 
          	printf("%s", c);
          }
          printf("\n");
    Although this example do not use struct or lists, which is what I need. It should look something like this:
    First, create the struct before main().
    Code:
    struct sang {
    struct sang *neste;
    char linje[200];
    int ant;
    };
    Then allocate memory in main():
    Code:
    struct sang *liste = malloc(sizeof(struct sang));
    Then open the text-file from argv[1] and put into the struct.
    Code:
     
    char c[10];
      FILE *file;
      file = fopen(argv[1], "r"); 
      if(file==NULL) {
        printf("Can not open file.\n");
        return 1;
    printf("testing %s",strcpy(liste.linje[200],argv[1]));
    The last line is my own invention. Which doesn't work. If anyone can point me in the right direction I would really appreciate it!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're using strcpy wrong. More importantly, you're passing arrays to functions incorrectly. When you pass an array, like when you want to use strcpy, just pass the name of the array. Also, assuming 'liste' is actually a pointer, and not just a structure instance, you're accessing its members incorrectly. To access structure members via pointer, you need the "arrow" operator.
    Code:
    strcpy( liste->linje, argv[1] );
    Of course that's not getting anything from your file. It's just copying your file name (assuming that's what is in argv[1]) into your list.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Ok! Thanks for the help. I've been working a couple of hours more now and figured some things out. A simplified code piece:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 2000
    #define LENGTH 200
    
    struct song {
      struct song *next;
      char *line;
    };
    
    int main(int argc, char *argv[])
           struct song *list = NULL, *last = NULL, *pdp;
    	pdp = malloc(sizeof(struct song));
           int n_lines;
           char *ant;
    
    /* Check if file can be opened*/
      	char c[10];
      	FILE *file;
      	file = fopen(argv[2], "r"); 
      	  if(file==NULL) {
        	    printf("Error: Can't open file.\n");
        	    exit(1);
      	  }   
    	
    /* Copy the text into list */
    	while (1) {
    	  ant = malloc(LENGTH );
    	    if (fgets(ant,LENGTH ,file) == NULL) {
    	      free(ant); break;
    	    }
    	  pdp->next = NULL; pdp->line = malloc(*ant);
    	  strcpy(pdp->line, ant);
    	    if (list == NULL) {
    	       list = last = pdp;
    	    } else {
    	      last->next = pdp; 
    	      last = pdp;
    	    }
    	}
    /* Print the list */
    	pdp = liste;
    	  while (pdp) {
    	    printf("%s\n", pdp->line);
    	    pdp = pdp->next;
    	  }
    I can figure out why the output prints the last line in the text-file infinite times (only stopped by CTRL+C). I think it has something to do with "fgets" but I maybe wrong.
    Last edited by tidemann; 09-19-2006 at 03:26 AM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Where are you declaring ant_line?

    And pdp->line = malloc(*ant); is wrong. You want malloc(strlen(ant->line) + 1) instead.
    Last edited by itsme86; 09-18-2006 at 08:39 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Ops! ant_line should be ant (edited now).. just a typo. If I use malloc(strlen(ant->line) + 1) i get the following error:

    "request for member 'line' in something not a structure or union"

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Well is ant a pointer to a structure or union?
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    It should be a pointer to a struct(ure). I'm a newbie when it comes to creating single-lists in structures.

    First I thought that...
    Code:
      if (fgets(ant,LENGTH ,file) == NULL) {
    	      free(ant); break;
    	    }
    ... disturbed the input with actually writing a line into the list which shouldn't be there. It maybe another problem though...

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by tidemann
    It should be a pointer to a struct(ure).
    If that's what it should be, why did you declare it as pointer-to-char?
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Since ant is working with the concurrent object in the list I thought that it should be a char* since it will contain text . The fgets-example I posted above won't work if its declared as anything else. Nor would strcpy. Should it be declared as something else?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    I solved the problem. I just had to re-write most of the copying... If anybodys interested I'll post the finished code at demand

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One of you malloc calls needs to go - you don't need both.

    Option 1 - read into array, then allocate space and copy
    char buff[BUFSIZ]; fgets( buff, sizeof buff, file );
    pdp->line = malloc(strlen(buff)+1);
    strcpy(pdp->line, ant);

    Option 2 - allocate space, read in a line then save the pointer
    ant = malloc(LENGTH );
    fgets(ant,LENGTH,file)
    pdp->line = ant;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM