I am trying to read input form the user and then print them to a file but the compiler is giving me some error about invalid pointer at the fprintf statements

void addrecord(void)
{
     struct  custinfo info;
     
     FILE *customer;
     
     printf("\n\n\t\t\t  PLEASE ENTER THE CUSTOMER INFORMATION BELOW");
     
     printf("\n\n\t\t\t  REGSISRTATION NUMBER");
     scanf("%d",&info.regis);
     
     printf("\n\n\t\t\t  FIRST NAME:");
     scanf("%s",&info.fname);
     
     printf("\n\n\t\t\t  LAST NAME:");
     scanf("%s",&info.lname);
     
     printf("\n\n\t\t\t  TREATMENT:");
     scanf("%s",&info.treatment);
     
     printf("\n\n\t\t\t  ALLERGIES:");
     scanf("%s",&info.allergies); 
     
     printf("\n\n\t\t\t  DATE OF BIRTH (DD-MNT-YR):");
     scanf("%d",&info.dob);
     
     printf("\n\n\t\t\t  DATE OF LAST APPOINTMENT (24):");
     scanf("%d",&info.date);
     
     printf("\n\n\t\t\t  MONTH OF LAST APPOINNENT (07):");
     scanf("%d",&info.month);
     
     printf("\n\n\t\t\t  YEAR OF LAST APPOINTMENT (1990):");
     scanf("%d",&info.year);     
     
     
     customer = fopen("customer.txt","w");
     
     if (customer!=NULL)
            printf("\n\n\t\t\t  FILE WAS OPENED");
     else
         {
            fprintf("Registration Number:%d\n",info.regis);
            fprintf("Customer Name:%s %s\n",info.fname,info.lname);
            fprintf("Treatment:%s\n",info.treatment); 
            fprintf("Allergies:%s\n",info.allergies);
            fprintf("Date of Birth:%s\n",info.dob);
            fprintf("Last appointment date:%d-%d-%d\n",info.date,info.month,info.month)               
         }
         
     fclose(customer);
     
}

Recommended Answers

All 3 Replies

Is this the new format for posting code? What happen the code tags?

fprintf() takes a FILE * as its first argument (which would be in your case the variable customer ).

Then your logic is backwards, in the sense that if the file is opened succesfully, you inform the user about it. But if the opening of the file fails, you'll be trying to write to the file.

And last but certainly not the least, please use code tags when you post code.

Don't bother.

6 posts with no code tags = 1 warning and 5 infractions.
Fastest ban I've ever seen that wasn't for spam.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.