| | |
Help about my program. About files
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hello, I've been having problems with my program. I really don't get files that much. This is my code:
I run the program two times. First,
It will write the data that are in function insert to file "test.dat". The read_file function call doesn't exist yet.
The second time I run the program, the function call insert and write_file are deleted and function call read_file is created.
I've been getting garbage value and I can't figure out why. Please help.
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<conio.h> #define max 3 typedef struct { char FName[24]; char LName[16]; char MI; }name; typedef struct { unsigned long ID; name n; char course[8]; int level; }profile; typedef struct { profile *p; int last; }student_record, *srptr; void initialize(srptr *s); void insert(srptr *s); void display(srptr s); void write_file(srptr s); void read_file(srptr *s); void initialize(srptr *s) { *s = (srptr) malloc(sizeof(student_record)); (*s)->p = (profile *) malloc(sizeof(profile) * max); (*s)->last = -1; } void insert(srptr *s) { strcpy((*s)->p[++(*s)->last].n.FName, "Bradd"); strcpy((*s)->p[(*s)->last].n.LName, "Pitt"); (*s)->p[(*s)->last].n.MI = 'P'; (*s)->p[(*s)->last].ID = 7000000; strcpy((*s)->p[(*s)->last].course, "BSIT"); (*s)->p[(*s)->last].level = 3; strcpy((*s)->p[++(*s)->last].n.FName, "Angelina"); strcpy((*s)->p[(*s)->last].n.LName, "Jolie"); (*s)->p[(*s)->last].n.MI = 'M'; (*s)->p[(*s)->last].ID = 7111111; strcpy((*s)->p[(*s)->last].course, "BSICT"); (*s)->p[(*s)->last].level = 1; strcpy((*s)->p[++(*s)->last].n.FName, "Tom"); strcpy((*s)->p[(*s)->last].n.LName, "Cruise"); (*s)->p[(*s)->last].n.MI = 'A'; (*s)->p[(*s)->last].ID = 7222222; strcpy((*s)->p[(*s)->last].course, "ACTMT"); (*s)->p[(*s)->last].level = 2; } void display(srptr s) { int ctr; for(ctr = 0; ctr < max; ctr++) { printf("%d)\n", ctr + 1); printf("%s %c. %s\n", s->p[ctr].n.FName, s->p[ctr].n.MI, s->p[ctr].n.LName); printf("%li\n", s->p[ctr].ID); printf("%s %d\n", s->p[ctr].course, s->p[ctr].level); printf("\n"); } } void write_file(srptr s) { FILE *fp; int ctr; if((fp = fopen("test.dat", "wb")) == NULL) printf("Cannot open file\n"); for(ctr = 0; ctr <= s->last; ctr++) { fwrite(&s->p[ctr].ID, sizeof(long), 1, fp); fwrite(&s->p[ctr].n.FName, sizeof(char), strlen(s->p[ctr].n.FName), fp); fwrite(&s->p[ctr].n.LName, sizeof(char), strlen(s->p[ctr].n.LName), fp); fwrite(&s->p[ctr].n.MI, sizeof(char), 1, fp); fwrite(&s->p[ctr].course, sizeof(char), strlen(s->p[ctr].course), fp); fwrite(&s->p[ctr].level, sizeof(int), 1, fp); } printf("File Saved\n"); getch(); clrscr(); fclose(fp); } void read_file(srptr *s) { FILE *fp; int ctr; if((fp = fopen("test.dat", "rb")) == 0) printf("File is empty\n"); for(ctr = 0; ctr < max; ctr++) { fread(&(*s)->p[ctr].ID, sizeof(long), 1, fp); fread(&(*s)->p[ctr].n.FName, sizeof(char), strlen((*s)->p[ctr].n.FName), fp); fread(&(*s)->p[ctr].n.LName, sizeof(char), strlen((*s)->p[ctr].n.LName), fp); fread(&(*s)->p[ctr].n.MI, sizeof(char), 1, fp); fread(&(*s)->p[ctr].course, sizeof(char), strlen((*s)->p[ctr].course), fp); fread(&(*s)->p[ctr].level, sizeof(int), 1, fp); (*s)->last++; } printf("File Loaded\n"); getch(); clrscr(); fclose(fp); } void main() { srptr s; initialize(&s); insert(&s); write_file(s); read_file(&s); display(s); }
It will write the data that are in function insert to file "test.dat". The read_file function call doesn't exist yet.
The second time I run the program, the function call insert and write_file are deleted and function call read_file is created.
I've been getting garbage value and I can't figure out why. Please help.
•
•
Join Date: Nov 2009
Posts: 5
Reputation:
Solved Threads: 1
0
#2 Nov 10th, 2009
1. Do not use
2. Always try to free the memory which is allocated.
3. When you run the program for the second time by deleting
void main() .2. Always try to free the memory which is allocated.
3. When you run the program for the second time by deleting
insert and write_file , then the strlen function will be garbage. i.e, strlen((*s)->p , [ctr].n.FName)[/icode], , strlen((*s)->p[ctr].n.LName) , and strlen((*s)->p[ctr].course) , in the read_file , function are garbage, hence you get garbage result when you display. ![]() |
Similar Threads
- C:\Program Files\Common keeps opening on boot (Windows NT / 2000 / XP)
- Could I run applications using just the Program Files from another computer? (Windows NT / 2000 / XP)
- Program files folder is blank (Windows NT / 2000 / XP)
- Error loading C:\WINNT\Downloaded Program Files\bridge.dll (Viruses, Spyware and other Nasties)
- Error loading C:\WINDOWS\Downloaded Program Files\bridge.dll (Viruses, Spyware and other Nasties)
- "WINDOWS\ SYSTEM\ BRIDGE.DLL" (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: homework help
- Next Thread: Using properties file in C
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h





