| | |
file problem
Thread Solved |
Hello.. im having a problem with my code.. when i save a file, then choose another variable and load the file, then try to display it, all that it display are garbage values.. i know the problem is either in function save_file or function load_file.. can anyone please help me..
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> #include<conio.h> #define max 20 typedef struct { char LN[16]; char FN[24]; char MI; }name; typedef struct { unsigned long ID; name n; char course[8]; int year; }studrec[max]; void save_file(studrec s, int n) { FILE *fp; char filename[30]; int ctr; printf("What is the name of the file? "); flushall(); gets(filename); if((fp = fopen(filename, "wb")) == 0) { printf("Cannot write file"); } for(ctr = 0; ctr < n; ctr++) fwrite(&(s[ctr]), sizeof(studrec), 1, fp); printf("File Saved"); fclose(fp); getch(); clrscr(); } void load_file(studrec r) { FILE *fp; int ctr; char filename[30]; printf("What is the filename? "); flushall(); gets(filename); if((fp = fopen(filename, "rb")) == 0) { printf("File does not exist"); } for(ctr = 0; fp == 0; ctr++) fread(&(r[ctr]), sizeof(studrec), 1, fp); printf("File Loaded"); getch(); clrscr(); }
Last edited by Whilliam; Jul 2nd, 2009 at 11:44 am.
> for(ctr = 0; fp == 0; ctr++)
What does this do?
> flushall();
> gets(filename);
flushall() is non-standard, and gets() is the tool of the devil.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
> #include<conio.h>
Why are you still using this obsolete header?
Is your compiler also obsolete?
What does this do?
> flushall();
> gets(filename);
flushall() is non-standard, and gets() is the tool of the devil.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
> #include<conio.h>
Why are you still using this obsolete header?
Is your compiler also obsolete?
I sense a problem here in your load file function :
You can correct it as
Above code will take one extra loop to find the end so better
There are far better ways,this is just the least modification of your code.
And sorry salem this is happening second time with me today when I begin writin there would be no post and when I post it there would be a reply already...and we cant delete any post made
c Syntax (Toggle Plain Text)
for(ctr = 0; fp == 0; ctr++) fread(&(r[ctr]), sizeof(studrec), 1, fp);
You can correct it as
c Syntax (Toggle Plain Text)
size_t c; for(ctr = 0; c != 0; ctr++) c=fread(&(r[ctr]), sizeof(studrec), 1, fp);
c Syntax (Toggle Plain Text)
size_t c; for(ctr = 0;; ctr++) { c = fread(&(r[ctr]), sizeof(studrec), 1, fp); if(!c) break; }

And sorry salem this is happening second time with me today when I begin writin there would be no post and when I post it there would be a reply already...and we cant delete any post made
Last edited by csurfer; Jul 2nd, 2009 at 12:39 pm.
I Surf in "C"....
most of these trifling problems will go away when you throw away the P.O.S. Borland/Turbo compiler and get a real C/C++ compiler like CodeBlocks or Bloodshed Dev C++.
these GCC-based, standard C compilers wont let you use worthless functions like flushall(), obsolete libraries like conio.h, and will complain heartily if you try and hang yourself by using gets()
.
these GCC-based, standard C compilers wont let you use worthless functions like flushall(), obsolete libraries like conio.h, and will complain heartily if you try and hang yourself by using gets()
.
Last edited by jephthah; Jul 2nd, 2009 at 2:05 pm.
@Salem:
Im using borland c
fp == 0 in for loop of function load_file is supposed to find the end of file.. I don't know how to do it so I tried that.
Please help me find a better way..
@csurfer
I tried the modified code and it work the first time but when I close the program and run it again and load the file that I saved in the first program, it displays garbage values.
Im using borland c
fp == 0 in for loop of function load_file is supposed to find the end of file.. I don't know how to do it so I tried that.
Please help me find a better way..

@csurfer
I tried the modified code and it work the first time but when I close the program and run it again and load the file that I saved in the first program, it displays garbage values.
Last edited by Whilliam; Jul 2nd, 2009 at 7:11 pm.
Post in your code or else attach the file.
I told you earlier itself that was just to show you the implementation.Thats it you need to research the things and work out for yourself. We here at Daniweb just show you the way we don't give you every details
I told you earlier itself that was just to show you the implementation.Thats it you need to research the things and work out for yourself. We here at Daniweb just show you the way we don't give you every details
Last edited by csurfer; Jul 2nd, 2009 at 7:12 pm.
I Surf in "C"....
ok here it is..
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> #include<conio.h> #define max 20 typedef struct { char LN[16]; char FN[24]; char MI; }name; typedef struct { unsigned long ID; name n; char course[8]; int year; }studrec[max]; void input(studrec s, int n); void view(studrec s, int n); void save_file(studrec s, int n); void load_file(studrec r); void input(studrec s, int n) { int ctr; for(ctr = 0; ctr < n; ctr++) { printf("Input student %d's Last Name [enter] First Name [enter] Middle Initial [enter]: \n", ctr + 1); flushall(); gets(s[ctr].n.LN); flushall(); gets(s[ctr].n.FN); scanf("%c", &s[ctr].n.MI); printf("What is student %d's ID number? ", ctr + 1); scanf("%d", &s[ctr].ID); printf("What is student %d's course? ", ctr + 1); flushall(); gets(s[ctr].course); printf("What is student %d's year? ", ctr + 1); scanf("%d", &s[ctr].year); printf("\n"); } } void save_file(studrec s, int n) { FILE *fp; char filename[30]; int ctr; printf("What is the name of the file? "); flushall(); gets(filename); if((fp = fopen(filename, "wb")) == 0) { printf("Cannot write file"); } for(ctr = 0; ctr < n; ctr++) fwrite(&(s[ctr]), sizeof(studrec), 1, fp); printf("File Saved"); fclose(fp); getch(); clrscr(); } void load_file(studrec r) { FILE *fp; int ctr; char filename[30]; size_t c; printf("What is the filename? "); flushall(); gets(filename); if((fp = fopen(filename, "rb")) == 0) { printf("File does not exist"); } for(ctr = 0; c != 0; ctr++) c=fread(&(r[ctr]), sizeof(studrec), 1, fp); printf("File Loaded"); getch(); clrscr(); } void view(studrec s, int n) { int ctr; for(ctr = 0; ctr < n; ctr++) { printf("%s, %s %c.\n", s[ctr].n.LN, s[ctr].n.FN, s[ctr].n.MI); printf("%d\n", s[ctr].ID); puts(s[ctr].course); printf("%d\n", s[ctr].year); printf("\n"); } } void main(void) { studrec s, r; char ans; int quit = 0, n; clrscr(); do { printf("Press\nI for Input\nS to save file\nL to load a file\nV to view\nQ to quit\n"); ans = getch(); clrscr(); if(ans == 'I' || ans == 'i') { printf("How many records? "); scanf("%d", &n); clrscr(); input(s, n); clrscr(); } else if(ans == 'S' || ans == 's') save_file(s, n); else if(ans == 'L' || ans == 'l') { load_file(r); view(r, n); } else if(ans == 'V' || ans == 'v') { view(s, n); getch(); clrscr(); } else if(ans == 'Q' || ans == 'q') quit = 1; }while(quit == 0); }
Last edited by Whilliam; Jul 2nd, 2009 at 7:15 pm.
Some major problems :
1>Get a standard compiler for yourself. They are all free of cost.Boreland C and Tc and all are not going to take you anywhere.
2>The load function works fine better use the second method I had suggested for "for loop" rather than the one implemented.Because the present condition loops one time extra.But still works.Some bug may creep in so the suggestion.
3>Problem is with your view function.You are calling it as
It should be mainly implemented this is the function looping infinitely.With arbitrary value for n it will loop any random number of times.So infinite loop.
1>Get a standard compiler for yourself. They are all free of cost.Boreland C and Tc and all are not going to take you anywhere.
2>The load function works fine better use the second method I had suggested for "for loop" rather than the one implemented.Because the present condition loops one time extra.But still works.Some bug may creep in so the suggestion.
3>Problem is with your view function.You are calling it as
view(s,n); .You cannot just pass any value for n you should keep a counter for total number of records in the file and that should be passed as n.Or atleast if you think that is users work to remember the number of records you atleast need to take the input for n before calling view as: c Syntax (Toggle Plain Text)
printf("Input the number records you want to view / input the number of records in file that you remember :"); scanf("%d",&n); view(r,n)
Last edited by csurfer; Jul 2nd, 2009 at 7:45 pm.
I Surf in "C"....
•
•
•
•
Some major problems :
1>Get a standard compiler for yourself. They are all free of cost.Boreland C and Tc and all are not going to take you anywhere.
2>The load function works fine better use the second method I had suggested for "for loop" rather than the one implemented.Because the present condition loops one time extra.But still works.Some bug may creep in so the suggestion.
3>Problem is with your view function.You are calling it asview(s,n);.You cannot just pass any value for n you should keep a counter for total number of records in the file and that should be passed as n.Or atleast if you think that is users work to remember the number of records you atleast need to take the input for n before calling view as:
It should be mainly implemented this is the function looping infinitely.With arbitrary value for n it will loop any random number of times.So infinite loop.c Syntax (Toggle Plain Text)
printf("Input the number records you want to view / input the number of records in file that you remember :"); scanf("%d",&n); view(r,n)

I need to use tc and borland c for now because its what we are using in school.
•
•
•
•
thanks man.. it worked!
I need to use tc and borland c for now because its what we are using in school.
I Surf in "C"....
![]() |
Similar Threads
- Editing a file problem (Python)
- File I/O Problem: Incomplete or Missing Content During Writeline (Python)
- Function & writing to file problem (Python)
- TSP read input file problem (C++)
- C++ seeking data to file problem (C++)
- New member with a system file problem... (Windows NT / 2000 / XP)
- file problem (C)
- Basic File I/O problem (C++)
- problem in reading a file (Shell Scripting)
Other Threads in the C Forum
- Previous Thread: Adding large numbers
- Next Thread: File problem in link list
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






