greeting!
Just found a sample linked list program and I've decided to play around with it (hoping that I can incorporate it with the project I'm currently working on).
here is the code: (this is only a quick hack/modification on the sample program that I've found, I haven't fix the warning message yet)
the question is, what would I have to do in order for the contents of a linked list to be place in one string (character array).Code:#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char name[1000]; } data; typedef struct { data d; struct NODE *next; } NODE; #define CFG_FILE "Read.txt" // name of file to be open int main(void) { int num = 0; FILE *cfgfile; char data[80]; int count = 1; //display purposes NODE* head = NULL; // this should always point to the 1st node NODE* pin = NULL; // used to iterate through nodes. cfgfile = fopen ( CFG_FILE, "r" ); // open file, simulate the data retrieve from the database if ( cfgfile == NULL ) { perror ( "Error in reading text file containing cfg file location!!!" ); } else { while ( fgets ( data, 80, cfgfile ) != NULL ) //delinmeter is newLine { printf("%d Line Read: %s",count++,data); // prints out the contents of Read.txt if( head == NULL ) { head = (NODE*) malloc(sizeof(NODE)); head->next = NULL; pin = head; } else { pin->next = (NODE*) malloc(sizeof(NODE)); pin = pin->next; pin->next = NULL; } strcpy(pin->d.name,data); } fclose ( cfgfile ); } //Printing the data. Goes through the linked list node by node. printf("\n\n"); pin = head; while( pin != NULL ) { printf("%s", &pin->d.name); num = num + strlen(pin->d.name); pin = pin->next; } printf("\ntotal size of string is %d\n",num); // cleanup while( head != NULL ) { pin = head->next; free( head ); head = pin; } return 0; }
to elaborate more.
let say the content of the Read.text (which can vary )are as follows:
after reading the file and inserting the values to the linked list, how would I arranged the contents of the linked list into one string,<DATA> 1
<DATA> 2
<DATA> 3
<DATA> 4
into this format
any kind of help or comment will be much appreciated.<DATA> 1<DATA> 2<DATA> 3<DATA> 4
regards,
jaro



LinkBack URL
About LinkBacks


