| | |
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
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






