Cmon Garfield, I thought you'd got the hang of this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SEG_LENGTH 21
typedef struct line_segment {
char buff[SEG_LENGTH];
struct line_segment *next;
} line_seg;
line_seg *append_line ( line_seg *head, char *buff ) {
// allocate (should check for NULL)
line_seg *temp = malloc( sizeof(line_seg) );
// copy data
strcpy( temp->buff, buff );
temp->next = NULL;
// append to linked list
if ( head == NULL ) {
head = temp;
} else {
line_seg *tail = head;
while ( tail->next != NULL ) {
tail = tail->next;
}
tail->next = temp;
}
return head;
}
void print_line ( line_seg *head ) {
while ( head != NULL ) {
printf( "%s\n", head->buff );
head = head->next;
}
}
int main ( ) {
line_seg *big_line = NULL;
char buff[SEG_LENGTH];
// read from stdin (or any FILE*)
while ( fgets(buff,SEG_LENGTH,stdin) != NULL ) {
big_line = append_line( big_line, buff );
if ( strchr( buff, '\n' ) != NULL ) break; // newline found
}
print_line( big_line );
// free big_line - easy exercise for the reader :)
return 0;
}