| | |
File problem in link list
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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
Views: 340 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab win32 windows.h






