copy the content of a file into a structure array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bogoreh
    New Member
    • Feb 2014
    • 1

    #1

    copy the content of a file into a structure array

    hi everyone
    i am trying to write a program for a library system that allow stuff to add, remove, view and delete customer. i try to use file to store data and i open the file in mode read then i store then i put the content of the file into a struture. now the problem started it is only showing me haft of the content and here is the coding. please help me

    Code:
    #include <stdio.h>
    # include <windows.h>
    # include <stdlib.h>
    #include<string.h>
    #include<conio.h>
    
    
    void search(int s,struct books eli[20]);
    void view(int x,struct books eli[20]);
    
    
    struct books{
    char name[22], book[25],date1[10], date2[10],id[7],bid[7];
    	
    };
    
    
    int main()
    {
    	
    	struct books eli[20];
    	int num,ctr=0,index=0;
    	
    	FILE *fp;
    	fp = fopen("database.txt","r");
    	
      while(fscanf(fp,"%s %s %s %s %s %s", eli[ctr].name,eli[ctr].id,eli[ctr].book,eli[ctr].bid,eli[ctr].date1,eli[ctr].date2)!=EOF)
    	{
    		fscanf(fp,"%s %s %s %s %s %s", eli[ctr].name,eli[ctr].id,eli[ctr].book,eli[ctr].bid,eli[ctr].date1,eli[ctr].date2);
    	
    	ctr=ctr+1;
    	 index=ctr+index;
    	 
    	}
    		fclose(fp);
    	
    
      do
         {
    	   system("cls");  
             printf("\n\t\t***************************************");
             printf("\n\t\t\t\t\t\t\t\t\t\t\t\t\t Welcome To BOOK MENU ");
    		 printf("\n\t\t***************************************\n");
    		 printf("\t\t1. Loan Transaction");
    		 printf("\n\t\t2. Update");
    		 printf("\n\t\t3. View Books");
    		 printf("\n\t\t4. Exit");
    		 printf("\n\t\t***************************************");
    		 printf("\n\t\t Choice:");
    		 scanf("%d", &num);
     
    		 switch(num)
    		 {
    	  
    		 case 1:
    			 search(index,eli);
    			 break;
    		 	case 2:
    		 	view(index,eli);
    				
    				break;
    		 	case 3:
    			  	return 0;	
    			 	break;
    			 default :
    			 	printf(" \n\t\t wrong value !!!");
    		 		break;
    		 }
    		
     	  
       }while(num!=3);
    
    	
    		 Sleep(2000);
    	}
    
    void view(int x,struct books eli[20])
    {
    	printf("%d",x);
    	int ct;
    for(ct=0;ct<x;ct++)
    	{
    		printf("\n%-13s %-13s %-13s %-13s %-13s %-13s",eli[ct].name,eli[ct].id,eli[ct].book,eli[ct].bid,eli[ct].date1,eli[ct].date2);
    }
    Sleep(5000);
    }
    void search(int s,struct books eli[20])
    {
    	
    	char name[20];
    	int ct=0;
    	printf("\n\t\t Username: ");
    	scanf("%s",name);
    	fflush(stdin);
    	for(ct=0;ct<s;ct++)
    	{
    	if(strcmp(name,eli[ct].name)==0)
    	{
    		
    	printf("\n%-13s %-13s %-13s %-13s %-13s %-13s",eli[ct].name,eli[ct].id,eli[ct].book,eli[ct].bid,eli[ct].date1,eli[ct].date2);
    	break;
    	}else
    		printf("could not found");
    		
    	}
    Sleep(2000);
    }
    Last edited by Rabbit; Feb 8 '14, 11:24 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I have no idea how your text file was created but I can tell you that the trick in dealing with structs and files is knowing exactly how the text file was created.

    You are using fscanf and %s which means a string with a null terminator. How did the null terminator get into the text file in the first place?

    If you are creating the text file using a text editor, then your reading of the file has to match what you typed. For example, the name can be 22 char. That means your read function must read 22 bytes OR it reads fewer bytes because you have placed a delimiter in the text file, like a comma, to stop the read before 22 characters. The read function can copy into the struct name.

    Code:
    weaknessforcats,How to Code,2014-02-01,etc...
    Here you read until you reach a comma. Since the struct has 6 members you need 5 commas before the newline character. You need to know which comma you read so you can copy to yhe correct struct member.

    I recommend two functions when dealing with structs and files. First, a PACK that takes a struct and builds ne string out of the struct members and inserts those commas. Second, and UNPACK which takes a single string and parses it into a struct:

    Code:
    void pack(struct books* book, char** string);
    void unpack(char** string, struct books* book);
    You pack, then write or you read and then unpack. Often I combine the pack and the write into ne function and the read an unpack into another function. This way all the logic of dealing with the file is in one spot. The removes a lot of clutter from main().

    Let me know what you think.

    Comment

    Working...