writing to file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Norgy
    New Member
    • May 2013
    • 56

    writing to file

    i am trying to sort an array of structures and put it in a file
    here is the code
    Code:
    #include <stdio.h>
    #include<string.h>
    
    struct Contacts
    {
    	char name[20];
    	int phone;
    	char address[20];
    	
    };
    int countLines(FILE * fp);
    
    void readfromfile(int c);
         
      
      
    int main(int argc, char** argv) {
    	FILE *f=fopen("D:\\test.txt","r");
    	int c;
    	c=countLines(f);
    	
    	printf("number of lines %d  \n \n ",c);
    	
    	  
        readfromfile(c);
       
    	 
    	return 0;
    }
    int countLines(FILE * fp){
    	char line[80];
    	int counter = 0;
    	while ( fgets(line, sizeof line, fp) ){ 
    	
    		
    		counter+=1;
    		
    	}
    	return counter;
    }
       
       
      void readfromfile(int c)
      {
      	int i,j;
      	
      	struct Contacts array[c];
      	FILE *f=fopen("D:\\test.txt","rw");
      	struct Contacts temp;
      	for(i=0;i<c;i++)
      	{
      		fscanf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
      		printf("%s %d %s \n \n ",array[i].name,array[i].phone,array[i].address);
    	}
    	  
    	  for(i=0;i<c;i++)
    	  {
    	  	for(j=1;j<c;j++)
    	  	{
    	  		if(strcmp(array[i].name,array[j].name) > 0 )
    	  		{
    	  			temp=array[i];
    	  			array[i]=array[j];
    	  			array[j]=temp;
    	  			  			
    			  }
    		  }
    	  }
    	  
    	  
    		  fclose(f);
    		  
    		  
        FILE* fp = fopen("D:\\test.txt", "a");
    	for(i=0;i<c;i++)
    	{
    	fprintf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
    	}
    		  
    	  fclose(fp);
    		  
    		 
      }
    when i open the file which is written in it, a garbage is found in the file
    would anyone tell me what is the problem of this
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This struct:

    Code:
    struct Contacts
    {
    	char name[20];
    	int phone;
    	char address[20];
    
    };
    when you use fprintf to write the name with a %s says to write characters until you reach a \0. If the name is FOX you would write 3 characters and not 20. That would atart the phone in position 4 instead of 21. Not good.

    The name has to be written as 20 bytes regardless of the actual length of the name. The easiest thing is to use fwrite and write the sizeof(struct Contacts) for the number of elements in your array.

    You would then use fread to read back into a buffer the sizeof(struct Contacts) x the number of array elements.

    You can't look at this file using a text editor since the above method does write a text file.

    If you need to see the file in characters using a text editor, then you must write 20 characters for the name, convert the phone to a 12 char array and write the 12 chars, and finally write 20 characters for the address.

    Comment

    • Norgy
      New Member
      • May 2013
      • 56

      #3
      i have tried to use fread and fwrite but nothing changed

      Code:
      #include <stdio.h>
      #include<string.h>
      /* run this program using the console pauser or add your own getch, system("pause") or input loop */
      struct Contacts
      {
      	char name[20];
      	int phone;
      	char address[20];
      	
      };
      //functions prototypes
       int countLines();
       //void write (int c);
        void sort_name(int c);
           
        
        
      int main(int argc, char** argv) {
      	
      	int c;
      	c=countLines();
      	
      	printf("number of lines %d  \n \n ",c);
      	   // write ( c);
      	  
         sort_name(c);
         
      	 
      	return 0;
      }
      int countLines(){
      	char line[80];
      	FILE *fp=fopen("D:\\test.txt","r");
      	int counter = 0;
      	while ( fgets(line, sizeof line, fp) ){ 
      	
      		
      		counter+=1;
      		
      	}
      	fclose(fp);
      	return counter;
      }
         
           
         
         
       //read from file 
        void sort_name(int c)
        {
        	int i,j;
        	
        	struct Contacts array[c];
        	FILE *f=fopen("D:\\test.txt","r");
        	struct Contacts temp;
        	for(i=0;i<c;i++)
        	{
        		fscanf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
        		fread(array, sizeof(array), 1, f);
        		
      	  }
      	  
      	  for(i=0;i<c;i++)
      	  {
      	  	for(j=1;j<c;j++)
      	  	{
      	  		if(strcmp(array[i].name,array[j].name) > 0 )
      	  		{
      	  			temp=array[i];
      	  			array[i]=array[j];
      	  			array[j]=temp;
      	  			printf("sssss");
      	  			
      			  }
      		  }
      	  }
      	  
      	  
      	 
      		  fclose(f);
      		  
      		  
      		  FILE* fp = fopen("D:\\test.txt", "w");
      	for(i=0;i<c;i++)
      	{
      		fwrite(array, sizeof(array), 1, fp);
      	
      	}
      		  
      		  fclose(fp);
      		  
      		 
        }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        fscanf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
        fread(array, sizeof(array), 1, f);
        The fscanf is altering your position in the file and screwing up the fread. Just use the fread. The data will be in element 0 of the array. I suspect you will want to read the entire file in one fread so you should read c elements into array at the beginning.

        That should be the end of the fread. The rest of the code just sorts the array and never touches the file again.

        Do you know how to use your debugger?

        Comment

        Working...