fwrite writes only name from the structure emp and rest of them are written garbage value to the file. so it would be really great if any one of you can help me out

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



int main(){


	FILE *fp;
	char another,choice,useme;
	
	struct emp
		{

		 char name[40];
		 int age[10];
		 int bs[10];
		
	};

	 struct emp e;

	
        int stsize;

	fp=fopen("emp.dat","rb+");
	
	if(fp==NULL)
	{
		fp=fopen("emp.dat","wb+");
		puts("cannot open file");
		exit(1);
	}

       

      stsize=sizeof(struct emp);

	while(1){
		printf("\n 1. add records");
		printf("\n 2. list records");
		printf("\nyour choice\n");
                                choice=getchar();	       
		


		switch(choice)
			
		{
			case'1' :
				fseek(fp,0,SEEK_END);
				another='y';
					
				while(another=='y')
					{


			printf("\n Enter name");
                 		scanf("%s",e.name);
			printf("\n enter age\n");
			scanf("%s",e.age); 
			printf("\n enter salary\n");
			scanf("%s",e.bs);
		             	 fwrite(&e,stsize,1,fp);
					
			        
			  fflush(stdin);
			  printf("\n add another record(y/n)");		
		                   another=getchar();
			   
			}
		break;

		case '0':

			fclose(fp);
			exit(0);

		}				
	}
}

Only quickly checked but do initial this value...stsize.

My mistake...There it is

stsize=sizeof(struct emp);

Found some other problems

struct emp
		{

		 char name[40];
		 int age[10];
		 int bs[10];
		
	};

and

printf("\n enter age\n");
			scanf("%s",e.age); 
			printf("\n enter salary\n");
			scanf("%s",e.bs);

your scanf functions are using format strings "%s" while your structure defines the variables as integers

You have so many errors that I can only guess at what your trying to do...A hint write your program in pieces, making sure each piece works before you proceed to the next....Here's what I think your trying to do...like I said think

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

#define NAME "Bob Smith"
#define AGE "1234"
#define BS "9999999"

int main()
{
	FILE *fd;

	struct emp
	{
		 char name[40];
		 char age[10];
		 char bs[10];
	};

	struct emp e;
	int stsize = sizeof(struct emp);

	memset((void*)&e, 0, stsize);

	if (!(fd = fopen("emp.dat","w")))
	{
		fputs("could not open emp.data!\n", stderr);
		exit(EXIT_FAILURE);
	}

	sprintf(e.name, "%s", NAME);
	sprintf(e.age, "%s", AGE);
	sprintf(e.bs, "%s", BS);

	fwrite(&e, 1, stsize, fd);

	fclose(fd);
	exit(EXIT_SUCCESS);
}

yeah can u take a look at this code ..no error but problem is it retreive only first value of structure ie name and rest as garbage value

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



int main(){


	FILE *fp,*ft;
	char another,choice,useme;
	
	struct emp
		{

		 char name[40];
		  int  age[10];
		   int bs[10];
		
	};

	 struct emp e;
                

	fp=fopen("emp.dat","rb+");
	
	if(fp==NULL)
	{
		puts("cannt open");
		exit(0);
	}


		fp=fopen("emp.dat","wb");
		if(fp==NULL){
	
			puts("cannot open file");
			exit(1);
		}
	

       

      stsize=sizeof(e);

	while(1){
		printf("\n 1. add records");
		printf("\n 2. list records");
		printf("\nyour choice\n");
                                choice=getchar();	       
		


		switch(choice)
			
		{
		case'1' :
			fseek(fp,0,SEEK_END);
			another='y';
					
		while(another=='y')
				{


			printf("\n Enter name");
                 		 scanf("%s",e.name);
			printf("\n enter age\n");
			scanf("%d",&e.age); 
			printf("\n enter salary\n");
			scanf("%d",&e.bs);
		             	 
                                                fwrite(&e,sizeof(e),1,fp);	

					
			   fflush(stdin);   
			   another=getchar();
		                   printf("\n add another record(y/n)");		
		                       
			}
			break;

		case '2':	rewind(fp);

		while(fread(&e,sizeof(e),1,fp)==1)
		printf("\n %s %d %d",e.name,e.age,e.bs);
		break;

                	case '0':

			fclose(fp);
			exit(0);

		}				
	}
}

Before you try write or reading the values to/from a file, try printing the values and see if they are correct.

fprintf(stdout, "emp.name->%s\n", e.name);
fprintf(stdout, "emp.age->%d\n", e.age);
fprintf(stdout, "emp.bs->%d\n", e.bs);

And what is this?? To post that here is asking for a flaming war.

fflush(stdin);

This addresses every one of your problems...meaning it works.

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

#define NAME "Bob Smith"
int age = 1234;
int bs = 9999999;

int main()
{
	FILE *fd;

	struct emp
	{
		 char name[40];
		 int age;
		 int bs;
	};

	struct emp e, ans;

	memset((void*)&e, 0, sizeof(struct emp));

	if (!(fd = fopen("emp.dat","wb")))
	{
		fputs("could not open emp.dat!\n", stderr);
		exit(EXIT_FAILURE);
	}

	sprintf(e.name, "%s", NAME);
	e.age = age;
	e.bs = bs;

	fwrite(&e, 1, sizeof(struct emp), fd);

	fclose(fd);

	if (!(fd = fopen("emp.dat", "rb")))
	{
		fputs("could not open emp.dat!\n", stderr);
		exit(EXIT_FAILURE);
	}

	fread(&ans, 1, sizeof(struct emp), fd);

	fprintf(stdout, "emp.name->%s\n", ans.name);
	fprintf(stdout, "emp.age->%d\n", ans.age);
	fprintf(stdout, "emp.bs->%d\n", ans.bs);

	fclose(fd);
	exit(EXIT_SUCCESS);
}

Also..

yeah can u take a look at this code ..no error but problem is it retreive only first value of structure ie name and rest as garbage value

Maybe no errors?? The code wouldn't compile...

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.