Thread: problem in loading file into program..

  1. #1
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35

    problem in loading file into program..

    i made a database program in c and so far, all functions are working(add data, remove data, modify data, view a data, view all data, and save database into a file) except for loading the saved database into the program..

    my save function looks like this:
    Code:
    int save() {
    	FILE *fp;
    	int count;
    
    	if ((fp = fopen("database.txt", "w")) != 0) {
    		for (count=0; count<=top; count++) {
    			fprintf(fp, "Name: %s\nAge: %d\nSex: %c\n", dB[count]->name, dB[count]->age, dB[count]->sex);
    			}
    		}
    	else {
    		printf("Error! Cannot open 'database.txt' for writing.\n");
    		exit(0);		
    		}
    	
    	printf("Command executed!");	
    	fclose(fp);
    	return 0;
    	}
    this function works as i want it to.
    but the problem is the load function:
    Code:
    int load() {
    	FILE *fp;
    	int count;
    
    	if ((fp = fopen("database.txt", "r")) != 0) {		
    		for (count=0 ; count<=top; count++) {
    			fscanf(fp, "Name: %s\nAge: %d\nSex: %c\n", &dB[count]->name, &dB[count]->age, &dB[count]->sex);
    			}
    		}
    	else {
    		printf("Error! Cannot open 'database.txt' for writing.\n");
    		exit(0);
    		}
    		
    	fclose(fp);
    	return 0;
    	}
    what could be wrong?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    'It's not working' does not tell us anything.
    What result are you getting now when you call load() function? garbage? segfault? or what??
    Do you allocate enough memory? and the main problem is your fscanf(). Go and read doc of fscanf()...
    You should check return value of fscanf() also...

  3. #3
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    oh.. the load function won't load its contents to the program!
    for example, if i use the viewall function once i've restarted the program after saving a database, it won't show anything eventhough database.txt has something in it..

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, if you save and close the program it writes top+1 elements to the file. When you restart the program, is there something that figures out what top is supposed to be (some magic code that runs prior to calling load) or does it have some default value (0 perhaps). If it's some default (0) then your loop won't run/load anything.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM