error C2172: 'fwrite' : actual parameter is not a pointer : parameter 1???

This is a discussion on error C2172: 'fwrite' : actual parameter is not a pointer : parameter 1??? within the C Programming forums, part of the General Programming Boards category; Full code Code: if(file != NULL){ while(head != NULL){ fwrite(head->code,sizeof(head->code),1,file); fwrite(head->name, sizeof(head->name),1,file); fwrite(head->dob->day, sizeof(head->dob->day),1,file); fwrite(head->dob->month, sizeof(head->dob->month),1,file); fwrite(head->dob->year, sizeof(head->dob->year),1,file); fwrite(head->jobCat, sizeof(head->jobCat),1,file); ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24

    error C2172: 'fwrite' : actual parameter is not a pointer : parameter 1???

    Full code
    Code:
    if(file != NULL){
    		while(head != NULL){
    			
    			fwrite(head->code,sizeof(head->code),1,file);
    
    			fwrite(head->name, sizeof(head->name),1,file);
    
    			fwrite(head->dob->day, sizeof(head->dob->day),1,file);
    
    			fwrite(head->dob->month, sizeof(head->dob->month),1,file);
    
    			fwrite(head->dob->year, sizeof(head->dob->year),1,file);
    
    			fwrite(head->jobCat, sizeof(head->jobCat),1,file);
    
    			fwrite(head->basicPay,sizeof(head->basicPay),1,file);
    //error C2172: 'fwrite' : actual parameter is not a pointer : parameter 1
    			fwrite(head->hours, sizeof(head->hours),1,file);
    
    			fwrite(head->dataElement->otHours, sizeof(head->dataElement->otHours),1,file);
    
    			fwrite(head->dataElement->otHours, sizeof(head->dataElement->otPay),1,file);
    
    			fwrite(head->dataElement->otHours, sizeof(head->dataElement->weeklyPay),1,file);
    
    			head = head->nextElement;
    		}
    Hi, i am writing these data to a binary file, but i am getting a compliation error here

    Code:
    			fwrite(head->basicPay,sizeof(head->basicPay),1,file);
    /error C2172: 'fwrite' : actual parameter is not a pointer : parameter 1
    and i dun see the reason why because the rest work fine but just that element..

    Can anyone guide me to solving this problem

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    That isn't the full code. That can't compile, now can it?

    What about giving us the struct declaration? And some more code before the snippet you posted to see what you've used the element in question for.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24
    Structure
    Code:
    struct EmployeeData
    {	
    	int		code;		/* Employee Code */
    	char *	name;		/* Employee Name */
    	int		age;		/* Employee Age	 */ 
    	double	basicPay;	/* Basic pay of Employee */
    	char	jobCat;		/* Job Category in char of Employee */
    	int		hours;		/* Hour works by Employee */
    
    	struct	Date *			dob;			/* Date of birth data element */
    	struct	Pay *			dataElement;	/* Employee Pay data element */
    	struct	EmployeeData *	nextElement;	/* Linked List, next element */
    };
    
    /*
    Structure for Pay data
    */
    struct Pay{
    
    	int		otHours;	/* Overtime hours */
    	double	weeklyPay;	/* Weekly pay */
    	double	otPay;		/* Overtime pay */
    };
    
    /*
    Structure for Date
    */
    struct Date{
    	int  month;	/* Month of year */
    	int  day;	/* Day of month */
    	int  year;	/* year */
    };
    Full Method body
    Code:
    void saveEmployeeDataByBinary(struct EmployeeData *emp)
    {
    	FILE *file;
    	char choice;
    	char fileName[100];
    	struct EmployeeData *head = emp;
    	int i;
    
    
    	if(emp == NULL)//If list is empty
    	{
    		printf("\n\tNo Employee Have been added!\n\n\t");
    		return;
    	}
    
    	while(emp != NULL){
    		//Check if all data are computed
    		if (emp->dataElement->weeklyPay == 0){
    			printf("\n\tOne or more of the employee pay have not been computed, \n\tplease compute before saving.\n\t");
    			return;
    		}
    		else
    			emp = emp->nextElement;
    	}
    	do{
    		printf("\t__________________________________________________________________\n");
    		printf("\n\tEnter the full file path that you would like to save the file to \n");
    		printf("\n\te.g. C:\\Temp\\Employee.BIN\n");
    		printf("\n\tNOTE THE .BIN EXTENSION\n\n");
    		printf("\t__________________________________________________________________\n");
    		printf("\tFile path: ");
    		gets(fileName);
    		fflush(stdin);
    
    		for(i = 0; fileName[i]; i++){
    			fileName[i] = tolower(fileName[i]);
    		}
    
    		if (strstr(fileName, ":\\") == NULL){
    			cls();
    			printf("\n\tNo Drive Was Being Specified\n\n");
    		}
    		else if(strstr(fileName, ".bin" )== NULL){
    			cls();
    			printf("\n\tInvalid file extension\n\n");
    		}
    
    	}while((strstr(fileName, ".bin" )== NULL) || (strstr(fileName, ":\\") == NULL));
    
    
    	if(fopen(fileName, "r") != NULL)// check if specified file exists
    	{
    		do{
    			printf("\n\tFile already exists!");
    			printf("\n\n\tA. Overwrite\n\n\tB. Append \n\n\tC. Close? \n");
    			printf("\n\tEnter choice: ");
    			scanf("%c",&choice);
    			fflush(stdin);
    
    			//ask user if he would like to append, overwrite or close and end the operation
    			switch(choice){
    				case 'a': case 'A':
    					file = fopen(fileName, "wb"); //overwrite
    					break;
    
    				case 'B': case 'b':
    					file = fopen(fileName, "ab"); //Append
    					break;
    
    				case 'c': case 'C':
    					printf("\t");
    					return;		//End operation
    					break;
    
    				default:
    					printf("\n\tWrong choice submitted, please try again.\n");
    					break;
    			}
    		}while ((choice!='a') && (choice!= 'b') && (choice!='c') && (choice!= 'A') && (choice!='B') && (choice!= 'C'));
    
    	}
    	else
    	{
    		file = fopen(fileName, "wb"); //open for write
    	}
    
    	if(file != NULL){
    		while(head != NULL){
    			
    			fwrite(head->code,sizeof(head->code),1,file);
    
    			fwrite(head->name, sizeof(head->name),1,file);
    
    			fwrite(head->dob->day, sizeof(head->dob->day),1,file);
    
    			fwrite(head->dob->month, sizeof(head->dob->month),1,file);
    
    			fwrite(head->dob->year, sizeof(head->dob->year),1,file);
    
    			fwrite(head->jobCat, sizeof(head->jobCat),1,file);
    
    			fwrite(head->basicPay,sizeof(head->basicPay),1,file);
    
    			fwrite(head->hours, sizeof(head->hours),1,file);
    
    			fwrite(head->dataElement->otHours, sizeof(head->dataElement->otHours),1,file);
    
    			fwrite(head->dataElement->otHours, sizeof(head->dataElement->otPay),1,file);
    
    			fwrite(head->dataElement->otHours, sizeof(head->dataElement->weeklyPay),1,file);
    
    			head = head->nextElement;
    		}
    
    		if(fclose(file)){ //if file fails to close
    			printf("\n\tError during write operation!\n\n\t");
    		}
    		else{
    			printf("\n\tFile Saved Successfully!\n\n\t");
    		}
    	}
    	else{
    		printf("\n\tError during write operation!\n\n\t");
    	}
    }

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well this is just a guess, but head->basicPay probably isn't a pointer.
    Did you try sticking a '&' infront of it and see if that works?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    fwrite() takes the address of the data you want to write.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Singapore
    Posts
    24
    Hi, i solved the problem, however, is it possible to read back the binary data value by value instead of reading it as an entire structure???

    any help appreciated

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sure, fread() and fwrite() are symmetrical/orthogonal, so using fread() in the same way as fwrite() would give you the desired result.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function formal parameter pointer style for arrays
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2003, 07:18 PM
  2. function pointer & Thread Parameter :: Multithreading
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 09-11-2002, 09:42 AM
  3. Void pointer. So, is this an actual object?
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2002, 06:19 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Replies: 1
    Last Post: 03-19-2002, 08:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21