I have a file of name PROFILES.DAT in which several records
are stored. I want to create some new fields in those records so I made a new program to read the file, store records in a structure, copy the structure elements into a new structure and finally write the new structure to a file PROFILES2.DAT. The program does not reach the first while loop at line 60. Can anyone help me out? I have added the source code of the other program I used to edit the file PROFILES.DAT.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long int recsize,recsize2;
struct profile
{
	char fname[15],lname[15],month[10];
	int date,year,grade,roll,admin;
	float meng,msst,mmat,msci,mhin,pcent;
}stud;
struct newprofile
{
	char fname[15],lname[15],month[10];
	int date,year,grade,roll,admin;
	float eng1,sst1,mat1,sci1,hin1,pcent1,pcent2,pcenttotal,cgpa1,cgpa2,cgpatotal,hin2,eng2,mat2,sci2,sst2,eng,mat,sci,sst,hin;
}studnew;
FILE *fp,*file;
void main()
{
	int y=1;
	clrscr();
	file=fopen("F:\PROFILES.DAT","rb+");
	if(file==NULL)
	{
		file=fopen("F:\PROFILES.DAT","wb+");
		if(file==NULL)
		{
			puts("CANNOT OPEN THE SOURCE FILE.....!!!!!");
			getch();
			exit(2);
		}
	}
	else
	{
		gotoxy(20,10);
		cout<<"FILE1 OPENED SUCCESSFULLY";
		getch();
	}
	fp=fopen("F:\PROFILES2.DAT","rb+");
	if(fp==NULL)
	{
		fp=fopen("F:\PROFILES2.DAT","wb+");
		if(fp==NULL)
		{
			puts("CANNOT OPEN THE Target FILE.....!!!!!");
			getch();
			exit(2);
		}
	}
	else
	{
		gotoxy(20,10);
		cout<<"FILE2 OPENED SUCCESSFULLY";
		getch();
	}
	recsize2=sizeof(studnew);
	fseek(fp,0,SEEK_END);
	while(y==1)
	{
	if(fread(&stud,recsize,1,file)!=0)
		{
			clrscr();
			gotoxy(5,2);
			cout<<"You are in the record display module. Record number: ";
			gotoxy(10,4);
			cout<<"Name: ";
			printf("%s",stud.fname);
			cout<<" ";
			printf("%s",stud.lname);
			gotoxy(10,6);
			cout<<"Class: "<<stud.grade;
			gotoxy(10,7);
			cout<<"Roll number: "<<stud.roll;
			gotoxy(10,8);
			cout<<"Scholar number: "<<stud.admin;
			gotoxy(10,10);
			cout<<"Date of birth: "<<stud.date<<" ";
			printf("%s",stud.month);
			cout<<","<<stud.year;
			gotoxy(10,12);
			cout<<"Marks obtained: Hindi: "<<stud.mhin;
			gotoxy(26,13);
			cout<<"English: "<<stud.meng;
			gotoxy(26,14);
			cout<<"Maths: "<<stud.mmat;
			gotoxy(26,15);
			cout<<"Science: "<<stud.msci;
			gotoxy(26,16);
			cout<<"Social Studies: "<<stud.msst;
			gotoxy(10,18);
			cout<<"Percentage: "<<stud.pcent<<"%";
			getch();


			strcpy(studnew.fname,stud.fname);
			strcpy(studnew.lname,stud.lname);
			strcpy(studnew.month,stud.month);
			studnew.grade=stud.grade;
			studnew.admin=stud.admin;
			studnew.date=stud.date;
			studnew.roll=stud.roll;
			studnew.year=stud.year;
			studnew.eng1=stud.meng;
			studnew.mat1=stud.mmat;
			studnew.sst1=stud.msst;
			studnew.sci1=stud.msci;
			studnew.hin1=stud.mhin;
			studnew.pcent1=stud.pcent;
			studnew.hin=80;
			studnew.eng=80;
			studnew.sci=80;
			studnew.mat=80;
			studnew.sst=80;
			studnew.cgpa1=((stud.msst*1.25)+(stud.meng*1.25)+(stud.mhin*1.25)+(stud.mmat*1.25)+(stud.msci*1.25))/5;
			studnew.hin2=studnew.sci2=studnew.sst2=studnew.mat2=studnew.eng2=studnew.pcent2=studnew.pcenttotal=studnew.cgpa2=studnew.cgpatotal=0;
			fseek(fp,0,SEEK_END);

				fwrite(&studnew,recsize2,1,fp);

		}
		else
			y=0;
	}



       }

CODE OF OLD FILE

#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define UARROW 72
#define DARROW 80
#define ENTER 13
#define ESCAPE 27
int key,q=1,del=1,adminnumber;
char s=219;
FILE *file,*tmp;
long int recsize;
void display(int);
void drawbox(int,int,int,int);
void getkey();
struct profile
{
	char fname[15],lname[15],month[10];
	int date,year,grade,roll,admin;
	float meng,msst,mmat,msci,mhin,pcent;
}stud;
float hin=80.0,eng=80.0,mat=80.0,sci=80.0,sst=80.0;
void fname_search()
{
	char name[15];
	int w,i=1,match=0;
	gotoxy(5,2);
	cout<<"You are in the SEARCH BY FIRST NAME module";
	gotoxy(10,4);
	cout<<"Enter first name: ";
	gets(name);
	rewind(file);
	while(fread(&stud,recsize,1,file)==1)
	{
		if(strcmp(stud.fname,name)==0)
		{
			match=1;
			clrscr();
			gotoxy(5,2);
			cout<<"You are in the SEARCH BY FIRST NAME module";
			gotoxy(10,4);
			cout<<"Name: ";
			printf("%s",stud.fname);
			cout<<" ";
			printf("%s",stud.lname);
			gotoxy(10,6);
			cout<<"Class: "<<stud.grade;
			gotoxy(10,7);
			cout<<"Roll number: "<<stud.roll;
			gotoxy(10,8);
			cout<<"Scholar number: "<<stud.admin;
			gotoxy(10,10);
			cout<<"Date of birth: "<<stud.date<<" ";
			printf("%s",stud.month);
			cout<<","<<stud.year;
			gotoxy(10,12);
			cout<<"Marks obtained: Hindi: "<<stud.mhin;
			gotoxy(26,13);
			cout<<"English: "<<stud.meng;
			gotoxy(26,14);
			cout<<"Maths: "<<stud.mmat;
			gotoxy(26,15);
			cout<<"Science: "<<stud.msci;
			gotoxy(26,16);
			cout<<"Social Studies: "<<stud.msst;
			gotoxy(10,18);
			cout<<"Percentage: "<<stud.pcent<<"%";
			gotoxy(10,24);
			cout<<"Press ENTER to see next result.";
			getch();
			
		}
		i++;
	}
	if(match==0)
	{
		clrscr();
		gotoxy(20,10);
		cout<<"NO MATCH FOUND";
		getch();
	}
}
void lname_search()
{
	char name[15];
	int w,i=1,match=0;
	gotoxy(5,2);
	cout<<"You are in the SEARCH BY LAST NAME module";
	gotoxy(10,4);
	cout<<"Enter first name: ";
	gets(name);
	rewind(file);
	while(fread(&stud,recsize,1,file)==1)
	{
		if(strcmp(stud.lname,name)==0)
		{
			match=1;
			clrscr();
			gotoxy(5,2);
			cout<<"You are in the SEARCH BY LAST NAME module";
			gotoxy(10,4);
			cout<<"Name: ";
			printf("%s",stud.fname);
			cout<<" ";
			printf("%s",stud.lname);
			gotoxy(10,6);
			cout<<"Class: "<<stud.grade;
			gotoxy(10,7);
			cout<<"Roll number: "<<stud.roll;
			gotoxy(10,8);
			cout<<"Scholar number: "<<stud.admin;
			gotoxy(10,10);
			cout<<"Date of birth: "<<stud.date<<" ";
			printf("%s",stud.month);
			cout<<","<<stud.year;
			gotoxy(10,12);
			cout<<"Marks obtained: Hindi: "<<stud.mhin;
			gotoxy(26,13);
			cout<<"English: "<<stud.meng;
			gotoxy(26,14);
			cout<<"Maths: "<<stud.mmat;
			gotoxy(26,15);
			cout<<"Science: "<<stud.msci;
			gotoxy(26,16);
			cout<<"Social Studies: "<<stud.msst;
			gotoxy(10,18);
			cout<<"Percentage: "<<stud.pcent<<"%";
			gotoxy(10,24);
			cout<<"Press ENTER to see next result.";
			getch();
			
		}
		i++;
	}
	if(match==0)
	{
		clrscr();
		gotoxy(20,10);
		cout<<"NO MATCH FOUND";
		getch();
	}
}
void admin_search()
{
	int w,i=1,match=0,number;
	gotoxy(5,2);
	cout<<"You are in the SEARCH BY SCHOLAR NUMBER module";
	gotoxy(10,4);
	cout<<"Enter scholar number: ";
	cin>>number;
	rewind(file);
	while(fread(&stud,recsize,1,file)==1)
	{
		if(stud.admin==number)
		{
			match=1;
			clrscr();
			gotoxy(5,2);
			cout<<"You are in the SEARCH BY SCHOLAR NUMBER module";
			gotoxy(10,4);
			cout<<"Name: ";
			printf("%s",stud.fname);
			cout<<" ";
			printf("%s",stud.lname);
			gotoxy(10,6);
			cout<<"Class: "<<stud.grade;
			gotoxy(10,7);
			cout<<"Roll number: "<<stud.roll;
			gotoxy(10,8);
			cout<<"Scholar number: "<<stud.admin;
			gotoxy(10,10);
			cout<<"Date of birth: "<<stud.date<<" ";
			printf("%s",stud.month);
			cout<<","<<stud.year;
			gotoxy(10,12);
			cout<<"Marks obtained: Hindi: "<<stud.mhin;
			gotoxy(26,13);
			cout<<"English: "<<stud.meng;
			gotoxy(26,14);
			cout<<"Maths: "<<stud.mmat;
			gotoxy(26,15);
			cout<<"Science: "<<stud.msci;
			gotoxy(26,16);
			cout<<"Social Studies: "<<stud.msst;
			gotoxy(10,18);
			cout<<"Percentage: "<<stud.pcent<<"%";
			gotoxy(10,24);
			cout<<"Press anykey to go back";
			getch();
			
		}
		i++;
	}
	if(match==0)
	{
		clrscr();
		gotoxy(20,10);
		cout<<"NO MATCH FOUND";
		getch();
	}
}
void menu_search(int ch)
{
	clrscr();

		gotoxy(20,1);
		cout<<"Select a criterion by which you want to search\n\n\n\n\n";
		cout<<"                         Search by First Name\n\n";
		cout<<"                         Search by Last Name\n\n";
		cout<<"                         Search by Scholar Number\n\n";
		cout<<"                         Go back\n\n";


	gotoxy(10,2*(ch)+4);
	cout<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s;
	gotoxy(51,2*(ch)+4);
	cout<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s;
	gotoxy(10,24);
	cout<<"Press UP and DOWN ARROWS to navigate. Press ENTER to select";
}
void execute_search(int ch)
{
	clrscr();
	switch(ch)
	{
		case 1:
			fname_search();
			break;
		case 2:
			lname_search();
			break;
		case 3:
			admin_search();
			break;
		case 4:
			q=0;
			break;
	}
}
void searchrec()
{
	int ctr=1,i=1;
	q=1;
	while(ctr==1)
	{
		menu_search(i);
		getkey();
		switch(key)
		{
			case UARROW:
				i=i-1;
				break;
			case DARROW:
				i=i+1;
				break;
			case ENTER:
				execute_search(i);
				break;
		}
		if(i==0)
			i=4;
		if(i==5)
			i=1;
		ctr=q;
	}
}
void disprec()
{
	int ctr=1,i=1;
	clrscr();
	while(ctr==1)
	{
		display(i);
		getkey();
		switch(key)
		{
			case UARROW:
				i=i-1;
				break;
			case DARROW:
				i=i+1;
				break;
			case ESCAPE:
				ctr=0;
				break;
		}
		if(i==0)
			i=1;
	}
}
void addrec()
{
	fseek(file,0,SEEK_END);
	int ctr=0;
	while(ctr==0)
	{
			gotoxy(5,2);
			cout<<"You are in the record entry module. Follow the steps to enter the record.";
			gotoxy(10,4);
			cout<<"Enter first name: ";
			gets(stud.fname);
			gotoxy(10,5);
			cout<<"Enter last name: ";
			gets(stud.lname);
			gotoxy(10,6);
			cout<<"Enter grade: ";
			cin>>stud.grade;
			gotoxy(10,7);
			cout<<"Enter roll number: ";
			cin>>stud.roll;
			gotoxy(10,8);
			cout<<"Enter scholar number: ";
			cin>>stud.admin;
			gotoxy(10,10);
			cout<<"Date of birth: Date: ";
			cin>>stud.date;
			gotoxy(25,11);
			cout<<"Month(type the full name): ";
			gets(stud.month);
			gotoxy(25,12);
			cout<<"Year: ";
			cin>>stud.year;
			gotoxy(10,14);
			cout<<"Marks entry: Hindi: ";
			cin>>stud.mhin;
			gotoxy(23,15);
			cout<<"English: ";
			cin>>stud.meng;
			gotoxy(23,16);
			cout<<"Maths: ";
			cin>>stud.mmat;
			gotoxy(23,17);
			cout<<"Science: ";
			cin>>stud.msci;
			gotoxy(23,18);
			cout<<"Social Studies: ";
			cin>>stud.msst;
			gotoxy(10,20);
			cout<<"Record Has been saved";
			getch();
			stud.pcent=((stud.mhin+stud.meng+stud.mmat+stud.msci+stud.msst)*100)/(hin+eng+sst+sci+mat);
			fwrite(&stud,recsize,1,file);
			clrscr();
			gotoxy(10,10);
			cout<<"Press ENTER to add another record.\n";
			gotoxy(10,11);
			cout<<"Press ESCAPE to go back.\n";
			getkey();
			clrscr();
			switch(key)
			{
				case ENTER:
					ctr=0;
					break;
				case ESCAPE:
					ctr=1;
			}
	}
}
void setdefault()
{
	clrscr();
	gotoxy(5,2);
	cout<<"Set maximum marks for the subjects-";
	gotoxy(10,4);
	cout<<"Hindi: ";
	cin>>hin;
	gotoxy(10,5);
	cout<<"English: ";
	cin>>eng;
	gotoxy(10,6);
	cout<<"Maths: ";
	cin>>mat;
	gotoxy(10,7);
	cout<<"Science: ";
	cin>>sci;
	gotoxy(10,8);
	cout<<"Social Studies: ";
	cin>>sst;
	gotoxy(10,10);
	cout<<"The values for maximum marks have been saved";
	getch();
}
void display(int ch)
{
	int qwert=0,i;
	rewind(file);
	for(i=1;i<=ch;i++)
	{
		if(fread(&stud,recsize,1,file)!=0)
		{
			clrscr();
			gotoxy(5,2);
			cout<<"You are in the record display module. Record number: "<<ch;
			gotoxy(10,4);
			cout<<"Name: ";
			printf("%s",stud.fname);
			cout<<" ";
			printf("%s",stud.lname);
			gotoxy(10,6);
			cout<<"Class: "<<stud.grade;
			gotoxy(10,7);
			cout<<"Roll number: "<<stud.roll;
			gotoxy(10,8);
			cout<<"Scholar number: "<<stud.admin;
			gotoxy(10,10);
			cout<<"Date of birth: "<<stud.date<<" ";
			printf("%s",stud.month);
			cout<<","<<stud.year;
			gotoxy(10,12);
			cout<<"Marks obtained: Hindi: "<<stud.mhin;
			gotoxy(26,13);
			cout<<"English: "<<stud.meng;
			gotoxy(26,14);
			cout<<"Maths: "<<stud.mmat;
			gotoxy(26,15);
			cout<<"Science: "<<stud.msci;
			gotoxy(26,16);
			cout<<"Social Studies: "<<stud.msst;
			gotoxy(10,18);
			cout<<"Percentage: "<<stud.pcent<<"%";
			gotoxy(10,24);
			cout<<"Press UP and DOWN ARROWS to navigate. Press ESCAPE to go back";
			qwert=1;
		}
		else
		{
			ch=ch-1;
			clrscr();
			gotoxy(20,10);
			cout<<"NO MORE RECORDS FOUND";
			gotoxy(10,24);
			cout<<"Press UP ARROW to view previous record. Press ESCAPE to go back";
		}
	}

	if(qwert==0)
		cout<<"NO RECORDS FOUND";
}
void drawbox(int boxa,int boxb,int boxc,int boxd)
{
	int i,j;
	char s=205,c=201,q=186,d=187,e=200,f=188;
	gotoxy(boxa,boxb);
	cout<<c;
	gotoxy(boxa,boxd-1);
	cout<<e;
	gotoxy(boxc-1,boxb);
	cout<<d;
	gotoxy(boxc-1,boxd-1);
	cout<<f;
	for(i=boxa+1;i<boxc-1;i++)
	{
		for(j=boxb;j<boxd;j++)
		{
			gotoxy(i,j);
			cout<<s;
		}
	}
	for(i=boxa;i<boxc;i++)
	{
		for(j=boxb+1;j<boxd-1;j++)
		{
			gotoxy(i,j);
			cout<<q;
		}
	}
	for(i=boxa+1;i<boxc-1;i++)
	{
		for(j=boxb+1;j<boxd-1;j++)
		{
			gotoxy(i,j);
			cout<<" ";
		}
	}
	gotoxy(boxa+1,boxb+1);

}
void menu_main(int ch)
{
	textcolor(7);
	textbackground(0);
	int q;
	clrscr();

		gotoxy(20,1);
		cout<<"Welcome to KPercent5.0\n\n\n\n\n";
		cout<<"                         Add Records\n\n";
		cout<<"                         Display Records\n\n";
		cout<<"                         Search Records\n\n";
		cout<<"                         Set Maximum Marks\n\n";
		cout<<"                         Exit\n\n";


	gotoxy(10,2*(ch)+4);
	cout<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s;
	gotoxy(44,2*(ch)+4);
	cout<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s<<s;
	gotoxy(10,24);
	cout<<"Press UP and DOWN ARROWS to navigate. Press ENTER to select";
}
void execute_main(int ch)
{
	clrscr();
	switch(ch)
	{
		case 1:
			addrec();
			break;
		case 2:
			disprec();
			break;
		case 3:
			searchrec();
			break;
		case 5:
			fclose(file);
			exit(1);
		case 4:
			setdefault();
			break;
	}

}
void getkey()
{
	key=getch();
	if(key==0)
		key=getch();
}
void main()
{
int i=1;
clrscr();
file=fopen("E:\PROFILES.DAT","rb+");
if(file==NULL)
{
	file=fopen("E:\PROFILES.DAT","wb+");
	if(file==NULL)
	{
		puts("CANNOT OPEN THE SOURCE FILE.....!!!!!");
		getch();
		exit(2);
	}
}
else
{
	gotoxy(20,10);
	cout<<"FILE OPENED SUCCESSFULLY";
	getch();
}
recsize=sizeof(stud);


while(1)
{
	menu_main(i);
	getkey();
	switch(key)
	{
		case UARROW:
			i=i-1;
			break;
		case DARROW:
			i=i+1;
			break;
		case ENTER:
			execute_main(i);
			break;
	}
	if(i==0)
		i=5;
	if(i==6)
		i=1;
}
}

The only problem I'm seeing right now is the way you're passing filepath argument to fopen

file=fopen("E:\PROFILES.DAT","rb+");

It should be like this:

file=fopen("E:\\PROFILES.DAT","rb+");

I dont think that it is one because the previous program works fine that way... But thanks I will try it the way you tell.

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.