Thread: unable to write to a file

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    unable to write to a file

    I am facing this peculiar problem where in i am writing to a file using this function fprintf but instead of writng the data to the file i.e. name age and salary only the name gets written to the file but age and salary are instead printed some garbage values.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    
    	FILE *fp;
    	char another = 'y';
    	char name[40];
    	int age;
    	float bs;
    
    	fp = fopen("C:\\Documents and Settings\\ROHAN\\Desktop\\DESKTOP\\EMP.dat", "w");
    	if(fp == NULL){
    	
    		printf("\n Unable to open the file");
    		exit(EXIT_FAILURE);
    	}
    
    	while(another == 'y' || another == 'Y'){
    		
    		printf("\n Enter the name, age and basic salary\n");
    		scanf("%s %d %f", name, &age, &bs);
    		fprintf(fp, "%s %d %f\n", name, &age, &bs);
    
    		printf("\n Enter another emp");
    		fflush(stdin);
    		another = getche();
    	}
    	fclose(fp);
    	return 0;
    }
    So for example if i were to enter
    XYZ 34 80980
    XYZX 34 09809870


    only
    XYZ some garbage value for age and salary
    XYZX some garbage value for age and salary

    woudl be present in the file.
    Last edited by roaan; 08-07-2009 at 08:11 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Do not use 'address of' for the parameters for fprintf().

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    @nonob

    Thanks, it solved the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM