long LogOpen( char *filename, __int64 *filelen ) { long fileHandle; fileHandle = LogOpenQuiet( filename, filelen ); if( !fileHandle ) ErrorOpeningFile( filename ); return fileHandle; }
void Flush2File (struct nodo ** list) { /* flushes all the items in the linked list into a file */ struct nodo * act = NULL; FILE * fp; int choice; if (! *list) return; act = *list; printf("\n\n1 - New File"); printf("\n2 - Append Data"); CHOICE (choice); if (choice == 1) { /* we delete all the contents of the file and open it to write */ if ((fp = fopen("dlist.bin", "wb")) == NULL) { ErrorOpeningFile(); return; } } else { /* we open the file to append data */ if ((fp = fopen("dlist.bin", "ab")) == NULL) { ErrorOpeningFile(); return; } } /* while we have a node pointing to a differnt address than NULL... */ while (act) { /* now we write into the file each node of the linked list */ fwrite (act, sizeof (struct nodo), 1, fp); act = act->next; } fclose(fp); printf("\n\nData was flushed into \'dlist.bin\'. Press any key to continue..."); getch(); }
void ReadFile (struct nodo ** list) { /* creates a new linked list from data in 'dlist.bin' deleting all repeated entries, filtering by nodo->nclient */ struct nodo * aux = NULL; FILE * fp; int sizenodo = sizeof(struct nodo); /* first we make sure to free all the memory used by the list since we are going to create a new one */ FreeAll (list); *list = NULL; if ((fp = fopen("dlist.bin", "rb")) == NULL) { ErrorOpeningFile(); return; } /* we read the first record on the file and we add it to the beginning of the list */ aux = (struct nodo *) malloc (sizenodo); fread(aux, sizenodo, 1, fp); AddBegin (list, aux); /* we do the loop while we are not in the end of file */ while (!feof(fp)) { aux = (struct nodo *) malloc (sizeof(struct nodo)); if(fread(aux, sizenodo, 1, fp)) { /* if nclient does not exist in our list then we add the node to the list */ if (!NumberExists(list, aux->nclient)) AddBetween(list, aux); else /* since I did not add the item to the list I free the memory used by it */ free (aux); } } free (aux); printf("Data was read from \'dlist.bin\' and Double Linked List was created.\nPress any key to continue..."); getch(); }