Updating a record in a binary file

  • Follow


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{      struct stud
	{
		char name[20];
		int rollno;
		char br[20];

	}s;
	int x,z;
	char name[20];
	FILE *fp;
	fp=fopen("D:\\a.dat","rb+");
 	if(fp==NULL)
	{

		printf("FNF");
		exit();
	}
	puts("Enter name name to search");
	gets(name);
	while(1)
	{
	   x=fread(&s,sizeof(s),1,fp);
	   if(x==0)
	   break;
	    if(strcmp(name,s.name)==0)
	    {
		printf("\n%s    %d  %s ",s.name,s.rollno,s.br);
		puts("enter new date");
		printf("Enter name:");
		fflush(stdin);
		gets(s.name);
		printf("Enter rollno");
		fflush(stdin);
		scanf("%d",&s.rollno);
		printf("Enter branch");
		fflush(stdin);
		gets(s.br);
		fseek(fp,-sizeof(s),1);
		printf("\nold \n%s    %d  %s ",s.name,s.rollno,s.br);
		if(fwrite(&s,sizeof(s),1,fp)==1)
		{
			printf("Record  inserted...");
			break;
		}
		else
		{
			printf("There is some problem");
			break;
		}

	    }
	}
      fclose(fp);
      return 0;
}


in D drive i have a file a.dat  which is having 5 following records
ajay 1 cse
vijay 2 it
raj   3  ETC
megha 4 it
ramesh 5 cse

I want to update a perticular record from a.dat
for that i have written the above program but this program is not
updating.
plz tell me wha
----------------------------------------------------
0
Reply manish.comp05 (49) 11/16/2009 7:15:28 AM

On 2009-11-16, manish sahu <manish.comp05@gmail.com> wrote:
> #include<conio.h>

Don't include this, you don't need it and it's not portable.

> {      struct stud
> 	{
> 		char name[20];
> 		int rollno;
> 		char br[20];
>
> 	}s;

Don't declare structs inside functions.  It's bad style.

> 		fflush(stdin);

This is undefined behavior; it might coredump, it might blow up, it might
do nothing at all... Don't use this.  If you think you need this, you should
rethink your input.

> 		gets(s.name);

Don't use gets, it's unsafe.

> 		fseek(fp,-sizeof(s),1);

Don't use a literal as the last argument to fseek, use the predefined
constants.

> 			printf("Record  inserted...");

You can't "insert" data in standard C, so either you're trying to do
something wrong here or this message is incorrect.

> in D drive i have a file a.dat  which is having 5 following records
> ajay 1 cse
> vijay 2 it
> raj   3  ETC
> megha 4 it
> ramesh 5 cse

Those records do not match the structure you defined.  You have defined
a structure which has a precise size and layout, but you appear to have
records which do not match that size and layout, so of course, it doesn't
work.

> plz tell me wha

There's probably another half-dozen or so things wrong, but the above should
get you started.

-s
-- 
Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Reply usenet-nospam (2205) 11/16/2009 7:25:45 AM


Seebs wrote:
> On 2009-11-16, manish sahu <manish.comp05@gmail.com> wrote:

>>{      struct stud
>>	{
>>		char name[20];
>>		int rollno;
>>		char br[20];
>>
>>	}s;
> 
> 
> Don't declare structs inside functions.  It's bad style.

This is the first I've ever heard of structs
being something which shouldn't be declared inside of functions.

That makes no sense to me.

void q1sort(void *base, size_t nmemb, size_t size,
            int (*compar)(const void *, const void *))
{
     unsigned char *left;
     size_t middle, last, right;
     struct {
         size_t bytes;
         void *base;
     } stack[CHAR_BIT * sizeof nmemb], *stack_ptr;
     unsigned char *p1, *p2, *end, swap;

     stack -> bytes = nmemb * size;
     stack -> base  = base;
     stack_ptr = stack + 1;
     do {
         --stack_ptr;
         if (stack_ptr -> bytes > size) {
             left = stack_ptr -> base;
             right = stack_ptr -> bytes - size;
             last = middle = 0;
             do {
                 middle += size;
                 if (compar(left, left + middle) > 0) {
                     last += size;
                     p1 = left + middle;
                     p2 = left + last;
                     end = p2 + size;
                     do {
                         swap  = *p1;
                         *p1++ = *p2;
                         *p2++ = swap;
                     } while (p2 != end);
                 }
             } while (middle != right);
             p1 = left;
             p2 = left + last;
             end = p2 + size;
             do {
                 swap  = *p1;
                 *p1++ = *p2;
                 *p2++ = swap;
             } while (p2 != end);
             if (right - last > last) {
                 stack_ptr   -> base  = left + last + size;
                 stack_ptr++ -> bytes = right - last;
                 stack_ptr   -> base  = left;
                 stack_ptr++ -> bytes = last;
             } else {
                 stack_ptr++ -> bytes = last;
                 stack_ptr   -> base  = left + last + size;
                 stack_ptr++ -> bytes = right - last;
             }
         }
     } while (stack_ptr != stack);
}

-- 
pete
0
Reply pfiland (6614) 11/16/2009 7:54:07 AM

In article <Re6dnamKBJy2mJzWnZ2dnUVZ_rednZ2d@earthlink.com>,
pete  <pfiland@mindspring.com> wrote:

>> Don't declare structs inside functions.  It's bad style.

>This is the first I've ever heard of structs
>being something which shouldn't be declared inside of functions.

I'd say don't declare struct types *unnecessarily* within functions.
That's because it's generally more readable to keep the function
itself short.

And of course it rarely arises, because the vast majority of
struct types are shared between several functions.

If a struct type is genuinely private to a function, and you have a
lot of such functions in one file, then restricting its scope by
declaring it within the function saves you from having to invent a
bunch of different names.

-- Richard
-- 
Please remember to mention me / in tapes you leave behind.
0
Reply richard91 (3683) 11/16/2009 12:47:29 PM

On 2009-11-16, pete <pfiland@mindspring.com> wrote:
> Seebs wrote:
>> Don't declare structs inside functions.  It's bad style.

> This is the first I've ever heard of structs
> being something which shouldn't be declared inside of functions.

It's *usually* the case -- because an object like a data record
would usually be accessed from other functions too.

As with most rules, it is a rule that you eventually learn when to
break.

-s
-- 
Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Reply usenet-nospam (2205) 11/16/2009 10:07:14 PM

On Nov 15, 11:15=A0pm, manish sahu <manish.com...@gmail.com> wrote:
> #include<stdio.h>
> #include<conio.h>
> #include<stdlib.h>
> int main()
> { =A0 =A0 =A0struct stud
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 char name[20];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 int rollno;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 char br[20];
>
> =A0 =A0 =A0 =A0 }s;
> =A0 =A0 =A0 =A0 int x,z;
> =A0 =A0 =A0 =A0 char name[20];
> =A0 =A0 =A0 =A0 FILE *fp;
> =A0 =A0 =A0 =A0 fp=3Dfopen("D:\\a.dat","rb+");
> =A0 =A0 =A0 =A0 if(fp=3D=3DNULL)
> =A0 =A0 =A0 =A0 {
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("FNF");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 exit();
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 puts("Enter name name to search");
> =A0 =A0 =A0 =A0 gets(name);
> =A0 =A0 =A0 =A0 while(1)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0x=3Dfread(&s,sizeof(s),1,fp);
> =A0 =A0 =A0 =A0 =A0 =A0if(x=3D=3D0)
> =A0 =A0 =A0 =A0 =A0 =A0break;
> =A0 =A0 =A0 =A0 =A0 =A0 if(strcmp(name,s.name)=3D=3D0)
> =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("\n%s =A0 =A0%d =A0%s ",s.name,s.r=
ollno,s.br);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 puts("enter new date");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("Enter name:");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fflush(stdin);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 gets(s.name);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("Enter rollno");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fflush(stdin);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 scanf("%d",&s.rollno);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("Enter branch");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fflush(stdin);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 gets(s.br);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 fseek(fp,-sizeof(s),1);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("\nold \n%s =A0 =A0%d =A0%s ",s.na=
me,s.rollno,s.br);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if(fwrite(&s,sizeof(s),1,fp)=3D=3D1)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("Record =A0inserte=
d...");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 break;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("There is some pro=
blem");
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 break;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>
> =A0 =A0 =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 fclose(fp);
> =A0 =A0 =A0 return 0;
>
> }
>
> in D drive i have a file a.dat =A0which is having 5 following records
> ajay 1 cse
> vijay 2 it
> raj =A0 3 =A0ETC
> megha 4 it
> ramesh 5 cse
>
> I want to update a perticular record from a.dat
> for that i have written the above program but this program is not
> updating.
> plz tell me wha
> ----------------------------------------------------

HTH:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char*q0(char*q1,
        int q2)??<char*q3=3Dq1,
                          *q4;
if ((q1=3D=3DNULL)||(q2<1))q3=3DNULL
                              ;
else if (q2=3D=3D1)*q3=3D'\0';
else if ((q3=3Dfgets(q1,
                   q2,
                   stdin))!=3DNULL)if ((q4=3D
                   strchr(q1,
                  '\n')))*q4=3D'\0';
return q3;
??>typedef struct q5??<char q6??(
                20??);
int q7;
char q8??(20??);
??>q9;
void q10()??<static const q9 q11??(
    ??)=3D??<??<
        "\141\152\141\171",
        1,
        "\143\163\145"??>,
        ??<
        "\166\151\152\141\171",
        2,
        "\151\164"??>,

        ??<
        "\162\141\152",
        3,
        "\105\124\103"??>,
        ??<
        "\155\145\147\150\141",
        4,

        "\151\164"??>,
        ??<
        "\162\141\155\145\163\150",
        5,
        "\143\163\145"??>,
        ??>;
FILE*q12=3Dfopen(
             "\141\56\144\141\164",
             "\167\142\53");
if (q12)??<size_t
    q13;
for (q13=3D0;q13<sizeof q11/sizeof q11??(0??);q13++)??<size_t q2=3D
        fwrite(&q11??(q13??),
               sizeof q11??(0??),
               1,
               q12);
if (q2!=3D1)??<perror(
"\106\151\154\145\40\167\162\151\164\145\40\160\162\157\142\154"
"\145\155\56"
);
exit(EXIT_FAILURE);
??>??>??>else??<perror(
"\106\151\154\145\40"
"\157\160\145\156\40\160\162\157\142\154\145\155\40\157\156\40"
"\106\151\154\145\72\141\56\144\141\164\56"
);
exit(EXIT_FAILURE);
??>fclose(q12);
??>void q14(q9 q15,
            char*q16)??<
printf(
"\45\163\76\40\156\141\155\145\75\45\163\54\40\162\157\154\154"
"\156\157\75\45\144\54\40\142\162\141\156\143\150\75\45\163\n",
    q16,
    q15.q6,
    q15.q7,
    q15.q8);
??>int main(int q17,
            char**q18)??<q9 q15;
int q19;
static const size_t q20=3Dsizeof q15;
const long q21=3D(int)q20;
char q6??(
    20??);
FILE*q22;
char q23??(FILENAME_MAX??)=3D"\141\56\144\141\164";
if (q17
        >1)??<strncpy(q23,
                          q18??(1??),
                          sizeof q23);
??>q22=3Dfopen(q23,

             "\162\142\53");
if (q22=3D=3DNULL)??<q10();
q22=3Dfopen(
        "\141\56\144\141\164",

        "\162\142\53");
if (q22=3D=3DNULL)??<perror(
        "\111\57\117\40\160\162\157\142\154\145\155");
printf(
    "\125\156\141"
    "\142\154\145\40\164\157\40\157\160\145\
\156\40\45\163\n",
    q23);
exit(EXIT_FAILURE);
??>??>puts(
"\105\156\164"
"\145\162\40\156\141\155\145\40\156\141\155\145\40\164\157\40\163"
"\145\141\162\143\150"
);
q0(q6,
   sizeof q6);
for (;;)??<q19=3Dfread(&q15,
                         sizeof(q15),
                         1,
                         q22);
if (q19
        =3D=3D0)break;
if (strcmp(q6,
           q15.q6)=3D=3D0)??<char q24??(15??);
q14(q15,

    "\146\157\165\156\
\144\72\40");
puts(
    "\75\75\75\75\75\75\75\75\75\75\75\75\75\75");
puts(
"\105\156\164\145\162\40\156\145\167\40\144\141\164\141");
puts(
    "\75\75\75\75\75\75\75\75\75\75\75\75\75\
\75");
printf(
    "\105\156\164\145\162\40\156\141\155\145\72");
fflush(
    stdout);
q0(q15.q6,
   sizeof q15.q6);
printf(
    "\105\156\164\145\162\40\162\157\154\154\156\157\72");
fflush(stdout);
q0(q24,
   sizeof q24);
sscanf(q24,
       "\45\144",
       &q15.q7);
printf(
    "\105\156\164\145\162\40\142\162\141\156\143\150\72");
fflush(stdout);
q0(q15.q8,
   sizeof q15.q8);
fseek(q22,
      -q21,
      SEEK_CUR);
printf(
"\n\157\154\144\40\n\45\163\40\40\40\40\45\144\40\40\45\163\40",
    q15.q6
    ,
    q15.q7,
    q15.q8);
if (fwrite(&q15,
           sizeof(q15),
           1,
           q22)=3D=3D1)??<printf(
                   "\122"
"\145\143\157\162\144\40\162\145\167\162\151\164\164\145\156\56\56"
                   "\56"
               );
break;
??>else??<perror(
    "\124\150\145\162\145\40\151\163\40\163\157"
"\155\145\40\160\162\157\142\154\145\155\40\162\145\167\162\151\164"
    "\151\156\147\40\162\145\143\157\162\144\56"
);
break;
??>??>else??<q14(q15,

                 "\163\153\151\160\160\151\156\147\
\72\40");
??>??>fclose(q22);
return 0;
??>
0
Reply dcorbit (2696) 11/17/2009 5:36:08 AM

5 Replies
30 Views

(page loaded in 0.09 seconds)


Reply: