file handling

Discussion in 'C' started by harim, May 7, 2011.

  1. harim

    harim New Member

    Joined:
    Apr 18, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    a program that reads data form a file i.e employeein.txt and adds 2000 in each employees salary and displays data in employeeout.txt?
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    
    
    /* compiled with Borland C++ v5.5 freeware command line compiler 
       if you are still learning basics of C file handling please use 
       Turboc v2.01 free download - very helpful listing of function usage
       just type a function name like 'fread( ' and move the cursor to
       anywhere on this function name & press ^F1 - you get popup HELP
       that you can close with ESC
    */
    
    typedef struct
    {
    	int empnum;
    	char name[ 10 ];
    	double sal;
    }
    EMPREC;
    
    EMPREC emparray[ 4 ] =
    						{
    							{	1256,	"Ram",		(double) 35600.0	},
    							{	4259,	"Raju",		(double) 21700.0	},
    							{	5678,	"Vijay",	(double) 19500.0	},
    							{	2345,	"Mahesh",	(double) 25900.0	}
    						};
    char *filename = "emprec.txt";
    						
    void main()
    {
    	FILE* fp;
    	int i, saloffset, bytes;
    	double d;
    	EMPREC e;
    	
    	printf("sizeof EMPREC : %d\n", sizeof(EMPREC));
    	printf("sizeof int : %d\n", sizeof(e.empnum));
    	printf("sizeof name field : %d\n", sizeof(e.name));
    	printf("sizeof double : %d\n", sizeof(e.sal));
    	printf("bytes unused / wasted for alignment in structure  : %d\n", 
    				sizeof(EMPREC) - sizeof(e.empnum) - sizeof(e.name) - sizeof(e.sal));
    	
    	
    	fp = fopen(filename, "wb");  /* "wb" binary mode write */
    	if( fp != NULL )
    	{
    		for( i = 0; i < sizeof(emparray) / sizeof(emparray[ 0 ]); i++ )
    		{
    			bytes = fwrite(&emparray[ i ], 1, sizeof(EMPREC), fp);
    			printf("Record : %d Bytes written : %d\n", i + 1, bytes);
    		}
    		fclose(fp);
    	}
    	
    	fp = fopen(filename, "rb"); /* "rb" binary mode read */
    	if( fp != NULL )
    	{
    		for( i = 0; i < sizeof(emparray) / sizeof(emparray[ 0 ]); i++ )
    		{
    			bytes = fread(&e, 1, sizeof(EMPREC), fp);
    			printf("Rec : %d bytes read : %d\n", i + 1, bytes);
    			printf("Emp No : %d Name : %s Salary : %g\n", e.empnum, e.name, e.sal);
    		}
    		fclose(fp);
    	}
    	
    	saloffset = (char*) &e.sal - (char*) &e;
    	printf("\n\nOffset of Field Salary within record EMPREC : %d\n\n", saloffset);
    	
    	fp = fopen(filename, "r+b");  /* "r+b" binary mode read / write */
    	if( fp != NULL )
    	{
    		for( i = 0; i < sizeof(emparray) / sizeof(emparray[ 0 ]); i++ )
    		{
    			/* seek to the offset of field salary in the record */
    			fseek(fp, i * sizeof(EMPREC) + saloffset, SEEK_SET);
    			/* since we have stored data in raw format read size of double to a variable of type double */
    			fread(&d, 1, sizeof(double), fp);
    			d += 2000.0;
    			/* after reading size of double in prev step the file pointer would have moved ahead by sizeof(double)
    			   move BACK the file pointer by the no of bytes ie sizeof(double) from the current position 
    			   ie SEEK_CUR to position it at the start of the salary field */
    			fseek(fp, -1 * sizeof(double), SEEK_CUR);
    			fwrite(&d, 1, sizeof(double), fp);  /* write the revised value of salary */
    		}
    		fflush(fp);  /* write all changes */
    		
    		fseek(fp, 0, SEEK_SET);  /* go back to start of file */
    		for( i = 0; i < sizeof(emparray) / sizeof(emparray[ 0 ]); i++ )
    		{
    			bytes = fread(&e, 1, sizeof(EMPREC), fp);
    			printf("Rec : %d bytes read : %d\n", i + 1, bytes);
    			printf("Emp No : %d Name : %s Salary : %g\n", e.empnum, e.name, e.sal);
    		}
    		fclose(fp);
    	}
    	
    	
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice