views:

85

answers:

3

Hi

I'm trying to make a program that read a file line by line and then put the readed line into a a linked list, my problem is to add the string to list. Look at the code, in the else test you can see my problem.

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

struct list_el {
    char *ord;
       struct list_el * next;
};

typedef struct list_el item;

int main(int argc, char *argv[]) {
    int c;
    item *curr, *head;
    head = NULL;
    FILE *fileHandle = fopen("tresmaa.txt", "r");

    while((c = fgetc(fileHandle)) != '\n' || c != EOF)
        if(c == EOF) {
            printf("\n");
            break;
        } else {
            curr = (item*)malloc(sizeof(item));
            curr->ord = "I cant point curr -< ord = c, how can i point the readed sentences to the value Ord?";
            curr->next = head;
            head = curr;
            putchar(c);
        }
    curr = head;

   while(curr) {
      printf("%s\n", curr->ord);
      curr = curr->next ;
   }
}
A: 

I see your problem in the else. :)

Your malloc of the struct is not sufficient. This malloc only creates the memory of the struct memory (two pointers) not the memory inside. You'll have to malloc your char memory (ord) with the proper size of the string as well. Use strlen and add one for null to determine size of this string.

Starkey
Ye, because char *ord is a pointer that point on to a string, now I understand. Can you please show me how to malloc char ?
+1  A: 
curr->ord = "some string" is wrong

instead you need to allocate a buffer and place the string in it

e.g.

curr->ord = malloc( strlen(yourstring) + 1 );
strcpy(curr->ord, yourstring);

because

curr = (item*)malloc(sizeof(item));

only allocates the struct including the 'ord' pointer, but not what it points to.

another thing that looks a bit suspicious is

        curr->next = head;
        head = curr;

looks more like the name should have been 'prev' and not 'next' the way you do it (LIFO)

otherwise if you want a "normal" FIFO linked list just have a head ptr and an end ptr, then use the end ptr to append elements while keeping the head pointing to the first list element.

Anders K.
Thanks. But this strings is very confusing for a Java programmer. int c that contains the line that has been read, and can be used to printf("%S", c) print the line. But I cant replace yourstring with c in = malloc(strlen(yourstring) +1); Have not quit understand the point of cstring
int c contains only one character that you read, not a string. In C a string is a character array char[] + one position to hold the ending \0 character. So if you want to read a line from the file you would typically use a buffer: char buffer[maxlength] and then use fgets(buffer,maxlength-1,filehandle) to read a line from the file into the buffer, then you would do the curr->ord= malloc( maxlength ) and copy the buffer into allocated spot strcpy(curr->ord, buffer).
Anders K.
Ok. I want to change some letters in nodes cstring before i print the cstring with changed letters out, therefor I want with a for loop copy the cstring to another cstring that I will performe changes on. but I cant write char tmp[300] = curr->ord; in the for loop. Isent curr->ord a char array? and strcpy(new cstring, curr->ord) dont work. How can i copy curr->ord to a char [] ?
A: 

curr->ord = "some string" is right!!

liu
But not what we want :P