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!