| | |
File problem in link list
![]() |
hello.. its me again and Im having problems with files again..this time, its with link list..
I can't get my program to display the contents it reads from a file.
Please help..
I want to solve this myself but I have no time, my exam will start in 1 hour..
I can't get my program to display the contents it reads from a file.
Please help..
I want to solve this myself but I have no time, my exam will start in 1 hour..

c Syntax (Toggle Plain Text)
void save_file(LIST L) { FILE *fp; char filename[30]; LIST p; printf("What is the name of the file? "); flushall(); gets(filename); if((fp = fopen(filename, "wb")) == NULL) { printf("Cannot write file"); } for(p = L; p != NULL; p=p->next) fwrite(&p, sizeof(celltype), 1, fp); printf("File Saved"); fclose(fp); getch(); clrscr(); } void load_file(LIST *A) { FILE *fp; char filename[30]; size_t c; LIST p, temp; printf("What is the filename? "); flushall(); gets(filename); if((fp = fopen(filename, "rb")) ==NULL) { printf("File does not exist"); } for(p = *A; p != NULL; p = p->next) { fread(&p, sizeof(celltype), 1, fp); } printf("File Loaded"); getch(); clrscr(); }
Last edited by Whilliam; Jul 2nd, 2009 at 8:29 pm.
I hope your implementation of list is something like :
In the save_file function:
You need to specify the address of the character array which holds the value.Say your structure is as
And after all the proper memory assignments for the node and the character array you should use it as:
And the statements
And if you have implemented the list as something like:
Then the statements
typedef struct node * LIST; In the save_file function:
•
•
•
•
for(p = L; p != NULL; p=p->next)
fwrite(&p, sizeof(celltype), 1, fp);
c Syntax (Toggle Plain Text)
struct node{ char *store_this; };
c Syntax (Toggle Plain Text)
for(p = L; p != NULL; p=p->next) fwrite(&(p->store_this), sizeof(celltype), 1, fp);
And the statements
LIST *A and p=*A; are absolute nonsence because if p is of type node* then p=A is enough and if p is of type node then p=*A becomes meaningless.And if you have implemented the list as something like:
c Syntax (Toggle Plain Text)
typedef struct node LIST;
p=p->link; loose value. Its kind of an confusion with your program. Last edited by csurfer; Jul 2nd, 2009 at 8:44 pm.
I Surf in "C"....
sob.. its still not working..
here's my main function and structure:
here's my main function and structure:
c Syntax (Toggle Plain Text)
typedef struct { char LN[16]; char FN[24]; char MI; }name; typedef struct { unsigned long ID; name n; char course[8]; int year; }studrec; typedef struct cell { studrec s; struct cell *next; }celltype, *LIST void main(void) { LIST L, A; char ans; int quit = 0; studrec n; L = NULL; A = NULL; clrscr(); do { printf("Press\nN for Input\nI for Insert\nS to save file\nL to load a file\nV to view\nQ to quit\n"); ans = getch(); clrscr(); if(ans == 'N' || ans == 'n') { input(&n); clrscr(); } else if(ans == 'I' || ans == 'i') insert(&L, n); else if(ans == 'S' || ans == 's') save_file(L); else if(ans == 'L' || ans == 'l') { load_file(&A); view(A); } else if(ans == 'V' || ans == 'v') { view(L); getch(); clrscr(); } else if(ans == 'Q' || ans == 'q') quit = 1; }while(quit == 0); }
How many records in a data file?
You have to test the eof().
Can fread() allocate a node?
?
C Syntax (Toggle Plain Text)
for(p = *A; p != NULL; p = p->next) { fread(&p, sizeof(celltype), 1, fp); }
Can fread() allocate a node?
?
Last edited by adatapost; Jul 3rd, 2009 at 1:12 am.
Failure is not fatal, but failure to change might be. - John Wooden
Hey you want us to do all the work ???
Try on your own man...Put in some effort...Go on reading each line like a compiler does and ya if you are using Boreland C then may be F7 will help you trace program flow line by line.So try to debug it.
Some of the errors which you can easily find :
1>No semicolon after the struct cell.
2>You have declared A and L as
@adatapost :
Let him correct these primitive mistakes first adatapost memory allocation and other things are off limits if he cannot trace these simple mistakes.Basically he is not putting in the effort required!!!
Try on your own man...Put in some effort...Go on reading each line like a compiler does and ya if you are using Boreland C then may be F7 will help you trace program flow line by line.So try to debug it.Some of the errors which you can easily find :
1>No semicolon after the struct cell.
2>You have declared A and L as
LIST A,L; and LIST is itself struct node * type how can you pass pointer to this pointer...? As in p = *A; @adatapost :
Let him correct these primitive mistakes first adatapost memory allocation and other things are off limits if he cannot trace these simple mistakes.Basically he is not putting in the effort required!!!
Last edited by csurfer; Jul 3rd, 2009 at 1:30 am.
I Surf in "C"....
![]() |
Similar Threads
- a problem with linked list (C++)
- Not repeated elements in link list (help) (C++)
- Help... Link list problem (C++)
- link list with copy and delete elements (C++)
- insert file data to linked list? (C++)
- Link List with an embedded stack C++ (C++)
- Binary Tree with Link List (C++)
- Link List Sorting Problem (C++)
- Using a class to add/delete/show numbers in a Link List (C++)
Other Threads in the C Forum
- Previous Thread: file problem
- Next Thread: Using isdigit
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard strchr string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h






