fread() condition problem

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1580 Views - Last Post: 13 May 2013 - 05:45 AM Rate Topic: -----

#16 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 07 May 2013 - 02:57 AM

#include <stdio.h>
#include <stdlib.h>

//declare functions
void addsale(FILE *,FILE *);
void addcust(FILE *,FILE *);
void search(int answer);
int getanswer(void);

//declare structures
struct customer
{
	int id;
	char first_name[11];
	char surname[21];
	int age;
	char gender;
	char address[50];
};

struct sales
{
	int id;
	char product_description[25];
	float price;
	int quantity;
};

int main()
{
	int selection;
	FILE *fpsales,*fpsalesbin,*fpcust,*fpcustbin;
	int id_answer;
	
	fpsales = fopen("sales.txt","r");
	fpsalesbin = fopen("sales.bin","w+b");
	
	fpcust = fopen("customers.txt","r");
	fpcustbin = fopen("customers.bin","w+b");
	
	if (fpsales == NULL)
	{
		printf("ERROR: sales.txt not found\n");
		exit(1);
	}
	
	if (fpsalesbin == NULL)
	{
		printf("ERROR: sales.bin not found\n");
		exit(1);
	}
	
	if (fpcust == NULL)
	{
		printf("ERROR: customers.txt not found\n");
		exit(1);
	}
	
	if (fpcustbin == NULL)
	{
		printf("ERROR: customers.bin not found\n");
		exit(1);
	}
	
	
	start:
	
	printf("Enter one of the following numbers to select that option:\n\n1.) Add a new sale\n2.) Add a new customer\n"
			"3.) Search the file\n4.) Exit the program\n\n");
	
	//reading in users choice
	scanf("%d", &selection);
	
	switch(selection)
	{
			case 1:printf("\nYou selected option 1: Add a new sale\n");
					addsale(fpsales,fpsalesbin);
					break;
			case 2:printf("\nYou selection option 2: Add a new customer\n");
					addcust(fpcust,fpcustbin);
					break;
			case 3:printf("\nYou selected option 3: Search the file\n");
					id_answer = getanswer();
					search(id_answer);
					break;
			case 4: return 0;
			default:printf("\nERROR: Invalid character entered\n\n");
					goto start;
					break;
     }
	
	return 0;
}

void addsale(FILE *fpsales,FILE *fpsalesbin)
{
	struct sales new_sale;
	
	printf("\nEnter sale ID: ");
	scanf("%d",&new_sale.id);		
		
	printf("Enter sale price: ");
	scanf("%f",&new_sale.price);
	
	printf("Enter sale quantity: ");
	scanf("%d",&new_sale.quantity);
	
	printf("Enter sale description: ");
	scanf("%s*c",&new_sale.product_description);
	
	fscanf(fpsalesbin, "%d %f %d %s\n", new_sale.id,
										new_sale.price,
										new_sale.quantity,
										new_sale.product_description
										);
										
	fclose(fpsales);
	fclose(fpsalesbin);
}

void addcust(FILE *fpcust,FILE *fpcustbin)
{
	struct customer new_cust;
	
	printf("\nEnter customer ID: ");
	scanf("%d",&new_cust.id);
	
	//insert error checking to make sure customer ID doesn't already exist
	
	printf("Enter customer firstname: ");
	scanf("%s*c",&new_cust.first_name);
	
	printf("Enter customer surname: ");
	scanf("%s*c", &new_cust.surname);
	
	printf("Enter customer age: ");
	scanf("%d",&new_cust.age);
	
	printf("Enter customer gender(M or F): ");
	scanf("%c",&new_cust.gender);

	printf("Enter customer address: ");
	scanf("%s*c",&new_cust.address);
	
	
	fscanf(fpcustbin, "%d %s %s %d %c %s\n", 	new_cust.id,
										new_cust.first_name,
										new_cust.surname,
										new_cust.age,
										new_cust.gender,
										new_cust.address
										);
										
	fclose(fpcust);
	fclose(fpcustbin);
}

void search(int answer)
{
	struct customer
	{
		int id;
		char first_name[11];
		char surname[21];
		int age;
		char gender;
		char address[50];
	};
	
	FILE *fp;
	
	fp = fopen("customers.bin","rb");
	
	struct customer supp;
	int offset;

	offset = (answer - 1) * sizeof(struct customer);
	fseek(fp, offset, 0);

	if((fread(&supp,sizeof(struct customer),1,fp)) != 1)
	{
		printf("\nError in reading file");
	}
	
	if((fread(&supp,sizeof(struct customer),1,fp)) ==  NULL)
	{
		perror("The following error occurred");
	}
	
	else
	{
		printf( "\nID: %d\nName: %s %s\nAge :%d\nGender: %c\nAddress: %s",
				supp.id, supp.first_name,supp.surname,supp.age,supp.gender, supp.address);
	}
}

int getanswer(void)
{
	int answer;
	
	printf("\nEnter a user ID to display their information: ");
	scanf("%d",&answer);

	return(answer);
}



I cleaned up my code a bit to make it simpler. Also I only realised there that I wasn't attaching the file properly, so it is attached properly to this post. Apologies

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#17 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 07 May 2013 - 06:43 AM

Okay, after looking at both the full program and the binary file I have noticed quite a few problems. Let's start with the errors and warnings I receive when compiling your program.

Quote

main.c||In function ‘addsale’:|
main.c|109|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[25]’ [-Wformat]|
main.c|115|warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]|
main.c|115|warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double’ [-Wformat]|
main.c|115|warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat]|
main.c||In function ‘addcust’:|
main.c|131|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[11]’ [-Wformat]|
main.c|134|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[21]’ [-Wformat]|
main.c|143|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat]|
main.c|152|warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]|
main.c|152|warning: format ‘%d’ expects argument of type ‘int *’, but argument 6 has type ‘int’ [-Wformat]|
main.c|152|warning: format ‘%c’ expects argument of type ‘char *’, but argument 7 has type ‘int’ [-Wformat]|
main.c||In function ‘search’:|
main.c|185|error: comparison between pointer and integer|
||=== Build finished: 1 errors, 10 warnings ===|


You need to recheck your documentation for the scanf() function and remember that the address of a string is the name of the variable, no ampersand needed. Also remember that the scanf() function stops processing when it encounters a space character. Also never use scanf() to retrieve a string without specifying the maximum number of characters it can receive.

printf("Enter customer surname: ");
scanf("%20s*c", &new_cust.surname); // Notice the width specifier.



And you need to recheck your documentation for fwrite(), as I already told you it never returns NULL.

Next you have a couple of style issues. You defined your structures before main, the redefined the same structures inside a couple of your functions. Get rid of the definitions inside the functions.

Also several of the c-strings in your structure have too small of a size, I'd recommend a size of at least 51 characters.

One of your biggest problems, in my opinion, is how you are opening your files and how you are writing the binary files.

In main() you open your files like:
	fpcust = fopen("customers.txt","r");
	fpcustbin = fopen("customers.bin","w+b");

The w+ erases the file contents, is that really what you want?

Then without closing these files you try to reopen them in one of your functions:

	FILE *fp;
	
	fp = fopen("customers.bin","rb");

Don't try to reopen a already open file. I recommend opening the files in main with the "r+b" open mode and pass the file pointers into the function.

Probably the biggest problem is how you writing the data to your binary file. If you want to read your file with fread() you must write the file with fwrite().


Oh, and before I forget, goto, really?? Stop using this bad practice and use one of the other more acceptable loop constructs.


Jim

This post has been edited by jimblumberg: 07 May 2013 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

#18 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 07 May 2013 - 09:49 AM

OK so thanks to your help I have made a lot of progress with my code. I have fixed the opening and closing of my text and binary files by opening them in main and passing them to the functions. I also chose to use fread() differently. I also properly converted the text files to binary in main(). I would appreciate if you would take another look at my code to help me with a few more things.

I closed the text and binary files in the functions. Would you suggest that I close them in main()? would it make a difference?

Would you recommend I use gets() instead of scanf() for inputting strings?

The searchsale() function allows you to search for a customers ID and list the purchases of that customer. I used the same code as I did for the searchdetails() function but I came across a problem. In the searchdetails() function, an ID is searched and then the details of that customer are displayed. However in the sales.bin file a single ID has multiple purchases(from a single customer). So when you search an ID, the program is meant to display multiple purchases from that user. I couldn't get it working. I tried to get the function to display a single purchase from that customer but I was wondering how I would go about getting the function to list more than one purchase. A loop?

Also I have another question;

Quote

Oh, and before I forget, goto, really?? Stop using this bad practice and use one of the other more acceptable loop constructs.


For a simple program like this, why would you advise against using goto? It seems like a simple alternative to a loop that serves the same purpose; ensuring a user enters a valid character.

One last thing (apologies in advance for the large post) but why am I still getting warnings like

Quote

181:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]


#include <stdio.h>

//declare functions
void addsale(FILE *,FILE *);
void addcust(FILE *,FILE *);
void searchdetails(void);
void searchsales(void);
void getanswer(void);

//declare structures
struct customer_rec
{
	int id;
	char first_name[21];
	char surname[21];
	int age;
	char gender;
	char address[51];
}; 
struct customer_rec cust;

struct sales
{
	int id;
	char product_description[41];
	float price;
	int quantity;
};

struct sales sale;

int main()
{
	//declaring variables
	int selection;
	FILE *fpsales,*fpsalesbin,*fpcust,*fpcustbin;
	
	//opening the sales files
	fpsales = fopen("sales.txt","r");
	fpsalesbin = fopen("sales.bin","w+b");
	
	//opening the customers files
	fpcust = fopen("customers.txt","r");
	fpcustbin = fopen("customers.bin","w+b");
    
    while((fscanf(fpcust,"%d%s%s%d\n%c\n%[^\n]\n",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)
    {        
        fwrite(&cust.id,sizeof(cust.id),1,fpcustbin);
        fwrite(cust.first_name,sizeof(cust.first_name),1,fpcustbin);
        fwrite(cust.surname,sizeof(cust.surname),1,fpcustbin);
        fwrite(&cust.age,sizeof(cust.age),1,fpcustbin);
        fwrite(&cust.gender,sizeof(cust.gender),1,fpcustbin);
        fwrite(cust.address,sizeof(cust.address),1,fpcustbin);
    }
    
    
    while((fscanf(fpsales,"%d\n%[^\n]\n%f%d",&sale.id,&sale.price,sale.product_description,&sale.quantity))!=EOF)
    { 
		fwrite(&sale.id,sizeof(sale.id),1,fpsalesbin);
		fwrite(&sale.price,sizeof(sale.price),1,fpsalesbin);
		fwrite(sale.product_description,sizeof(sale.product_description),1,fpsalesbin);
		fwrite(&sale.quantity,sizeof(sale.quantity),1,fpsalesbin);

    }
	
	//error checking to ensure that the files can be found on the harddrive
	if (fpsales == NULL)
	{
		printf("ERROR: sales.txt not found\n");
		return(1);
	}
	
	if (fpsalesbin == NULL)
	{
		printf("ERROR: sales.bin not found\n");
		return(1);
	}
	
	if (fpcust == NULL)
	{
		printf("ERROR: customers.txt not found\n");
		return(1);
	}
	
	if (fpcustbin == NULL)
	{
		printf("ERROR: customers.bin not found\n");
		return(1);
	}
	
	//allowing the program to "restart" in the event of an invalid user input
	start:
	
	printf("Enter one of the following numbers to select that option:\n\n1.) Add a new sale\n2.) Add a new customer\n"
			"3.) Search the file\n4.) Exit the program\n\n");
	
	//reading in users choice
	scanf("%d", &selection);
	
	switch(selection)
	{
			case 1:printf("\nYou selected option 1: Add a new sale\n");
					addsale(fpsales,fpsalesbin);
					break;
			case 2:printf("\nYou selection option 2: Add a new customer\n");
					addcust(fpcust,fpcustbin);
					break;
			case 3:printf("\nYou selected option 3: Search the file\n");
					getanswer();
					break;
			case 4: return 0;
			default:printf("\nERROR: Invalid character entered\n\n");
					goto start;
    }
	
}

void addsale(FILE *fpsales,FILE *fpsalesbin)
{
	struct sales new_sale;
	
	printf("\nEnter sale ID: ");
	scanf("%d",&new_sale.id);		
		
	printf("Enter sale price: ");
	scanf("%f",&new_sale.price);
	
	printf("Enter sale quantity: ");
	scanf("%d",&new_sale.quantity);
	
	printf("Enter sale description: ");
	scanf("%41s*c",new_sale.product_description);
	
	fscanf(fpsalesbin, "%d %f %d %s\n", new_sale.id,
										new_sale.price,
										new_sale.quantity,
										new_sale.product_description
										);
										
	fclose(fpsales);
	fclose(fpsalesbin);
}

void addcust(FILE *fpcust,FILE *fpcustbin)
{
	struct customer_rec new_cust;
	
	printf("\nEnter customer ID: ");
	scanf("%d",&new_cust.id);
	
	//insert error checking to make sure customer ID doesn't already exist
	
	printf("Enter customer firstname: ");
	scanf("%21s*c",new_cust.first_name);
	
	printf("Enter customer surname: ");
	scanf("%21s*c",new_cust.surname);
	
	printf("Enter customer age: ");
	scanf("%d",&new_cust.age);
	
	printf("Enter customer gender(M or F): ");
	scanf("%c",&new_cust.gender);

	printf("Enter customer address: ");
	scanf("%51s*c",new_cust.address);
	
	
	fscanf(fpcustbin, "%d %s %s %d %c %s\n", 	new_cust.id,
										new_cust.first_name,
										new_cust.surname,
										new_cust.age,
										new_cust.gender,
										new_cust.address
										);
										
	fclose(fpcust);
	fclose(fpcustbin);
}

void getanswer(void)
{
	int answer;
	
	start2:
	
	printf("\nWhat would you like to do?\n\n1.) Display a customers details\n2.) Display a customers purchases\n\n");
	scanf("%d", &answer);
	
	switch(answer)
	{
			case 1:printf("\nYou selected option 1: Display a customer details\n");
					searchdetails();
					break;
			case 2:printf("\nYou selection option 2: Display a customers purchases\n");
					searchsales();
					break;
			default:printf("\nERROR: Invalid character entered\n\n");
					goto start2;
    }
	
}

void searchdetails(void)
{
	int answer;
	
	printf("\nEnter a user ID to display the valid information: ");
	scanf("%d",&answer);
	
	FILE *fp;

	fp = fopen("customers.bin","rb");
	
	if(fp == NULL)
	{
		printf("ERROR: customers.bin not found");
		return(1);
	}
	
	int offset;

	offset = (answer - 1) * sizeof(struct customer_rec);
	fseek(fp, offset, SEEK_SET);

	fread(&cust.id,sizeof(cust.id),1,fp);
	fread(cust.first_name,sizeof(cust.first_name),1,fp);
	fread(cust.surname,sizeof(cust.surname),1,fp);
	fread(&cust.age,sizeof(cust.age),1,fp);
	fread(&cust.gender,sizeof(cust.gender),1,fp);
	fread(cust.address,sizeof(cust.address),1,fp);

    printf("\nID: %d\n",cust.id);
    printf("Name: %s %s\n",cust.first_name,cust.surname);
    printf("Age: %d\n",cust.age);
    printf("Gender: %c\n",cust.gender);
	printf("Address: %s",cust.address);
    printf("\n");
    
    fclose(fp);
}

void searchsales(void)
{
	int answer;
	
	printf("\nEnter a user ID to display the valid information: ");
	scanf("%d",&answer);
	
	FILE *fp;

	fp = fopen("sales.bin","rb");

	int offset;

	offset = (answer - 1) * sizeof(struct sales);
	fseek(fp, offset, SEEK_SET);

	fread(&sale.id,sizeof(sale.id),1,fp);
	fread(&sale.price,sizeof(sale.price),1,fp);
	fread(sale.product_description,sizeof(sale.product_description),1,fp);
	fread(&sale.quantity,sizeof(sale.quantity),1,fp);

	
    printf("\nID: %d\n",sale.id);
    printf("Price: %.2f\n",sale.price);
    printf("Product Description: %s\n",sale.product_description);
    printf("Quantity: %d\n",sale.quantity);
    printf("\n");
    
    fclose(fp);
}

Attached File(s)

  • Attached File  sales.txt (298.04K)
    Number of downloads: 21

Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 07 May 2013 - 10:06 AM

Quote

have fixed the opening and closing of my text and binary files by opening them in main and passing them to the functions.

Quote

I closed the text and binary files in the functions. Would you suggest that I close them in main()? would it make a difference?

I recommend closing the files in the function that opened them, in this case main.

But I do see that you're reopening some files in your functions, why?

Quote

Would you recommend I use gets() instead of scanf() for inputting strings?

For this program, yes. The fgets() function will allow the names and address to have spaces embedded within them. But remember to remove the new line character from the string if it is present.

Quote

For a simple program like this, why would you advise against using goto? It seems like a simple alternative to a loop that serves the same purpose; ensuring a user enters a valid character.

Because goto is a bad habit that you should never get into, even in a small program. Forget you ever heard of this loop construct and learn to use the other looping constructs.

Quote

One last thing (apologies in advance for the large post) but why am I still getting warnings like

Probably because you're not using the function properly. Remember you need to pass the address of the variable into the function.

Quote

I also chose to use fread() differently.

Why? You still need to check the return value from your fread()/fwrite() functions every time you call these function to insure they are working properly.

Jim
Was This Post Helpful? 0
  • +
  • -

#20 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 07 May 2013 - 10:13 AM

Look at the error messages I got when I compiled your code:
main.c||In function ‘main’:|
main.c|57|warning: format ‘%[^|
main.c|57|warning: format ‘%f’ expects argument of type ‘float *’, but argument 5 has type ‘char *’ [-Wformat]|
main.c||In function ‘addsale’:|
main.c|138|warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]|
main.c|138|warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double’ [-Wformat]|
main.c|138|warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat]|
main.c||In function ‘addcust’:|
main.c|175|warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]|
main.c|175|warning: format ‘%d’ expects argument of type ‘int *’, but argument 6 has type ‘int’ [-Wformat]|
main.c|175|warning: format ‘%c’ expects argument of type ‘char *’, but argument 7 has type ‘int’ [-Wformat]|
main.c||In function ‘searchdetails’:|
main.c|218|error: ‘return’ with a value, in function returning void|
||=== Build finished: 1 errors, 8 warnings ===|



You need to carefully read the documentation for printf() and scanf() these functions are very picky about the format specifiers matching the variable types.

If the specifiers don't precisely match your variables you will get undefined behavior.

Jim
Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 07 May 2013 - 10:18 AM

Also since you are no longer writing your structure to the file your fseek() is wrong, actually it's the way you're computing the offset that's wrong. You can't use the sizeof(your Structure) to compute the location, you must use the sizeof() every component, because you're writing the individual components not the structure. The sizeof(your Structure) will probably not be the same as the sum of the sizeof() individual members.


Jim
Was This Post Helpful? 0
  • +
  • -

#22 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 07 May 2013 - 10:44 AM

Quote

I recommend closing the files in the function that opened them, in this case main.

But I do see that you're reopening some files in your functions, why?


I opened the sales and customers binary files in my searchsale() and searchdetails() functions respectively because I am opening them in "rb" mode instead of "w+b" mode. Is that the correct way to go about that?

And you're right about the struct size. I did this
int structure1,structure2;
	int structure1total,structure2total;
	
	structure1 = sizeof(struct customer_rec);
	structure2 = sizeof(struct sales);
	structure1total = sizeof(cust.address) + sizeof(cust.age) + sizeof(cust.first_name) + sizeof(cust.surname) + sizeof(cust.gender) + sizeof(cust.id);
	structure2total = sizeof(sale.id) + sizeof(sale.price) + sizeof(sale.product_description) + sizeof(sale.quantity);
	
	printf("\nSize of struct customer_rec = %d\n",structure1);
	printf("Size of struct sales = %d\n",structure2);
	printf("\nSize of customer_rec total = %d",structure1total);
	printf("\nSize of sales total = %d\n\n",structure2total);


And the output was as follows

Quote

Size of struct customer_rec = 104
Size of struct sales = 56

Size of customer_rec total = 102
Size of sales total = 53


So in my fseek() instead of having
offset = (answer - 1) * sizeof(struct customer_rec);
	fseek(fp, offset, SEEK_SET);


You're suggesting that I do it like
offset = (answer - 1) * (sizeof(cust.address) + sizeof(cust.age) + sizeof(cust.first_name) + sizeof(cust.surname) + sizeof(cust.gender) + sizeof(cust.id));
fseek(fp, offset, SEEK_SET);

Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 07 May 2013 - 10:53 AM

Quote

You're suggesting that I do it like


Why did you switch from writing/reading the structure to writing the individual members? Why did you stop checking the return values of the fopen(), fread(), fwrite() functions to insure they are working correctly?

Quote

I opened the sales and customers binary files in my searchsale() and searchdetails() functions respectively because I am opening them in "rb" mode instead of "w+b" mode. Is that the correct way to go about that?


You shouldn't usually try to open the same file several times at the same time. Why are you trying to switch open modes? Is it necessary to always delete the file contents in main()?

Jim

This post has been edited by jimblumberg: 07 May 2013 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#24 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 07 May 2013 - 11:45 AM

Quote

Why did you switch from writing/reading the structure to writing the individual members? Why did you stop checking the return values of the fopen(), fread(), fwrite() functions to insure they are working correctly?


To ensure that the offset is accurate. Is that not what I should have done?

Also I got the return values for my fread/fwrite/fopen statements. They are as follows

//searchdetails()
fread(&cust.id,sizeof(cust.id),1,fp);
	fread(cust.first_name,sizeof(cust.first_name),1,fp);
	fread(cust.surname,sizeof(cust.surname),1,fp);
	fread(&cust.age,sizeof(cust.age),1,fp);
	fread(&cust.gender,sizeof(cust.gender),1,fp);
	fread(cust.address,sizeof(cust.address),1,fp);

Return values for above code: All 0's

//searchsales()
fread(&sale.id,sizeof(sale.id),1,fp);
	fread(&sale.price,sizeof(sale.price),1,fp);
	fread(sale.product_description,sizeof(sale.product_description),1,fp);
	fread(&sale.quantity,sizeof(sale.quantity),1,fp);

Return values for above code: All 1's

//fwrite() for customers.txt  
while((fscanf(fpcust,"%d%s%s%d\n%c\n%[^\n]\n",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)
    {        
        fwrite(&cust.id,sizeof(cust.id),1,fpcustbin);
        fwrite(cust.first_name,sizeof(cust.first_name),1,fpcustbin);
        fwrite(cust.surname,sizeof(cust.surname),1,fpcustbin);
        fwrite(&cust.age,sizeof(cust.age),1,fpcustbin);
        fwrite(&cust.gender,sizeof(cust.gender),1,fpcustbin);
        fwrite(cust.address,sizeof(cust.address),1,fpcustbin);
    }
    

Return values for above code: All 1's

//fwrite for sales.txt 
while((fscanf(fpsales,"%d\n%[^\n]\n%f%d",&sale.id,&sale.price,sale.product_description,&sale.quantity))!=EOF)
    { 
		fwrite(&sale.id,sizeof(sale.id),1,fpsalesbin);
		fwrite(&sale.price,sizeof(sale.price),1,fpsalesbin);
		fwrite(sale.product_description,sizeof(sale.product_description),1,fpsalesbin);
		fwrite(&sale.quantity,sizeof(sale.quantity),1,fpsalesbin);

    }

Return values for above code: All 1's

I'm terribly confused by the results. The first result suggests that the function didn't work yet it yields the correct results when I run the program with the binary file. Can you explain this please?
Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 07 May 2013 - 12:06 PM

Quote

Return values for above code: All 1's

How do you know they are all 1's? I don't see you checking the return values anywhere. You should always check these return values.

Quote

To ensure that the offset is accurate. Is that not what I should have done?

I don't understand? What does the offset have to do with reading and writing your structure?

I'm terribly confused by the results. 


Please show me how you are actually getting the results you are talking about.

Also please post your complete current code.

The following while statement is also incorrect:
    while((fscanf(fpsales,"%d\n%[^\n]\n%f%d",&sale.id,&sale.price,sale.product_description,&sale.quantity))!=EOF)



Why do you have the '\n' embedded within these while loops? What are you trying to accomplish with the "%[^\n]" specifier?


Jim
Was This Post Helpful? 0
  • +
  • -

#26 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 07 May 2013 - 03:12 PM

Apologies for not including that piece of code. I used this printf statement to print the return values of these functions
 printf("\nvalues = %d %d %d %d %d %d\n",val1,val2,val3,val4,val5,val6);


Quote

Also since you are no longer writing your structure to the file your fseek() is wrong, actually it's the way you're computing the offset that's wrong. You can't use the sizeof(your Structure) to compute the location, you must use the sizeof() every component, because you're writing the individual components not the structure. The sizeof(your Structure) will probably not be the same as the sum of the sizeof() individual members.


Quote

I don't understand? What does the offset have to do with reading and writing your structure?


I was just fixing the fseek() offset value like you suggested.

Quote

Why do you have the '\n' embedded within these while loops? What are you trying to accomplish with the "%[^\n]" specifier?


I'm not sure I found that in sample code online.

Here's my complete current code
#include <stdio.h>

//declare functions
void addsale(FILE *,FILE *);
void addcust(FILE *,FILE *);
void searchdetails(void);
void searchsales(void);
void getanswer(void);

//declare structures
struct customer_rec
{
	int id;
	char first_name[21];
	char surname[21];
	int age;
	char gender;
	char address[51];
}; 
struct customer_rec cust;

struct sales
{
	int id;
	char product_description[41];
	float price;
	int quantity;
};

struct sales sale;

int main()
{
	//declaring variables
	int selection;
	FILE *fpsales,*fpsalesbin,*fpcust,*fpcustbin;
	
	//opening the sales files
	fpsales = fopen("sales.txt","r");
	fpsalesbin = fopen("sales.bin","w+b");
	
	//opening the customers files
	fpcust = fopen("customers.txt","r");
	fpcustbin = fopen("customers.bin","w+b");
	
	int val1,val2,val3,val4,val5,val6;
    
    while((fscanf(fpcust,"%d%s%s%d\n%c\n%[^\n]\n",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)
    {        
        fwrite(&cust.id,sizeof(cust.id),1,fpcustbin);
        fwrite(cust.first_name,sizeof(cust.first_name),1,fpcustbin);
        fwrite(cust.surname,sizeof(cust.surname),1,fpcustbin);
        fwrite(&cust.age,sizeof(cust.age),1,fpcustbin);
        fwrite(&cust.gender,sizeof(cust.gender),1,fpcustbin);
        fwrite(cust.address,sizeof(cust.address),1,fpcustbin);
    }
    
    val1 = fwrite(&cust.id,sizeof(cust.id),1,fpcustbin);
    val2 = fwrite(cust.first_name,sizeof(cust.first_name),1,fpcustbin);        
    val3 = fwrite(cust.surname,sizeof(cust.surname),1,fpcustbin);
    val4 = fwrite(&cust.age,sizeof(cust.age),1,fpcustbin);
    val5 = fwrite(&cust.gender,sizeof(cust.gender),1,fpcustbin);
    val6 = fwrite(cust.address,sizeof(cust.address),1,fpcustbin);
    
     printf("fwrite customers.bin values = %d %d %d %d %d %d\n",val1,val2,val3,val4,val5,val6);
    
    while((fscanf(fpsales,"%d\n%[^\n]\n%f%d",&sale.id,&sale.price,sale.product_description,&sale.quantity))!=EOF)
    { 
		fwrite(&sale.id,sizeof(sale.id),1,fpsalesbin);
		fwrite(&sale.price,sizeof(sale.price),1,fpsalesbin);
		fwrite(sale.product_description,sizeof(sale.product_description),1,fpsalesbin);
		fwrite(&sale.quantity,sizeof(sale.quantity),1,fpsalesbin);

    }
    
	val1 = 	fwrite(&sale.id,sizeof(sale.id),1,fpsalesbin);
	val2 = 	fwrite(&sale.price,sizeof(sale.price),1,fpsalesbin);
	val3 = 	fwrite(sale.product_description,sizeof(sale.product_description),1,fpsalesbin);
	val4 = 	fwrite(&sale.quantity,sizeof(sale.quantity),1,fpsalesbin);
    
    
    printf("\nfwrite() sales.bin = values = %d %d %d %d\n\n",val1,val2,val3,val4);
	
	//error checking to ensure that the files can be found on the harddrive
	if (fpsales == NULL)
	{
		printf("ERROR: sales.txt not found\n");
		return(1);
	}
	
	if (fpsalesbin == NULL)
	{
		printf("ERROR: sales.bin not found\n");
		return(1);
	}
	
	if (fpcust == NULL)
	{
		printf("ERROR: customers.txt not found\n");
		return(1);
	}
	
	if (fpcustbin == NULL)
	{
		printf("ERROR: customers.bin not found\n");
		return(1);
	}
	
	//allowing the program to "restart" in the event of an invalid user input
	start:
	
	printf("Enter one of the following numbers to select that option:\n\n1.) Add a new sale\n2.) Add a new customer\n"
			"3.) Search the file\n4.) Exit the program\n\n");
	
	//reading in users choice
	scanf("%d", &selection);
	
	switch(selection)
	{
			case 1:printf("\nYou selected option 1: Add a new sale\n");
					addsale(fpsales,fpsalesbin);
					break;
			case 2:printf("\nYou selection option 2: Add a new customer\n");
					addcust(fpcust,fpcustbin);
					break;
			case 3:printf("\nYou selected option 3: Search the file\n");
					getanswer();
				break;
			case 4: return 0;
			default:printf("\nERROR: Invalid character entered\n\n");
					goto start;
    }
    
    fclose(fpcust);
    fclose(fpcustbin);
    fclose(fpsales);
    fclose(fpsalesbin);
	
}

void addsale(FILE *fpsales,FILE *fpsalesbin)
{
	struct sales new_sale;
	
	printf("\nEnter sale ID: ");
	scanf("%d",&new_sale.id);		
		
	printf("Enter sale price: ");
	scanf("%f",&new_sale.price);
	
	printf("Enter sale quantity: ");
	scanf("%d",&new_sale.quantity);
	
	printf("Enter sale description: ");
	scanf("%41s*c",new_sale.product_description);
	
	fscanf(fpsalesbin, "%d %f %d %s\n", new_sale.id,
										new_sale.price,
										new_sale.quantity,
										new_sale.product_description
										);
										
	fclose(fpsales);
	fclose(fpsalesbin);
}

void addcust(FILE *fpcust,FILE *fpcustbin)
{
	struct customer_rec new_cust;
	
	printf("\nEnter customer ID: ");
	scanf("%d",&new_cust.id);
	
	//insert error checking to make sure customer ID doesn't already exist
	
	printf("Enter customer firstname: ");
	scanf("%21s*c",new_cust.first_name);
	
	printf("Enter customer surname: ");
	scanf("%21s*c",new_cust.surname);
	
	printf("Enter customer age: ");
	scanf("%d",&new_cust.age);
	
	printf("Enter customer gender(M or F): ");
	scanf("%c",&new_cust.gender);

	printf("Enter customer address: ");
	scanf("%51s*c",new_cust.address);
	
	
	fscanf(fpcustbin, "%d %s %s %d %c %s\n", 	new_cust.id,
										new_cust.first_name,
										new_cust.surname,
										new_cust.age,
										new_cust.gender,
										new_cust.address
										);
										
	fclose(fpcust);
	fclose(fpcustbin);
}

void getanswer(void)
{
	int answer;
	
	start2:
	
	printf("\nWhat would you like to do?\n\n1.) Display a customers details\n2.) Display a customers purchases\n\n");
	scanf("%d", &answer);
	
	switch(answer)
	{
			case 1:printf("\nYou selected option 1: Display a customer details\n");
					searchdetails();
					break;
			case 2:printf("\nYou selection option 2: Display a customers purchases\n");
					searchsales();
					break;
			default:printf("\nERROR: Invalid character entered\n\n");
					goto start2;
    }
	
}

void searchdetails(void)
{
	int answer;
	
	printf("\nEnter a user ID to display the valid information: ");
	scanf("%d",&answer);
	
	FILE *fp;

	fp = fopen("customers.bin","rb");
	
	if(fp == NULL)
	{
		printf("ERROR: customers.bin not found");
		return(1);
	}
	
	int offset;

	offset = (answer - 1) * (sizeof(cust.address) + sizeof(cust.age) + sizeof(cust.first_name) + sizeof(cust.surname) + sizeof(cust.gender) + sizeof(cust.id));
	fseek(fp, offset, SEEK_SET);
	
	int val1,val2,val3,val4,val5,val6;
	
	fread(&cust.id,sizeof(cust.id),1,fp);
	fread(cust.first_name,sizeof(cust.first_name),1,fp);
	fread(cust.surname,sizeof(cust.surname),1,fp);
	fread(&cust.age,sizeof(cust.age),1,fp);
	fread(&cust.gender,sizeof(cust.gender),1,fp);
	fread(cust.address,sizeof(cust.address),1,fp);
	
	val1 = fread(&cust.id,sizeof(cust.id),1,fp);
	val2 = fread(cust.first_name,sizeof(cust.first_name),1,fp);
	val3 = fread(cust.surname,sizeof(cust.surname),1,fp);
	val4 = fread(&cust.age,sizeof(cust.age),1,fp);
	val5 = fread(&cust.gender,sizeof(cust.gender),1,fp);
	val6 = fread(cust.address,sizeof(cust.address),1,fp);
		
	printf("\nfread customers.bin values = %d %d %d %d %d %d\n",val1,val2,val3,val4,val5,val6);

    printf("\nID: %d\n",cust.id);
    printf("Name: %s %s\n",cust.first_name,cust.surname);
    printf("Age: %d\n",cust.age);
    printf("Gender: %c\n",cust.gender);
	printf("Address: %s",cust.address);
    printf("\n");
    
    fclose(fp);
}

void searchsales(void)
{
	int answer;
	
	printf("\nEnter a user ID to display the valid information: ");
	scanf("%d",&answer);
	
	FILE *fp;

	fp = fopen("sales.bin","rb");

	int offset;

	offset = (answer - 1) * (sizeof(sale.id) + sizeof(sale.price) + sizeof(sale.product_description) + sizeof(sale.quantity));
	fseek(fp, offset, SEEK_SET);
	
	int val1,val2,val3,val4;

	fread(&sale.id,sizeof(sale.id),1,fp);
	fread(&sale.price,sizeof(sale.price),1,fp);
	fread(sale.product_description,sizeof(sale.product_description),1,fp);
	fread(&sale.quantity,sizeof(sale.quantity),1,fp);
	
	val1 = fread(&sale.id,sizeof(sale.id),1,fp);
	val2 = fread(&sale.price,sizeof(sale.price),1,fp);
	val3 = fread(sale.product_description,sizeof(sale.product_description),1,fp);
	val4 = fread(&sale.quantity,sizeof(sale.quantity),1,fp);
	
	printf("\nfread sales.bin values = %d %d %d %d \n",val1,val2,val3,val4);
	
    printf("\nID: %d\n",sale.id);
    printf("Price: %.2f\n",sale.price);
    printf("Product Description: %s\n",sale.product_description);
    printf("Quantity: %d\n",sale.quantity);
    printf("\n");
    
    fclose(fp);
}


Was This Post Helpful? 0
  • +
  • -

#27 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 07 May 2013 - 08:12 PM

Look closely at this snippet:
    while((fscanf(fpcust,"%d%s%s%d\n%c\n%[^\n]\n",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)
    {        
        fwrite(&cust.id,sizeof(cust.id),1,fpcustbin);
        fwrite(cust.first_name,sizeof(cust.first_name),1,fpcustbin);
        fwrite(cust.surname,sizeof(cust.surname),1,fpcustbin);
        fwrite(&cust.age,sizeof(cust.age),1,fpcustbin);
        fwrite(&cust.gender,sizeof(cust.gender),1,fpcustbin);
        fwrite(cust.address,sizeof(cust.address),1,fpcustbin);
    }
    
    val1 = fwrite(&cust.id,sizeof(cust.id),1,fpcustbin);
    val2 = fwrite(cust.first_name,sizeof(cust.first_name),1,fpcustbin);        
    val3 = fwrite(cust.surname,sizeof(cust.surname),1,fpcustbin);
    val4 = fwrite(&cust.age,sizeof(cust.age),1,fpcustbin);
    val5 = fwrite(&cust.gender,sizeof(cust.gender),1,fpcustbin);
    val6 = fwrite(cust.address,sizeof(cust.address),1,fpcustbin);
    


Why are you calling fwrite() in both the loop, without checking the return value of the fwrite(). Then you write() some data again after the loop checking the values.

What good does it do to just print out the values from the second write? You should take some kind of action of the write doesn't succeed.

Quote

I was just fixing the fseek() offset value like you suggested.

But I have asked the following question a couple of times and not received an answer: "Why did you switch from writing the structure to writing each individual item?

That while loop I mentioned, the fcanf() call is incorrect. Have you fixed it yet? What exactly are you trying to do with this fscanf()?
fscanf(fpsales,"%d\n%[^\n]\n%f%d",&sale.id,&sale.price,sale.product_description,&sale.quantity)

The format specifier doesn't match the variables. Your variables are int, float, char*, int your specifier is int, char*, float, int. YOU NEED TO STUDY THE DOCUMENTATION FOR THE scanf() series of functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#28 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 08 May 2013 - 01:15 AM

Quote

But I have asked the following question a couple of times and not received an answer: "Why did you switch from writing the structure to writing each individual item?

Because when I tried this
if((fread(&cus,sizeof(struct customer),1,fp)) != 1)
	{
		printf("\nError in reading file");
	}
	
	else
	{
		printf( "\nID: %d\nName: %s %s\nAge :%d\nGender: %c\nAddress: %s",
				cus.id, cus.first_name,cus.surname,cus.age,cus.gender, cus.address);
	}



It wouldn't work remember? I tried it this way and it worked.

Quote

Why are you calling fwrite() in both the loop, without checking the return value of the fwrite(). Then you write() some data again after the loop checking the values.

What good does it do to just print out the values from the second write? You should take some kind of action of the write doesn't succeed.


I called the frwite() after the loop because I didn't think it would make much difference seeing as the frwite code snippets were identical to the ones used in the actual loop.

Quote

That while loop I mentioned, the fcanf() call is incorrect. Have you fixed it yet? What exactly are you trying to do with this fscanf()?


I came across a piece of code like that online and it worked. When I put in the normal delimiters the program wouldn't run at all. It wouldn't even print my initial menu in the terminal menu when I ran it
Was This Post Helpful? 0
  • +
  • -

#29 jimblumberg  Icon User is offline

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,458
  • Joined: 25-December 09

Re: fread() condition problem

Posted 08 May 2013 - 05:14 AM

Quote

It wouldn't work remember? I tried it this way and it worked.

It wasn't working because you were not writing the file correctly, remember? If you would've been correctly writing the file and then correctly calling and checking the return values you would have been okay.

Quote

I called the frwite() after the loop because I didn't think it would make much difference seeing as the frwite code snippets were identical to the ones used in the actual loop.

You need to check the return values every time you call the functions, not just throw in another bogus write so you can check the values. By throwing in that extra write you get another set of variables written to the file. Is that really what you want?

I came across a piece of code like that online and it worked. 

The problem with just throwing a piece of code you found online into your program without understanding it, is that you can't properly modify it to suit your needs. As I have said several times you need to read and understand the documentation for the functions you are trying to use. The fscanf() in question is malformed, as were several of the other scanf() printf() calls you are/were making. You need to insure that the format specifiers match the variables that are assigned to them. If they don't match you will not get predictable results. Have you fixed that fscanf() call yet? It's wrong!

Quote

It wouldn't even print my initial menu in the terminal menu when I ran it

You shouldn't need the '\n' in the fscanf() calls in most cases.

Please post your current code along with the complete warning/error messages your compiler is generating when you compile your code.

Jim
Was This Post Helpful? 0
  • +
  • -

#30 Briscoooe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-May 13

Re: fread() condition problem

Posted 08 May 2013 - 05:46 AM

Here's the full code so far
#include <stdio.h>

//declare functions
void addsale(FILE *,FILE *);
void addcust(FILE *,FILE *);
void searchdetails(void);
void searchsales(void);
void getanswer(void);

//declare structures
struct customer_rec
{
	int id;
	char first_name[21];
	char surname[21];
	int age;
	char gender;
	char address[51];
}; 
struct customer_rec cust;

struct sales
{
	int id;
	char product_description[41];
	float price;
	int quantity;
};

struct sales sale;

int main()
{
	//declaring variables
	int selection;
	FILE *fpsales,*fpsalesbin,*fpcust,*fpcustbin;
	
	//opening the sales files
	fpsales = fopen("sales.txt","r");
	fpsalesbin = fopen("sales.bin","w+b");
	
	//opening the customers files
	fpcust = fopen("customers.txt","r");
	fpcustbin = fopen("customers.bin","w+b");
    
    while((fscanf(fpcust,"%d%s%s%d\n%c\n%[^\n]\n",&cust.id,cust.first_name,cust.surname,&cust.age,&cust.gender,cust.address))!=EOF)
    {        
		//all 1's
        fwrite(&cust.id,sizeof(cust.id),1,fpcustbin);
        fwrite(cust.first_name,sizeof(cust.first_name),1,fpcustbin);
        fwrite(cust.surname,sizeof(cust.surname),1,fpcustbin);
        fwrite(&cust.age,sizeof(cust.age),1,fpcustbin);
        fwrite(&cust.gender,sizeof(cust.gender),1,fpcustbin);
        fwrite(cust.address,sizeof(cust.address),1,fpcustbin);
    }
    
    while((fscanf(fpsales,"%d%f%s%d",&sale.id,&sale.price,sale.product_description,&sale.quantity))!=EOF)
    //"%d\n%[^\n]\n%f%d"
    { 
		//all 1's
		fwrite(&sale.id,sizeof(sale.id),1,fpsalesbin);
		fwrite(&sale.price,sizeof(sale.price),1,fpsalesbin);
		fwrite(sale.product_description,sizeof(sale.product_description),1,fpsalesbin);
		fwrite(&sale.quantity,sizeof(sale.quantity),1,fpsalesbin);
    }
    
	//error checking to ensure that the files can be found on the harddrive
	if (fpsales == NULL)
	{
		printf("ERROR: sales.txt not found\n");
		return(1);
	}
	
	if (fpsalesbin == NULL)
	{
		printf("ERROR: sales.bin not found\n");
		return(1);
	}
	
	if (fpcust == NULL)
	{
		printf("ERROR: customers.txt not found\n");
		return(1);
	}
	
	if (fpcustbin == NULL)
	{
		printf("ERROR: customers.bin not found\n");
		return(1);
	}
	
	//allowing the program to "restart" in the event of an invalid user input
	start:
	
	printf("Enter one of the following numbers to select that option:\n\n1.) Add a new sale\n2.) Add a new customer\n"
			"3.) Search the file\n4.) Exit the program\n\n");
	
	//reading in users choice
	scanf("%d", &selection);
	
	switch(selection)
	{
			case 1:printf("\nYou selected option 1: Add a new sale\n");
					addsale(fpsales,fpsalesbin);
					break;
			case 2:printf("\nYou selection option 2: Add a new customer\n");
					addcust(fpcust,fpcustbin);
					break;
			case 3:printf("\nYou selected option 3: Search the file\n");
					getanswer();
				break;
			case 4: return 0;
			default:printf("\nERROR: Invalid character entered\n\n");
					goto start;
    }
    
    fclose(fpcust);
    fclose(fpcustbin);
    fclose(fpsales);
    fclose(fpsalesbin);
	
}

void addsale(FILE *fpsales,FILE *fpsalesbin)
{
	struct sales new_sale;
	
	printf("\nEnter sale ID: ");
	scanf("%d",&new_sale.id);		
		
	printf("Enter sale price: ");
	scanf("%f",&new_sale.price);
	
	printf("Enter sale quantity: ");
	scanf("%d",&new_sale.quantity);
	
	printf("Enter sale description: ");
	scanf("%41s*c",new_sale.product_description);
	
	fscanf(fpsalesbin, "%d %f %d %s\n", new_sale.id,
										new_sale.price,
										new_sale.quantity,
										new_sale.product_description
										);
										
	fclose(fpsales);
	fclose(fpsalesbin);
}

void addcust(FILE *fpcust,FILE *fpcustbin)
{
	struct customer_rec new_cust;
	
	printf("\nEnter customer ID: ");
	scanf("%d",&new_cust.id);
	
	//insert error checking to make sure customer ID doesn't already exist
	
	printf("Enter customer firstname: ");
	scanf("%21s*c",new_cust.first_name);
	
	printf("Enter customer surname: ");
	scanf("%21s*c",new_cust.surname);
	
	printf("Enter customer age: ");
	scanf("%d",&new_cust.age);
	
	printf("Enter customer gender(M or F): ");
	scanf("%c",&new_cust.gender);

	printf("Enter customer address: ");
	scanf("%51s*c",new_cust.address);
	
	
	fscanf(fpcustbin, "%d %s %s %d %c %s\n", 	new_cust.id,
										new_cust.first_name,
										new_cust.surname,
										new_cust.age,
										new_cust.gender,
										new_cust.address
										);
										
	fclose(fpcust);
	fclose(fpcustbin);
}

void getanswer(void)
{
	int answer;
	
	start2:
	
	printf("\nWhat would you like to do?\n\n1.) Display a customers details\n2.) Display a customers purchases\n\n");
	scanf("%d", &answer);
	
	switch(answer)
	{
			case 1:printf("\nYou selected option 1: Display a customer details\n");
					searchdetails();
					break;
			case 2:printf("\nYou selection option 2: Display a customers purchases\n");
					searchsales();
					break;
			default:printf("\nERROR: Invalid character entered\n\n");
					goto start2;
    }
	
}

void searchdetails(void)
{
	int answer;
	
	printf("\nEnter a user ID to display the valid information: ");
	scanf("%d",&answer);
	
	FILE *fp;

	fp = fopen("customers.bin","rb");
	
	if(fp == NULL)
	{
		printf("ERROR: customers.bin not found");
		return(1);
	}
	
	int offset;

	offset = (answer - 1) * (sizeof(cust.address) + sizeof(cust.age) + sizeof(cust.first_name) + sizeof(cust.surname) + sizeof(cust.gender) + sizeof(cust.id));
	fseek(fp, offset, SEEK_SET);
		
	//all 1'S
	fread(&cust.id,sizeof(cust.id),1,fp);
	fread(cust.first_name,sizeof(cust.first_name),1,fp);
	fread(cust.surname,sizeof(cust.surname),1,fp);
	fread(&cust.age,sizeof(cust.age),1,fp);
	fread(&cust.gender,sizeof(cust.gender),1,fp);
	fread(cust.address,sizeof(cust.address),1,fp);

    printf("\nID: %d\n",cust.id);
    printf("Name: %s %s\n",cust.first_name,cust.surname);
    printf("Age: %d\n",cust.age);
    printf("Gender: %c\n",cust.gender);
	printf("Address: %s",cust.address);
    printf("\n");
    
    fclose(fp);
}

void searchsales(void)
{
	int answer;
	
	printf("\nEnter a user ID to display the valid information: ");
	scanf("%d",&answer);
	
	FILE *fp;

	fp = fopen("sales.bin","rb");

	int offset;

	offset = (answer - 1) * (sizeof(sale.id) + sizeof(sale.price) + sizeof(sale.product_description) + sizeof(sale.quantity));
	fseek(fp, offset, SEEK_SET);

	//all 0's
	fread(&sale.id,sizeof(sale.id),1,fp);
	fread(&sale.price,sizeof(sale.price),1,fp);
	fread(sale.product_description,sizeof(sale.product_description),1,fp);
	fread(&sale.quantity,sizeof(sale.quantity),1,fp);
	
    printf("\nID: %d\n",sale.id);
    printf("Price: %.2f\n",sale.price);
    printf("Product Description: %s\n",sale.product_description);
    printf("Quantity: %d\n",sale.quantity);
    printf("\n");
    
    fclose(fp);
}



Here's the warnings I get upon compilation

Quote

gcc -Wall -o "test" "test.c" (in directory: /root/Desktop)
test.c: In function ‘addsale’:
test.c:150:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]
test.c:150:11: warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double’ [-Wformat]
test.c:150:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat]
test.c: In function ‘addcust’:
test.c:187:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]
test.c:187:11: warning: format ‘%d’ expects argument of type ‘int *’, but argument 6 has type ‘int’ [-Wformat]
test.c:187:11: warning: format ‘%c’ expects argument of type ‘char *’, but argument 7 has type ‘int’ [-Wformat]
test.c: In function ‘searchdetails’:
test.c:230:3: warning: ‘return’ with a value, in function returning void [enabled by default]
test.c: In function ‘main’:
test.c:128:1: warning: control reaches end of non-void function [-Wreturn-type]
Compilation finished successfully.


If I could direct your attention to line 63 you'll notice the, what I'm assuming are, the correct delimiters. It may be my compiler, but whenever I execute the code nothing shows up in the terminal window. I'm using geany on Ubuntu linux.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3