Thread: trying to write to a file?? it's not working

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Thumbs down trying to write to a file?? it's not working

    i've been trying to write an application so that the data entered is saved but it writes over the previous entry made. i was wondering if anyone could help me. i've been trying to sort the problem all day now
    i have to add sort and search to the application but cannot succeed in finishing this until i can write to a file.

    your feedback will be greatly recieved.
    Strawberry11

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What sort of file - text or binary ?
    How did you open the file - "w" "wb" "ab" ??
    How did you write to the file - did you fwrite() and fseek() or something else?
    Are you trying to read what you wrote without doing fflush() ?

    Post yer code!

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm assuming you're using something like fopen("foobarbaz", "w");

    The "w" mode will erase anything in the file. Instead, try using the "a" mode like fopen("foobarbaz", "a");. The "a" is for "append".

    Next time, please post your code so that helping you isn't such a guessing game.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    writing to a file not working??? more info

    sorry
    am opening it with wb
    and using fwrite

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <io.h>
    
    #define MAX 10
    
     struct Lib{
    	char Title[50];
    	char Author[30];
    	char Publisher[30];
    	char Year[10];
    	char ISBN[10];
    	char Edition[5];
      }Lib_info[MAX];
    
    //  struct LibCard NewRec[80];
     
    void enter(void);
    void list(void);
    void delete(void);
    void init_list(void);
    int menu_select(void);
    void load (void);
    void save(void);
    void SearchTitle(void);
    
    
    void main(void)
    {
      
    
          	char choice;
          	init_list();
    	load();
    
    for (;;) {
          
    	choice = menu_select();
    	switch(choice){
    		case 1:enter();
    		break;
    		case 2: delete();
    		break;
    		case 3:list();
    		break;
                    case 4:save();
    		break;
    		case 5:SearchTitle();
    		break;
    		case 6:exit(0);
    }
    }
    }
    //initialise the list
     
    void init_list(void)
    {
    	register int t;
    	for (t=0;t<MAX; ++t)Lib_info[t].Title[0] = '\0';
    }
    //get a menu selection
    
    	menu_select(void)
    	{char s [80];
    	int c;
    
    	printf("1.Enter\n");
    	printf("2.Delete\n");
    	printf("3.List\n");
    	printf("4.Save\n");
    	printf("5.Search\n");
    	printf("6.exit\n");
    
    	do{
    		printf("Enter choice ");
     		gets(s);
    		c = atoi(s);
    
    	}while(c<0 ||c> 6);
    	return c;
    }
    
    // ADD NEW ENTRIES TO THE FILE
    
    void enter(void)
    {
    	int slot;
    	char s[80];
    	slot = find_free();
    	if (slot==-1){
      		printf("LIst Full");
    	return;
    	}
    
    	printf("Enter Title: ");
    	gets(Lib_info[slot].Title);
    
    	printf("Enter Author: ");
    	gets(Lib_info[slot].Author);
    
    	printf("Enter Publisher: ");
    	gets(Lib_info[slot].Publisher);
    
    	printf("Enter Year: ");
    	gets(Lib_info[slot].Year);
    
    	printf("Enter ISBN: ");
    	gets(Lib_info[slot].ISBN);
    
    	printf("Enter Edition: ");
    	gets(Lib_info[slot].Edition);
    
    
    }
    
    void SearchTitle (void)
    {
    	int slot;
    	slot = search();
    	if (slot==-1){
      		printf("Not FOund");
    	return;
    
    	}
    }
    
    
    
    find_free(void)
    {
    	register int t;
    	for(t=0; Lib_info[t].Title[0] && t<MAX; ++t){
    
    		if (t==MAX) return -1;{
    		return t;
    		}
    	}
    }
    
    search(void)
    {
    	register int t;
    	char s[80];
    	char s2 [80];
    	int c;
    
    	printf("Enter TITLE: ");
    	gets(s);
    	c = atoi(s);
    
    for(t=0; Lib_info[t].Title[0] &&t<MAX; ++t){
    if (t==MAX) return -1;{
    //	if (Lib_info[t].Title  ){
    //                s2 = Lib_info[t].Title[0];
    //		printf("entry found");
    //	}
    return t;
    }
    }
    
    }
    
    //DELETE THE SELECTED ITEM
    
    void delete(void)
    {
    	register int slot;
    	char s[80];
    	printf("enter record #: ");
    	gets(s);
    	slot = atoi(s);
    
    	if (slot>=0 && slot < MAX)
    		Lib_info[slot].Title[0] = '\0';
    }
    
    
    //DISPLAY THE LIST OF ENTRIES TO THE SCREEN
    
    void list(void)
    {
    	register int t;
    	for (t=0; t<MAX; ++t) {
    		if (Lib_info[t].Title[0]) {
    			printf("%s\n",Lib_info[t].Title);
    			printf("%s\n",Lib_info[t].Author);
    			printf("%s\n",Lib_info[t].Publisher);
    			printf("%s\n",Lib_info[t].Year);
    			printf("%s\n",Lib_info[t].ISBN);
    			printf("%s\n",Lib_info[t].Edition);
    		}
    	}
    	printf("\n\n");
    }
    
    //SAVE THE LIB STRUCTURE DETAILS TO THE LIBRARY FILE
    
    void save(void)
    {
    	FILE *fp;
    	register int i;
    	if ((fp=fopen("LibCard.txt","wb"))==NULL){
    		printf("cannot open file \n");
    		return;
    
    	}
    for (i=0; i<MAX; i++)
      if (*Lib_info[i].Title)
    	if (fwrite(&Lib_info[i],
    		sizeof(struct Lib),1,fp)!=1)
    			printf("File Write error\n");
    
    fclose(fp);
    }
    
    //LOAD THE DETAILS OF THE LIBRARY FILE TO THE LIB STRUCTURE
    
    void load (void)
    
    {
    	FILE *fp;
            
    	register int i;
    	if ((fp=fopen("LibCard.txt","rb"))==NULL){
    		printf("cannot open file \n");
    		return;
    	}
    init_list();
    for (i=0; i<MAX; i++)
    	if (fread(&Lib_info[i],
    		sizeof(struct Lib),1,fp)!=1){
    			if (feof(fp)) break;
    			printf("File Write error\n");
    }
    fclose(fp);
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Threads merged

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main(void)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    > gets(s);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    In find_free()
    > if (t==MAX) return -1;
    > return t;
    Your braces make no sense, you just exit on the first pass of the loop anyway, whether you found a free slot or not
    search() is similarly broken.

    > register int t;
    It's largely a waste of time trying to guess how modern compilers allocate registers. Optimisation techniques have come a long way in the past 30 years.

    > #include <io.h>
    Non-standard header file - I couldn't see any reason why you would want this file included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM