Thread: address book problem

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    address book problem

    hey yooo .... well okay let me first post the trouble some code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    #include <ctype.h>
    #include <string.h>
    
    #define NAME_SIZE 10
    #define ADD_SIZE 100
    #define PH_SIZE 15
    
    void drawscreen ( void );
    void refreshscreen( void );
    char menu( void );
    void createdb( FILE * );
    void delrecord( FILE * );
    
    #
    
    struct data{
    					char firstname[NAME_SIZE];
    					char lastname[NAME_SIZE];
    					char address[ADD_SIZE];
    					char phone[PH_SIZE];
    };
    char dbload[80];
    char filename[20];
    int tf=0;
    
    int main()
    {
    	char select;
    	char filename[20];
       	FILE *fPtr;
    	struct data Data;
    	int cnt=0;
    
    
    	strcpy( dbload,"No file loaded.." );
    
    
    
    	while ( 1 )
    	{
    
    		while ( select != 'e'){
    			 select = menu();
    			if ( select == 'a')
    			{
    				createdb( fPtr );
    				if ( tf == 0 )
    				{
    			   		gotoxy(1,25);
    			   		cprintf("Create a file or Load a file before doing anything" );
    				}
    				else{
    					strcpy( dbload,"File loaded..." );
    					rewind( fPtr );
    					 while( 1 ){
    						refreshscreen();
    				 	    drawscreen();
    				 	    gotoxy(1,5);
    					    printf("Enter the infomation below(press <return> to go back).\n" );
    						gotoxy(1,7);
    						printf("Enter person First name: " );
    						fgets( Data.firstname,sizeof(Data.firstname), stdin );
    						if ( Data.firstname[0] == '\0')
    						{
    							gotoxy(1,25);
    							cprintf("You wish to exit.");
    							getch();
    							break;
    						}
    						printf("Enter persons Surname: " );
    						fgets(Data.lastname, sizeof(Data.lastname), stdin );
    						printf("\n");
    						printf("Enter his Address: ");
    						fgets(Data.address, sizeof(Data.address), stdin);
    						printf("\n");
    						printf("Enter his phone number: ");
    						fgets(Data.phone, sizeof(Data.phone), stdin );
    						fwrite(&Data, sizeof( struct data ),1 , fPtr );
    
    				 	}
    				}
    			}
    		}
    	}
    
         return 0;
    }
    void drawscreen ( void )
    {
    	gotoxy(1,1);
        cprintf("-------------------------------------------------------------------------------");
        gotoxy(1,2);
        cprintf("                    *~*~*~* Address Book v1.0 *~*~*~*                          ");
        gotoxy(1,3);
        cprintf("-------------------------------------------------------------------------------");
    
    	}
    void refreshscreen ( void )
    {
    	 clrscr();
         textcolor(WHITE);
         textbackground(BLACK);
         gotoxy(1,25);
         cprintf("                                                                               ");
         clrscr();
         textcolor(WHITE);
         textbackground(BLUE);
         gotoxy(1,25);
        cprintf("                                                                               ");
    	gotoxy(1,4);
    }
    char menu ( void )
    {
    	int choice;
    
    	window(1,1,80,25); /*Set position and screen mode*/
    
    	refreshscreen();
    	drawscreen();
    
    	gotoxy(1,5);
    	printf( "[A]dd an Entry.\n" );
    	printf( "[D]elete an Entry.\n" );
    	printf( "[U]pdate an Entry.\n" );
    	printf( "[S]earch an Entry.\n" );
    	printf( "[L]ist all Entry.\n" );
    	printf( "[M]ake a printing version of the Entry.\n" );
    	printf( "[C]reat a Database File.\n" );
    	printf( "[F]ile Load.\n" );
    	printf( "[H]elp.\n" );
    	printf( "[E]xit.\n" );
    
    	gotoxy(1,25);
    	cprintf("Please select an option from the above menu." );
    
    	gotoxy(1,17);
    	printf("File Status : %s", dbload );
    
    	gotoxy(1,16);
    	printf( "Select an option:" );
    	choice = tolower(getch());
    
    
    	return choice;
    
    }
    void createdb( FILE *cfPtr )
    {
    	int i;
    	struct data Data = {"","","",""};
    
    	refreshscreen();
    	drawscreen();
    
    	gotoxy(1,25);
    	cprintf("Creating a Database file..." );
    	gotoxy(1,6);
    	printf("Example( C:\myaddress.txt ). ");
    	gotoxy(1,5);
    	printf("Enter the name of the file to create: ");
    	scanf( "%s", filename );
    
    	if ( ( cfPtr = fopen( filename,"w" )) == NULL )
    	{
    		gotoxy(1,25);
    		cprintf( "ERROR : Could not create file.." );
    		getch();
    	}
    	else{
    			tf = 1;
    			for ( i = 0; i <= 100; i++ )
    				fwrite( &Data, sizeof( struct data ), 1 , cfPtr );
    	}
    }
    I am using a borland 5.5 cmd line compiler ...and it gives me this warning msg

    Unreachble code int main .....

    And after i added the fgets() function it started crashing why ????

    I dont what to use scanf() coz if the person has to enter his address e.g ... 4-A SouthPark street etc...

    scanf() will only store 4-A but not the strigs after the space why??? ....

    Well i tried using scanf() with gets() and it dint worked i looked up on my fav book CprogrammingFAQ by steve summit and he gave me a good explanation why does scanF() and gets not work togeter but about the fgets() i could,nt find anything ...so help me out guys i wanna finish this thingy very soon ...


    Its 10 AM i better go to sleep ZZZzzzzz
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.c>
    
    int main(void)
    {
     FILE *fPtr;
     char name[10];
    
     printf("Enter file name: " );
     fgets(name, 10, stdin);
    
     if ( (fPtr = fopen( name, "w" )) == NULL )
        printf( "error" );
    
           system("PAUSE");
          return 0;
    }
    Why is it not woking ..it only woks with scanf() but not with gets() and fgets() ...why plzz explanation ...and the above one tooo....
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    A few remarks:

    You have an infinite loop. So function main() will never end and the code "return 0;" in main() will never be reached. And within your infinite loop, there is another infinite loop.

    In main() this code is wrong: createdb( fPtr ). The reason is that you want to change the filepointer. So it should be: createdb(&fPtr). And in the function createdb() the declaration should look like: createdb (FILE **fptr,..).

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by datainjector
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.c>
    
    int main(void)
    {
     FILE *fPtr;
     char name[10];
    
     printf("Enter file name: " );
     fgets(name, 10, stdin);
    
     if ( (fPtr = fopen( name, "w" )) == NULL )
        printf( "error" );
    
           system("PAUSE");
          return 0;
    }
    Why is it not woking ..it only woks with scanf() but not with gets() and fgets() ...why plzz explanation ...and the above one tooo....
    Using fgets() will leave the newline character in the buffer (providing there's enough room in the buffer. The newline character will be part of the string used within fopen() and will cause it to fail. So, remove that \n
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    thanks alot for ya reaply ....okay hammer now i understand how the fgets(); stuff really works it takes in any buffer that is left over ...so should i use fflush() ?? or can i do this scanf("\n");

    ???? thanks alot
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  6. #6
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    A bit about the \n part ...why does fgets() leave the \n .onto the stdin ??? Does it mean that to each call of fgets () i have to place that peace of snippets salem gave me ??? alil explanation will help alot guys ....its just that i have never used fgets() before but after reading some of ur post (Hammer, prelude, salem and quazh ) all of you guys say we should use fgets() to take in a string ....so that why i wanna start using it ...so a bit explanation on how this thing work wil help alot ...
    My code is giving me some problem it does creat a file but when i enter the input it does not save it to the file ..I put the creatdb() function in the add area for testing if it can creat a file but the input isnt showing on the file ...why???

    And please do explain about the fgets() stuff.....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    #include <ctype.h>
    #include <string.h>
    
    #define NAME_SIZE 10
    #define ADD_SIZE 100
    #define PH_SIZE 15
    #define CLEAN_BUFF while ( getchar() != '\n')
    
    
    void drawscreen ( void );
    void refreshscreen( void );
    char menu( void );
    void createdb( FILE ** , int *, char []);
    void delrecord( FILE * );
    
    struct data{
    			char firstname[NAME_SIZE];
    			char lastname[NAME_SIZE];
    			char address[ADD_SIZE];
    			char phone[PH_SIZE];
    };
    
    
    char dbload[80];
    
    int main()
    {
    	char select;
    	FILE *fPtr;
    	char filename[PH_SIZE];
    	int tf = 0;
    	struct data Data;
    
    
    	strcpy( dbload,"No file loaded.." );
    
    
    
       		while ( select != 'e'){
    			 select = menu();
    			if ( select == 'a')
    			{
    				createdb( &fPtr, &tf, filename );
    				if ( tf == 0 )
    				{
    			   		gotoxy(1,25);
    			   		cprintf("Create a file or Load a file before doing anything" );
    					getchar();
    				}
    				else{
    					strcpy( dbload,"File loaded..." );
    					while( 1 ){
    						refreshscreen();
    				 	    drawscreen();
    						fflush( stdin );
    				 	    gotoxy(1,5);
    					    printf("Enter the infomation below(press <return> to go back).\n" );
    						gotoxy(1,7);
    						printf("Enter person First name: " );
    						fgets(Data.firstname, sizeof(Data.firstname), stdin );
    
    						if ( Data.firstname[0] == '\n' )
    						{
    							gotoxy(1,25);
    							cprintf("You wish to exit.");
    							getchar();
    							break;
    						}
    						printf("Enter persons Surname: " );
    						fgets(Data.lastname, sizeof(Data.lastname), stdin );
    						printf("\n");
    						printf("Enter his Address: ");
    						fgets(Data.address, sizeof(Data.address), stdin);
    						printf("\n");
    						printf("Enter his phone number: ");
    						fgets(Data.phone, sizeof(Data.phone), stdin );
    						fprintf(fPtr, "%s %s %s %s", Data.firstname, Data.lastname, Data.address,Data.phone);
    
    				 	}
    				}
    			}
    		}
    
    
         return 0;
    }
    void drawscreen ( void )
    {
    	gotoxy(1,1);
        cprintf("-------------------------------------------------------------------------------");
        gotoxy(1,2);
        cprintf("                    *~*~*~* Address Book v1.0 *~*~*~*                          ");
        gotoxy(1,3);
        cprintf("-------------------------------------------------------------------------------");
    
    	}
    void refreshscreen ( void )
    {
    	 clrscr();
         textcolor(WHITE);
         textbackground(BLACK);
         gotoxy(1,25);
         cprintf("                                                                               ");
         clrscr();
         textcolor(WHITE);
         textbackground(BLUE);
         gotoxy(1,25);
         cprintf("                                                                               ");
         gotoxy(1,4);
    }
    char menu ( void )
    {
    	int choice;
    
    	window(1,1,80,25); /*Set position and screen mode*/
    
    	refreshscreen();
    	drawscreen();
    
    	gotoxy(1,5);
    	printf( "[A]dd an Entry.\n" );
    	printf( "[D]elete an Entry.\n" );
    	printf( "[U]pdate an Entry.\n" );
    	printf( "[S]earch an Entry.\n" );
    	printf( "[L]ist all Entry.\n" );
    	printf( "[M]ake a printing version of the Entry.\n" );
    	printf( "[C]reat a Database File.\n" );
    	printf( "[F]ile Load.\n" );
    	printf( "[H]elp.\n" );
    	printf( "[E]xit.\n" );
    
    	gotoxy(1,25);
    	cprintf("Please select an option from the above menu." );
    
    	gotoxy(1,17);
    	printf("File Status : %s", dbload );
    
    	gotoxy(1,16);
    	printf( "Select an option:" );
    	choice = tolower(getch());
    
    
    	return choice;
    
    }
    void createdb( FILE **cfPtr, int *filestat,char filename[PH_SIZE] )
    {
    	int i;
    	char *p;
    
    	refreshscreen();
    	drawscreen();
    
    	gotoxy(1,25);
    	cprintf("Creating a Database file..." );
    	gotoxy(1,6);
    	printf("Example( C:\\myaddress.txt ). ");
    	gotoxy(1,5);
    
    	printf("Enter the name of the file to create: ");
    	fgets( filename, sizeof filename, stdin );
    	if ( ( p = strchr(filename, '\n' )) != NULL ){
    			*p = '\0';
    	}
    	else{
    		while( getchar() != '\n');
    	}
    
    	if (  (*cfPtr = fopen( filename,"w" )) == NULL )
    	{
    		gotoxy(1,25);
    		cprintf( "ERROR : Could not create file.." );
    		getchar();
    	}
    	else
    		*filestat = 1;
    }
    Last edited by datainjector; 11-24-2002 at 12:41 PM.
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  7. #7
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    If you are reading this plzzz help me out with my pprevouis reply ...okay so instead of using fprint (); i wanna use fread will this work fread(&Data, sizeof( struct data),1,fPtr ); ... and fwrite();

    Now it is saving to the file but also puts some garbage there ....Some odd looking charaters i use fwrite to do it ... why???

    I have this code snippet in my code that del a file i guess it goes into an infinite loop ....why????

    Code:
    while ( fPTr != NULL ){
          fread(&Data, sizeof( struct data), 1, fPtr );
          if ( strcmp( Data.firstname, del ) == 0 ){ //del is (a char [] variable) the person input for the name he wants to delete
             fwrite( &blank, sizeof( struct data), 1, fPtr );  // blank is type struct data and contains this blank = {"","","",""};
          }
    }
    So why does it go in an infinite loop and hints or point that can help me out ...any thing is i am using or doing something out ...thank alot
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  8. #8
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    guys please help me out....in this country that i am living in its very difficult to find some decent c/c++ programmers mostly MCSE **** heads and vb programmers ...
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  9. #9
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    hey thanks aot salem rallly i appreaite it ...and yeah that what i was thinking of doing making every single menu option a function cheers ,.......
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  10. #10
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    ah well i have just notice this weird ****ty problem with my programm kinda a bug i guess .... i dont know but this programm should work but its giving me problem ....okay it saves the data to the file properly i creat the file with a w+ option ...now when i want to delete it it creat a duplicate of the record i want to delete ... why i have done something similar but that time i used fseek(); coz it was using room number .... well now i am using rewind(); i think its rewind that is making all this mess....
    Code:
    void del ( FILE **dPtr)
    {
    	struct data Data, blank = {"","","",""};
    	char del[NAME_SIZE];
    	int select, done=0;
    
    
    	while(1){
    				memset( del, 0, sizeof(del ));
                                                                    rewinr( *dPtr );
    
    				refreshscreen();
    				drawscreen();
    				gotoxy(1,25);
    				cprintf("Select the option from above to del which record....");
    				gotoxy(1,5);
    				printf("[N]ame.\n[P]hone number.\n");
    				select = tolower(getch());
    				if ( select ==  '\n' )
    				{
    			 		gotoxy(1,25);
    			   		cprintf("You wish to exit.");
    			   		getch();
    			   		break;
    			   	}
    			   	if ( select == 'n')
    			   	{
    			   		refreshscreen();
    			   		drawscreen();
    			   		gotoxy(1,25);
    			   		cprintf("Do you want to del by searching the last or first name");
    			   		gotoxy(1,5);
    			   		printf("[F]irst name\n[L]astname\n");
    			   		select = tolower(getch());
    			   		if ( select == 'f' )
    			   		{
    			   			refreshscreen();
    			   			drawscreen();
    			   			gotoxy(1,25);
    			   			cprintf("Deleting begin..");
    			   			gotoxy(1,5);
    			   			printf("Enter the person First name:");
    			   			fgets(del, sizeof ( del ), stdin );
    
    			   			gotoxy(1,25);
    			   			cprintf("Deleting file. Please wait...");
    			   			while ( !feof( *dPtr ) )
    			   			{
    			   				fread(&Data, sizeof ( struct data), 1, *dPtr );
    
    			   				if ( strcmp( Data.firstname, del ) == NULL ){
    			   					fwrite(&blank, sizeof ( struct data), 1, *dPtr );
    								done = 1;
    								gotoxy(1,25);
    			   		    		cprintf("File deleted...");
    			   	      			getch();
    								break;
    							}
    						}
    						if ( done != 1 ){
    							cprintf("Sorry record not found in file...");
    							getch();
    							break;
    						}
    					}
    			        else if ( select == 'l')
    				    {
    				 	    refreshscreen();
    				 		drawscreen();
    				 		gotoxy(1,25);
    				 		cprintf("Deleting begin..");
    				 		gotoxy(1,5);
    				 		printf("Enter the person Last name:");
    				 		fgets(del, sizeof ( del ), stdin );
    				 		gotoxy(1,25);
    				 		cprintf("Deleting file. Please wait...");
    				 		while ( !feof(*dPtr) )
    				 		{
    				 			fread(&Data, sizeof ( struct data), 1, *dPtr );
    
    							if ( strcmp( Data.lastname, del ) == NULL ){
    				 				fwrite(&blank, sizeof ( struct data), 1, *dPtr );
    								done = 1 ;
    								gotoxy(1,25);
    			   		    		cprintf("File deleted...");
    			   	      			getch();
    								break;
    							}
    						}
    
    						if ( done != 1 ){
    							gotoxy(1,25);
    							cprintf("Sorry record not found in file...");
    							getch();
    							break;
    						}
    					}
    				}
    The struct type is the same as the one i used before... i think its the rewinf() problem ???Should i attach the whole code if u guys wanna take a look at it ...thanks alot..
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  11. #11
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    i am sorry about that but i tried changing the intention but it still isnt working when i post it here or anywhere else it just arranges it in a wronge manner ?? well i guess is my unlucky day
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  12. #12
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Code:
    void del ( FILE **dPtr)
    {
    	struct data Data, blank = {"","","",""};
    	char del[NAME_SIZE];
    	int select, done=0;
    
    
    	while(1){
    			memset( del, 0, sizeof(del ));
                            rewinr( *dPtr );
         
    	               	refreshscreen();
    			drawscreen();
    			gotoxy(1,25);
    			cprintf("Select the option from above to del which record....");
    			gotoxy(1,5);
    			printf("[N]ame.\n[P]hone number.\n");
    			select = tolower(getch());
    
    			if ( select ==  '\n' )
    			{
    		 		gotoxy(1,25);
    		   		cprintf("You wish to exit.");
    		   		getch();
    		   		break;
    		   	}
    		   	if ( select == 'n')
    		   	{
    		   		refreshscreen();
    		   		drawscreen();
    		   		gotoxy(1,25);
    		   		cprintf("Do you want to del by searching the last or first name");
    		   		gotoxy(1,5);
    		   		printf("[F]irst name\n[L]astname\n");
    		   		select = tolower(getch());
    
    		   		if ( select == 'f' )
    		   		{
    		   			refreshscreen();
    		   			drawscreen();
    		   			gotoxy(1,25);
    		   			cprintf("Deleting begin..");
    		   			gotoxy(1,5);
    		   			printf("Enter the person First name:");
    		   			fgets(del, sizeof ( del ), stdin );
     		   			gotoxy(1,25);
    		   			cprintf("Deleting file. Please wait...");
    
    		   			while ( !feof( *dPtr ) )
    		   			{
    		   				fread(&Data, sizeof ( struct data), 1, *dPtr );
     			   			if ( strcmp( Data.firstname, del ) == NULL ){
     		   				    fwrite(&blank, sizeof ( struct data), 1, *dPtr );
    						    done = 1;
    						    gotoxy(1,25);
    		   	           	    	    cprintf("File deleted...");
    		   	      		            getch();
    						    break;
    					      }
    					}
    					if ( done != 1 ){
    						cprintf("Sorry record not found in file...");
    						getch();
    						break;
    					}
    					}
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  13. #13
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct data{
           char firstname[10];
           char lastname[10];
           char address[100];
           char phone[10];
           };
    
    int main(void)
    {
    
     FILE *fPtr;
     int i;
     char name[10];
     struct data Data = {"","","",""}, blank= {"","","",""};
    
    
     if ( (fPtr = fopen( "karan.txt","w+" )) == NULL )
        printf("Error");
     else{
          rewind( fPtr );
          while ( 1 ){
                      printf( "Enter first name: " );
                      fgets(Data.firstname,10, stdin );
    
                       if ( Data.firstname[0] == 'e' )
                           break;
    
                            printf( "Enter secound name: " );
                            fgets(Data.lastname,10, stdin);
                            printf( "Enter your addres: " );
                            fgets(Data.address, 100, stdin );
                            printf("Enter your phone number: " );
                            fgets(Data.phone, 10, stdin );
    
                            fwrite( &Data, sizeof( struct data ) , 1,fPtr );
          }
    
    
         while(1){
                 rewind( fPtr );
                 printf( "Enter the name to del: " );
                 scanf( "%s", name );
    
                 if ( name[0] == '\n' )
                    break;
    
                 while ( !feof( fPtr ) ){
    
                 fread( &Data, sizeof( struct data), 1, fPtr );
    
                 if ( strcmp( Data.firstname, name ) == 0 ){
                    fwrite( &blank, sizeof( struct data ), 1, fPtr );
                    printf("deleted");
                 }
        }
        }
        flcose( fPtr );
       }
    
    
    
          system("PAUSE");
          return 0;
    }
    Okay this is a better code why isnt it working it does not delete the entried why ...plzz guys help me out
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  14. #14
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    hey thanks alot i remember my friend telling me that u cant delete from the middle of the file...he said i should store the entry in a temp file and rename etc ...u know what am i talking about ..i wrote a 1000 lines code using that method a.All that editing again ...**** .Thanks alot
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hmm, easy problem from by book.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 01-07-2003, 11:14 PM
  2. Address Book
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 12-10-2002, 05:18 PM
  3. Address Book program
    By sundeeptuteja in forum C++ Programming
    Replies: 0
    Last Post: 07-28-2002, 02:08 AM