Thread: insert_After_Target In File

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    24

    insert_After_Target In File

    hello,
    I have problem with this program i wanaa insert new data after target (id from user) the compiler did not give any mistakes ???
    this is the function insert_After_Target
    Code:
     
    /*********** Insert After Target ***********/
    void insert_After_Target(FILE *f1,int t){
    	FILE *f2;
    	data R;
    	data Z;
    	f1=fopen("f1.dat","r");
    	f2=fopen("f2.dat","w");
    	while(!feof(f1)){
    		fscanf(f1,"%d %f ",&R.id,&R.gpa);
    		fprintf(f2,"%d %f ",R.id,R.gpa);
    		if(R.id==t){
    			read(&Z);
    			fprintf(f2,"%d %f ",Z.id,Z.gpa);
    		}
    	}
    	fclose(f1);
    	fclose(f2);
    	f1=fopen("f1.dat","w");
    	f2=fopen("f2.dat","r");
    	while(!feof(f2)){
    		fscanf(f2,"%d %f ",&R.id,&R.gpa);
    		fprintf(f1,"%d %f ",R.id,R.gpa);
    	}
    	fclose(f1);
    	fclose(f2);
    }
    --------------
    this is the program
    Code:
    #include<stdio.h>
    /*********** Data ***********/
    typedef struct {
    	int id;
    	float gpa;
    }data;
    /*********** The functions ***********/
    void read(data*);
    void print(FILE*);
    void insert_last(FILE*);
    void insert_After_Target(FILE*,int);
    int search(FILE*,int);
    int size(FILE*);
    void menu();
    /*********** Main ***********/
    int main(){
    	data R;
    	int s;
    	FILE *f;
    	int choice;
    	int i,t,n;
    	if((f=fopen("f.dat","w"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	for(i=0;i<3;i++){
    		read(&R);
    		fprintf(f,"%d %.2f ",R.id,R.gpa);
    	}
    	fclose(f);
    	menu();
    	scanf("%d",&choice);
    	while(choice!=5){
    		switch(choice){
    		case 1:
    			s=size(f);
    			printf("The Size is : %d\n",s);
    			menu();
    			break;
    		case 2:
    			insert_last(f);
    			menu();
    			break;
    		case 3:
    			printf("Enter the Taget: ");
    			scanf("%d",&t);
    			n=search(f,t);
    			printf("n=%d\n",n);
    			if(n==1)
    			insert_After_Target(f,t);
    			else
    				printf("Target not found\n");
    			menu();
    			break;
    		case 4:
    			print(f);
    			menu();
    			break;
    		default :
    			printf("Error choice try agan\n");
    		 menu();
    			break;
    		}
    		scanf("%d",&choice);
    	} 
    
    	}
    	return 0;
    }
    
    /*********** Menu ***********/
    void menu(){
    	printf("+------------------------------------------------------+");
    	printf("\n|                | The Main Menu |                     |");
    	printf("\n+------------------------------------------------------+");
    	printf("\n| 1- Size of File.                                     |");
    	printf("\n| 2- Inserts a new data in the last.                   |");
    	printf("\n| 3-Inserts After Target.                              |");
    	printf("\n| 4- Print The File.                                   |");	
    	printf("\n| 5- End.                                              |");	
    	printf("\n+------------------------------------------------------+");
    	printf("\nEnter your choice :");
    }
    /*********** Read ***********/
    void read(data *R){
    	printf("Enter the id :");
    	scanf("%d",&R->id);
    	printf("\n");
    	printf("Enter the gpa :");
    	scanf("%f",&R->gpa);
    	printf("\n");
    }
    /*********** Print ***********/
    void print(FILE *f){
    	data R;
    		if((f=fopen("f.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    		fscanf(f,"%d %f",&R.id,&R.gpa);
    	while(!feof(f)){		
    		printf("The id is: %d the gpa is: %.2f\n",R.id,R.gpa);
    		fscanf(f,"%d %f",&R.id,&R.gpa);
    	}
    	fclose(f);
    }
    }
    /*********** Insert Last ***********/
    void insert_last(FILE *f){
    	data R;
    	if((f=fopen("f.dat","a"))==NULL)
    		printf("Sorry can not open the file\n");
    	else{
    		read(&R);
    		fprintf(f,"%d %f ",R.id,R.gpa);
    		fclose(f);
    	}
    }
    /*********** Insert After Target ***********/
    void insert_After_Target(FILE *f1,int t){
    	FILE *f2;
    	data R;
    	data Z;
    	f1=fopen("f1.dat","r");
    	f2=fopen("f2.dat","w");
    	while(!feof(f1)){
    		fscanf(f1,"%d %f ",&R.id,&R.gpa);
    		fprintf(f2,"%d %f ",R.id,R.gpa);
    		if(R.id==t){
    			read(&Z);
    			fprintf(f2,"%d %f ",Z.id,Z.gpa);
    		}
    	}
    	fclose(f1);
    	fclose(f2);
    	f1=fopen("f1.dat","w");
    	f2=fopen("f2.dat","r");
    	while(!feof(f2)){
    		fscanf(f2,"%d %f ",&R.id,&R.gpa);
    		fprintf(f1,"%d %f ",R.id,R.gpa);
    	}
    	fclose(f1);
    	fclose(f2);
    }
    /*********** Search ***********/
    int search(FILE *f,int t){
    	data R;
    	int m=0;
    	f=fopen("f.dat","r");
    
    	while(!feof(f)){
    			fscanf(f,"%d %f ",&R.id,&R.gpa);
    		if(R.id==t){
    			 m=1;
    			 fclose(f);
    			 return m;
    		}
    
    	}
    	fclose(f);
    	return m=0;
    
    }
    /*********** Size ***********/
    int size(FILE*f){
    	int c=0;
    	data R;
    	f=fopen("f.dat","r");
    	while(!feof(f)){
    		fscanf(f,"%d %f ",&R.id,&R.gpa);
    		c++;
    	}
    	fclose(f);
    	return c;
    }
    /*********** End ***********/

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >/*********** Insert After Target ***********/
    >void insert_After_Target(FILE *f1,int t){
    >	FILE *f2;
    >	data R;
    >	data Z;
    >	f1=fopen("f1.dat","r");
    
    /* Your original file was named f.dat, yet here you are opening f1.dat.  Change this to f.dat. */
    
    >	f2=fopen("f2.dat","w");
    >	while(!feof(f1)){
    >		fscanf(f1,"%d %f ",&R.id,&R.gpa);
    >		fprintf(f2,"%d %f ",R.id,R.gpa);
    >		if(R.id==t){
    >			read(&Z);
    >			fprintf(f2,"%d %f ",Z.id,Z.gpa);
    >		}
    >	}
    >	fclose(f1);
    >	fclose(f2);
    >	f1=fopen("f1.dat","w");
    
    /* Same problem here. */
    Last edited by swoopy; 12-14-2003 at 10:09 PM.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    yeeeeeees now the program run
    thank u very match

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    new error
    this error from compiler when i add the function Insert_first
    --------------------Configuration: Cpp1 - Win32 Debug--------------------
    Compiling...
    Cpp1.cpp
    Linking...
    Cpp1.obj : error LNK2001: unresolved external symbol "void __cdecl insert_first(struct _iobuf *)" (?insert_first@@YAXPAU_iobuf@@@Z)
    Debug/Cpp1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Cpp1.exe - 2 error(s), 0 warning(s)
    the function
    Code:
    /*********** Insert First ***********/
    void insert_fist(FILE *f){
    	FILE *f2;
    	data R;
    	f=fopen("f.dat","r");
    	f2=fopen("f2.dat","w");
    	read(&R);
    	fprintf(f2,"%d %f ",R.id,R.gpa);
    	while(!feof(f)){
    		fscanf(f,"%d %f ",&R.id,&R.gpa);
    		fprintf(f2,"%d %f ",R.id,R.gpa);
    	}
    	fclose(f);
    	fclose(f2);
    	f=fopen("f.dat","w");
    	f2=fopen("f2.dat","r");
    	while(!feof(f2)){
    		fscanf(f2,"%d %f ",&R.id,&R.gpa);
    		fprintf(f,"%d %f ",R.id,R.gpa);		
    	}
    	fclose(f);
    	fclose(f2);
    }
    ------
    the all program after chang
    Code:
    #include<stdio.h>
    /*********** Data ***********/
    typedef struct {
    	int id;
    	float gpa;
    }data;
    /*********** The functions ***********/
    void read(data*);
    void print(FILE*);
    void insert_last(FILE*);
    void insert_first(FILE*);
    void insert_After_Target(FILE*,int);
    void delet_first(FILE*);
    int search(FILE*,int);
    int size(FILE*);
    void copy(FILE*,FILE*);
    void menu();
    /*********** Main ***********/
    int main(){
    	data R;
    	FILE *f;
    	int choice;
    	int i,t,n;
    	if((f=fopen("f.dat","w"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    	for(i=0;i<3;i++){
    		read(&R);
    		fprintf(f,"%d %.2f ",R.id,R.gpa);
    	}
    	fclose(f);
    	menu();
    	scanf("%d",&choice);
    	while(choice!=6){
    		switch(choice){
    		case 1:
    			insert_first(f);
    			menu();
    			break;
    		case 2:
    			insert_last(f);
    			menu();
    			break;
    		case 3:
    			printf("Enter the Taget: ");
    			scanf("%d",&t);
    			n=search(f,t);
    			printf("n=%d\n",n);
    			if(n==1)
    			insert_After_Target(f,t);
    			else
    				printf("Target not found\n");
    			menu();
    			break;
    		case 4:
    			print(f);
    			menu();
    			break;
    		case 5:
    			delet_first(f);
    			menu();
    		default :
    			printf("Error choice try agan\n");
    		 menu();
    			break;
    		}
    		scanf("%d",&choice);
    	} 
    
    	}
    	return 0;
    }
    
    /*********** Menu ***********/
    void menu(){
    	printf("+------------------------------------------------------+");
    	printf("\n|                | The Main Menu |                     |");
    	printf("\n+------------------------------------------------------+");
    	printf("\n| 2- Inserts a new data in the first.                  |");
    	printf("\n| 2- Inserts a new data in the last.                   |");
    	printf("\n| 3- Inserts After Target.                             |");
    	printf("\n| 4- Print The File.                                   |");	
    	printf("\n| 5- Delet First.                                      |");
    	printf("\n| 6- End.                                              |");	
    	printf("\n+------------------------------------------------------+");
    	printf("\nEnter your choice :");
    }
    /*********** Read ***********/
    void read(data *R){
    	printf("Enter the id :");
    	scanf("%d",&R->id);
    	printf("\n");
    	printf("Enter the gpa :");
    	scanf("%f",&R->gpa);
    	printf("\n");
    }
    /*********** Print ***********/
    void print(FILE *f){
    	data R;
    		if((f=fopen("f.dat","r"))==NULL)
    		printf("sorry i can not open the file\n");
    	else{
    		fscanf(f,"%d %f",&R.id,&R.gpa);
    	while(!feof(f)){		
    		printf("The id is: %d the gpa is: %.2f\n",R.id,R.gpa);
    		fscanf(f,"%d %f",&R.id,&R.gpa);
    	}
    	fclose(f);
    }
    }
    /*********** Insert Last ***********/
    void insert_last(FILE *f){
    	data R;
    	if((f=fopen("f.dat","a"))==NULL)
    		printf("Sorry can not open the file\n");
    	else{
    		read(&R);
    		fprintf(f,"%d %f ",R.id,R.gpa);
    		fclose(f);
    	}
    }
    /*********** Insert After Target ***********/
    void insert_After_Target(FILE *f,int t){
    	FILE *f2;
    	data R;
    	data Z;
    	f=fopen("f.dat","r");
    	f2=fopen("f2.dat","w");
    	while(!feof(f)){
    		fscanf(f,"%d %f ",&R.id,&R.gpa);
    		fprintf(f2,"%d %f ",R.id,R.gpa);
    		if(R.id==t){
    			read(&Z);
    			fprintf(f2,"%d %f ",Z.id,Z.gpa);
    		}
    	}
    	fclose(f);
    	fclose(f2);
    	f=fopen("f.dat","w");
    	f2=fopen("f2.dat","r");
    	while(!feof(f2)){
    		fscanf(f2,"%d %f ",&R.id,&R.gpa);
    		fprintf(f,"%d %f ",R.id,R.gpa);
    	}
    	fclose(f);
    	fclose(f2);
    }
    /*********** Search ***********/
    int search(FILE *f,int t){
    	data R;
    	int m=0;
    	f=fopen("f.dat","r");
    
    	while(!feof(f)){
    			fscanf(f,"%d %f ",&R.id,&R.gpa);
    		if(R.id==t){
    			 m=1;
    			 fclose(f);
    			 return m;
    		}
    
    	}
    	fclose(f);
    	return m=0;
    
    }
    /*********** Size ***********/
    int size(FILE*f){
    	int c=0;
    	data R;
    	f=fopen("f.dat","r");
    	while(!feof(f)){
    		fscanf(f,"%d %f ",&R.id,&R.gpa);
    		c++;
    	}
    	fclose(f);
    	return c;
    }
    /*********** Delet First ***********/
    void delet_first(FILE *f){
    	FILE *f2;
    	data R;
    	f=fopen("f.dat","r");
    	f2=fopen("f2.dat","w");
    	fscanf(f,"%d %f ",&R.id,&R.gpa);
    	while(!feof(f)){
    		fscanf(f,"%d %f ",&R.id,&R.gpa);
    		fprintf(f2,"%d %f ",R.id,R.gpa);
    	}
    	fclose(f);
    	fclose(f2);
    	f=fopen("f.dat","w");
    	f2=fopen("f2.dat","r");
    	while(!feof(f2)){
    		fscanf(f2,"%d %f ",&R.id,&R.gpa);
    		fprintf(f,"%d %f ",R.id,R.gpa);
    	}
    	fclose(f);
    	fclose(f2);
    }
    /*********** Copy ***********/
    void copy(FILE *f,FILE *f2){
    	data R;
    	while(!feof(f2)){
    		fscanf(f2,"%d %f ",&R.id,&R.gpa);
    		fprintf(f,"%d %f ",R.id,R.gpa);
    	}
    }
    /*********** Insert First ***********/
    void insert_fist(FILE *f){
    	FILE *f2;
    	data R;
    	f=fopen("f.dat","r");
    	f2=fopen("f2.dat","w");
    	read(&R);
    	fprintf(f2,"%d %f ",R.id,R.gpa);
    	while(!feof(f)){
    		fscanf(f,"%d %f ",&R.id,&R.gpa);
    		fprintf(f2,"%d %f ",R.id,R.gpa);
    	}
    	fclose(f);
    	fclose(f2);
    	f=fopen("f.dat","w");
    	f2=fopen("f2.dat","r");
    	while(!feof(f2)){
    		fscanf(f2,"%d %f ",&R.id,&R.gpa);
    		fprintf(f,"%d %f ",R.id,R.gpa);		
    	}
    	fclose(f);
    	fclose(f2);
    }
    /*********** End ***********/

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    insert_fist != insert_first

    This error means that the function listed can not be found.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    thanks very very match evrything good now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM