c - Linked Files instead of linked list -
i have linked list time start , time end tuples. in each list, tuples not overlapping. now, instead of put tuples in list, want them stored in file. ever list should file. on order sort files , go through every file, have know file next file. in oder words, how can make pointer next file, such in linked list.
typedef struct list { struct list *next; } list;
but don't want this, create linked list first, , put each list in file, because want evade memory usage. here
static void create_files(list* first){ list* tmp = first; int partition_num = 1; while(tmp != null){ char filename[29]; sprintf(filename, "partitions%d/partition%d.txt",partition_folder, partition_num); file* partition; partition= fopen(filename, "w"); while(tmp->head != nil){ fprintf(partition,"[%d, %d) \n",ts,te); tmp->head= tmp->head->next; } list_num++; tmp=tmp->next; }
more this
file* firstfile; { //adding data file} file* second_file; { // adding data file} firstfile ->next = second file;
so want linked file. suggestions?
define this
struct filelinkedlist { file * f; filenode * next; }
but need have meta-data in files, example first line in file file-name of next file or 0 end
a.txt
b.txt first node in filelinkedlist
b.txt
0 last node in filelinkedlist
list
{ [a.txt] }--> { [b.txt] }--> 0
then define functions generate list you
filelinkedlist* createfilelinkedlist (char* file_name) { char next_file[255]; filelinkedlist* n,head = (filelinkedlist*)malloc(sizeof(filelinkedlist)); head->f = fopen(file_name); next_file = get_meta(head->f); init_reader(head->f); n = head; while (strcmp("0",next_file) != 0) { n->next = (filelinkedlist*)malloc(sizeof(filelinkedlist)); n = n->next; n->f = fopen(next_file); next_file = get_meta(head->f); init_reader(head->f); } return head; }
where get_meta
read first line need
char* get_meta (file* f) { char* m = (char*)malloc(sizeof(char)*255); rewind(f); fgets(m, sizeof(m), f); return m; }
and init_reader
put file cursor after meta-data part
void init_reader (file* f) { rewind(f); while(fgetc(f) != '\n'); }
Comments
Post a Comment