C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Modifying a record in a file Posted by adeydas on 26 Jan 2006 at 1:00 AM
This message was edited by adeydas at 2006-1-26 11:18:2

Hi!
I am writing a telephone directory program and I have a function called modify_record() that modifies records based on its position. The code is as follows:
void operation::modify_record()
	{
		if(pos==0)
		{
			cout<<"\nEnter the record you want to modify: ";
			cin>>pos;
		}
		a.open(file,ios::out|ios::in);
		loc=(pos-1)*sizeof(z);
		a.seekp(loc,ios::beg);
		z.addrecord();
		a.write((char*)&z,sizeof(z));
		a.seekp(0,ios::end);
		a.close();
		loc=0;
		a.close();
	} //end of modify_record() of class operation

The full program is here:
http://freepgs.com/abhishek/code/cpp/tele.CPP

Thank you.
Abhishek
http://freepgs.com/abhishek


Report
Re: Modifying a record in a file Posted by stober on 26 Jan 2006 at 4:46 AM
you forgot to ask a question

but the file needs to be opened in binary mode to prevent fstream from interpreting '\n' bytes.
a.open(file,ios::out|ios::in | ios::binary);

Report
Re: Modifying a record in a file Posted by adeydas on 26 Jan 2006 at 9:36 AM
This message was edited by adeydas at 2006-1-26 11:17:25

sorry about not asking the question :). my question was that the function modify_record() is not working. i added the ios::binary() but it didn't work. the code is:
void operation::modify_record()
	{
		if(pos==0)
		{
			cout<<"\nEnter the record you want to modify: ";
			cin>>pos;
		}
		a.open(file,ios::out|ios::in|ios::binary);
		loc=(pos-1)*sizeof(z);
		a.seekp(loc,ios::beg);
		z.addrecord();
		a.write((char*)&z,sizeof(z));
		a.seekp(0,ios::end);
		a.close();
		loc=0;
		a.close();
	} //end of modify_record() of class operation

the full program is here: http://freepgs.com/abhishek/code/cpp/tele.CPP

: you forgot to ask a question
:
: but the file needs to be opened in binary mode to prevent fstream from interpreting '\n' bytes.
:
: a.open(file,ios::out|ios::in | ios::binary);
: 

:


Thank you.
Abhishek
http://freepgs.com/abhishek



Report
Re: Modifying a record in a file Posted by stober on 26 Jan 2006 at 11:28 AM
This message was edited by stober at 2006-1-26 11:32:15

: sorry about not asking the question :). my question was that the function modify_record() is not working. i added the ios::binary() but it didn't work. the code is:

what makes you think it doesn't work? Do you have another function that re-reads the record?

And trying to write a c++ class like that is not a good idea except in the very simplest cases. That will not work if the class contains other c++ containers, such as std::string, or instances of other c++ classes, or base class that contains c++ containers, because that data is not part of the c++ class instance. You should allow each class (and base class(s)) to write out its own data.


Report
Re: Modifying a record in a file Posted by adeydas on 26 Jan 2006 at 1:18 PM
i have some code in the main() function that reads the data and prints it onscreen by the function putdata(). also i have seen the raw file. the data is not updated.
the full code is here:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<process.h>

			/* Abhishek Dey Das */
			/* Telephone Directory Program */
			/* Program is under construction and
			   contains bugs */



char file[20]={NULL}; 	//global variable for filename
int id_no; 		//global variable for ID
class add {

	public:
		char adress[50], nickname[10], email[30];
		char pn[50];
		char city[10], name[30];
		void putdata();  //function to display records
		void addrecord(); //function to enter records
	}; //end of class add


	class operation
	{
		add z;
		fstream a;
		int pos,loc;
	public:
		operation()
			{pos=0;}
		void search_byname(); //search by name
		void search_bycity(); //search by city
		void search_bypn();	//search by phone number
		void modify_record(); //modify record
	}; //end of class operation

	void add::putdata()
	{
		textcolor(8);
		clrscr();
		gotoxy(1,17);
		cout<<endl<<"		Record No : "<<id_no;
		cout<<endl<<"\n		First Name: "<<name;
		cout<<endl<<"\n		Surname   : "<<nickname;
		cout<<endl<<"\n		Address   : "<<adress;
		cout<<endl<<"\n		City      : "<<city;
		cout<<endl<<"\n		Phone no. : "<<pn;
		cout<<endl<<"\n		Email     : "<<email;
		getch();
	} //end of putdata() of class add

	void add::addrecord()
	{
		textcolor(4);
		clrscr();
		gotoxy(1,10);
		textcolor(CYAN);

		//inputting data for a record
		cprintf("ADD NEW RECORDS");
		cout<<"\n";
		cprintf("--- --- -------");
		cout<<"\n Enter the first name : ";
		gets(name);
		cout<<"\n Enter the surname    : ";
		gets(nickname);
		cout<<"\n Enter the address    : ";
		gets(adress);
		cout<<"\n Enter the City       : ";
		gets(city);
		cout<<"\n Phone number         : ";
		gets(pn);
		cout<<"\n Enter the email      : ";
		gets(email);
	} //end of addrecord() of class add

	void operation::modify_record()
	{
		//code for modify function
		fstream b;
		int k=0;
		b.open("temp.dat",ios::out|ios::binary);
		a.open(file,ios::in|ios::binary);
		while(a)
		{
		       k++;
		       if(pos!=k)
		       {
				a.read((char*)&z,sizeof(z));
				b.write((char*)&z,sizeof(z));
		       }
		       else
		       {
				z.addrecord();
				b.write((char*)&z,sizeof(z));
		       }
		}
		a.close();
		b.close();

		//Transferring the new contents
		a.open(file,ios::out|ios::binary);
		b.open("temp.dat",ios::in|ios::binary);
		while(b)
		{
			b.read((char*)&z,sizeof(z));
			a.write((char*)&z,sizeof(z));
		}
		a.close();
		b.close();
		delete "temp.dat";
	} //end of modify_record() of class operation

	void operation::search_byname()
	{
		//Code for searching by name
		clrscr();
		int ch1;
		char naam[30];
		int flag=0;
		char x;
		int rag;
		int j=0;
		id_no=0;
		pos=0;
		textcolor(CYAN);
		gotoxy(1,10);
		cprintf("\n\n SEARCH BY NAME ");
		cout<<"\n\n 1. Search by first name";
		cout<<"\n 2. Search by surname";
		cout<<"\n\n";
		cin>>ch1;
		if (ch1==1)
		{
			  clrscr();
			  char x;
			  gotoxy(1,10);
			  textcolor(CYAN);
			  cprintf("\n\n SEARCH BY NAME ");
			  cout<<"\n\n Enter name to search : ";
			  gets(naam);
			  int n=strlen(naam);
			  a.open(file,ios::in|ios::binary);
			  a.read((char*)&z,sizeof(z));
			  while(a)
			  {
			     id_no++;
			     pos++;
			     for (int j=0; j<n; j++)
			     {
				if (z.name[j]!=naam[j])
				 { flag=0;
				   break;
				 }
				 else
				 {
				   flag=1;
				   rag=10;
				 }
			   }
				 if (flag==1)
				 {
				    z.putdata();
					cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
					cin>>x;
					if('Y'==toupper(x))
					{
						modify_record();
						break;
					}
				 }
				  a.read((char*)&z,sizeof(z));
				}
				a.close();
				loc=0;

		   }
		   else
		   {
			  clrscr();
			  gotoxy(1,10);
			  textcolor(CYAN);
			  cprintf("\n\n SEARCH BY SURNAME ");
			  cout<<"\n\n Enter surname to search : ";
			  gets(naam);
			  int n=strlen(naam);
			  a.open(file,ios::in|ios::binary);
			  a.read((char*)&z,sizeof(z));
			  while(a)
			  {
			     id_no++;
			     pos++;
			     for (int j=0; j<n; j++)
			     {
				if (z.nickname[j]!=naam[j])
				 { flag=0;
				   break;
				 }
				 else
				 {
				   flag=1;
				   rag=10;
				 }
			   }
				 if (flag==1)
				 {
				    z.putdata();
				    cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
					cin>>x;
					if('Y'==toupper(x))
					{
						modify_record();
						break;
					}
				 }
				  a.read((char*)&z,sizeof(z));
				  id_no++;
				}
				a.close();
				loc=0;
		 }
	 if (flag==0&&rag!=10) //control is not going over here
			{
				cout<<flag;
				textcolor(13+128);
				cprintf("\n\n Record Not Found!");
			}
	 getch();
	};

	void operation::search_bycity()
	{
		//Code for searching by name
		clrscr();
		int ch1;
		char city[30];
		int flag=0;
		int rag;
		int j=0;
		id_no=0;
		char x;
		pos=0;
		textcolor(CYAN);
		gotoxy(1,10);
		textcolor(CYAN);
			  cprintf("\n\n SEARCH BY CITY ");
			  cout<<"\n\n Enter city to search : ";
			  gets(city);
			  int n=strlen(city);
			  a.open(file,ios::in|ios::binary);
			  a.read((char*)&z,sizeof(z));
			  while(a)
			  {
			     id_no++;
			     pos++;
			     for (int j=0; j<n; j++)
			     {
				if (z.city[j]!=city[j])
				 { flag=0;
				   break;
				 }
				 else
				 {
				   flag=1;
				   rag=10;
				 }
			   }
				 if (flag==1)
				 {
				    z.putdata();
				    cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
					cin>>x;
					if('Y'==toupper(x))
					{
						modify_record();
						break;
					}
				 }
				  a.read((char*)&z,sizeof(z));
				}
				loc=0;
				a.close();
		 if (flag==0&&rag!=10) //control is not going over here
			{
				cout<<flag;
				textcolor(13+128);
				cprintf("\n\n Record Not Found!");
			}
	 getch();
	}; //end of search_bycity() of class operation

	void operation::search_bypn()
	{
	  //Code for searching by telephone number
		clrscr();
		int ch1;
		char pn[50];
		int flag=0;
		int rag;
		int j=0;
		id_no=0;
		pos=0;
		char x;
		textcolor(CYAN);
		gotoxy(1,10);
		textcolor(CYAN);
			  cprintf("\n\n SEARCH BY TELEPHONE NUMBER ");
			  cout<<"\n\n Enter number to search : ";
			  cin>>pn;
			  int n=strlen(pn);
			  a.open(file,ios::in|ios::binary);
			  a.read((char*)&z,sizeof(z));
			  while(a)
			  {
			     id_no++;
			     pos++;
			     for (int j=0; j<n; j++)
			     {
				if (z.pn[j]!=pn[j])
				 { flag=0;
				   break;
				 }
				 else
				 {
				   flag=1;
				   rag=10;
				 }
			   }
				 if (flag==1)
				 {
				    z.putdata();
				    cout<<"\n\n\nDo you want to modify this record? [y/n]: ";
					cin>>x;
					if('Y'==toupper(x))
					{
						modify_record();
						break;
					}
				 }
				  a.read((char*)&z,sizeof(z));
				}
				loc=0;
				a.close();
		 if (flag==0&&rag!=10) //control is not going over here
			{
				cout<<flag;
				textcolor(13+128);
				cprintf("\n\n Record Not Found!");
			}
	 getch();

	};


void main()
	{
	fstream a;
	add b;
	operation o;
	int t;
	do
	{
		textcolor(2);
		clrscr();
		gotoxy(1,10);
		cout<<endl<<"		PERSONAL TELEPHONE DIRECTORY";
		cout<<endl<<"		  By Abhishek Dey Das \n\n";
		cout<<endl<<"			1:open";
		cout<<endl<<"			2:create";
		cout<<endl<<"			3:Add record";
		cout<<endl<<"			4:search";
		cout<<endl<<"			5:display";
		cout<<endl<<"			6:close File";
		cout<<endl<<"			7:exit\n";
		textcolor(RED);
		cprintf("\n HELP: ");
		cout<<"If you are a new user then create a  new file using Option 2";
		cout<<endl<<"\t\tIf you are not a new user and have just logged in, then";
		cout<<endl<<"\t\topen the desired file using Option 1 \n\n";
		cout<<endl<<"COMMAND :> ";
		cin>>t;
		switch(t)
		{
			case 1: cout<<"\n\nEnter the file name: ";
				cin>>file;
				break;
			case 2: cout<<"\n\nEnter the file name: ";
				cin>>file;
				a.open(file,ios::out|ios::trunc);
				cout<<"\nYour file has been created with the name: "<<file;
				getch();
				a.close();
				break;
			case 3: a.open(file,ios::app|ios::binary);
				char y;
				do
				{
					a.seekg(0);
					b.addrecord();
					a.write((char*)&b,sizeof(b));
					cout<<"Do you want to add one more record?[y/n]: ";
					cin>>y;
				} while('Y'==toupper(y));
				a.close();
				break;
			case 4: int x;
				textcolor(11);
				clrscr();
				gotoxy(1,10);
				cout<<"\n\n		1.by Name";
				cout<<"\n\n		2.by City";
				cout<<"\n\n		3.by phone number";
				cout<<"\n\n\n\n			-------->";
				cin>>x;
				switch(x)
				{
					case 1: o.search_byname(); break;
					case 2: o.search_bycity(); break;
					case 3: o.search_bypn(); break;
				}
				break;
			case 5: id_no=0;
				a.open(file,ios::in,ios::binary);
				a.seekg(0);
				a.read((char*)&b,sizeof(b));
				while(a.eof()==0)
				{
					id_no++;
					b.putdata();
					a.read((char*)&b,sizeof(b));
				}
				a.close();
				break;
			case 6: *file=NULL; break;
			case 7: exit(0);
			default: cout<<"\n\n Invalid Entry!"; break;
		}
		}while(t<9);
	}

i understand the consequences of using classes like that but Turbo C++ 3.0 is too ancient for that.


: This message was edited by stober at 2006-1-26 11:32:15

: : sorry about not asking the question :). my question was that the function modify_record() is not working. i added the ios::binary() but it didn't work. the code is:
:
: what makes you think it doesn't work? Do you have another function that re-reads the record?
:
: And trying to write a c++ class like that is not a good idea except in the very simplest cases. That will not work if the class contains other c++ containers, such as std::string, or instances of other c++ classes, or base class that contains c++ containers, because that data is not part of the c++ class instance. You should allow each class (and base class(s)) to write out its own data.
:
:
:


Thank you.
Abhishek
http://freepgs.com/abhishek

Report
Re: Modifying a record in a file Posted by stober on 26 Jan 2006 at 7:02 PM
		delete "temp.dat";


You cannot delete string literals. This will probably cause your program to crash.
Report
Re: Modifying a record in a file Posted by stober on 26 Jan 2006 at 7:44 PM
The main problem is that case 5: display, open had incorrect parameters
Replace the comma with or | operator. There should be only two parameters, not three.

There are other errors -- I'm supprised your compiler even produced an executable file without you correcting the errors.

Report
Re: Modifying a record in a file Posted by adeydas on 26 Jan 2006 at 10:59 PM
the comma operator if replaced with | gives an 'illegal pointer operation' error. its not that, the code in modify_record() dosen't work. i think its a logical error.
the function is:
void operation::modify_record()
	{
		//code for modify function
		fstream b;
		int k=0;
		b.open("temp.dat",ios::out|ios::binary);
		a.open(file,ios::in|ios::binary);
		while(a)
		{
		       k++;
		       if(pos!=k)
		       {
				a.read((char*)&z,sizeof(z));
				b.write((char*)&z,sizeof(z));
		       }
		       else
		       {
				z.addrecord();
				b.write((char*)&z,sizeof(z));
		       }
		}
		a.close();
		b.close();

		//Transferring the new contents
		a.open(file,ios::out|ios::binary);
		b.open("temp.dat",ios::in|ios::binary);
		while(b)
		{
			b.read((char*)&z,sizeof(z));
			a.write((char*)&z,sizeof(z));
		}
		a.close();
		b.close();
		delete "temp.dat";
	} //end of modify_record() of class operation

the full code is here: http://freepgs.com/abhishek/code/cpp/TELE.CPP


: The main problem is that case 5: display, open had incorrect parameters
: Replace the comma with or | operator. There should be only two parameters, not three.
:
: There are other errors -- I'm supprised your compiler even produced an executable file without you correcting the errors.
:
:


Thank you.
Abhishek
http://freepgs.com/abhishek

Report
Re: Modifying a record in a file Posted by stober on 27 Jan 2006 at 5:52 AM
: the comma operator if replaced with | gives an 'illegal pointer operation' error. its not that, the code in modify_record() dosen't work. i think its a logical error.

then you didn't do it right -- look at all the other lines where you call open() -- not one of them have a comma, yet they all compile and work correctly. open() function only takes two parameters, not three.
Report
Re: Modifying a record in a file Posted by adeydas on 27 Jan 2006 at 6:54 AM
no i mean comma after the filename. then it gives the error. i other cases i have tried both with comma and | and both of them works.


: : the comma operator if replaced with | gives an 'illegal pointer operation' error. its not that, the code in modify_record() dosen't work. i think its a logical error.
:
: then you didn't do it right -- look at all the other lines where you call open() -- not one of them have a comma, yet they all compile and work correctly. open() function only takes two parameters, not three.
:


Thank you.
Abhishek
http://freepgs.com/abhishek

Report
Re: Modifying a record in a file Posted by stober on 27 Jan 2006 at 11:10 AM
This message was edited by stober at 2006-1-27 11:12:45

: no i mean comma after the filename. then it gives the error. i other cases i have tried both with comma and | and both of them works.
:

a.open(file,ios::in,ios::binary);


This is from your link --
LOOK AT THAT LINE REALLLLLLLY HARD! how many commas do you count? (I assume you can cound to 2). Replace the second comma with | or operator.


Report
Re: Modifying a record in a file Posted by adeydas on 27 Jan 2006 at 11:22 AM
i tried replacing the second comma with | but its not working. infact it was | at first and i put comma later just to check. i don't think its a syntax error or the compiler would have warned me. tell me if you can find some mistake in the logic.


: This message was edited by stober at 2006-1-27 11:12:45

: : no i mean comma after the filename. then it gives the error. i other cases i have tried both with comma and | and both of them works.
: :
:
:
: a.open(file,ios::in,ios::binary);
: 

:
: This is from your link --
: LOOK AT THAT LINE REALLLLLLLY HARD! how many commas do you count? (I assume you can cound to 2). Replace the second comma with | or operator.
:
:
:


Thank you.
Abhishek
http://freepgs.com/abhishek

Report
Re: Modifying a record in a file Posted by Donotalo on 27 Jan 2006 at 12:19 PM
from ur full code: http://freepgs.com/abhishek/code/cpp/TELE.CPP, i've got the code and replaced all files' open functions' second comma (,) to the OR (|). made necessary removal so that my dev c++ compiler can compile ur turbo C++ code (for example, i removed all clrscr(), gotoxy() etc functions). it compiles well though gives some warning. as stober said, replace ALL second commas inside each file's open function.

now compile and if u get any error, post the error along with the portion of the code where the error will occur.

also, dont ignore warnings.


~Donotalo()

Report
Re: Modifying a record in a file Posted by adeydas on 27 Jan 2006 at 11:20 PM
i have changed that. now a strange thing is happening. some of the characters is properly showing up when the putdata() function is run but in place of others, some strange symbols are showing up. i think it is because the compiler is not opening it in binary mode.
there are three warnings displayed now. all the three are same, that of being 'j' not used in the functions search_byname(), search_bycity() and search_bypn(). no errors are shown. my compiler is turbo c++ 3.0.


: from ur full code: http://freepgs.com/abhishek/code/cpp/TELE.CPP, i've got the code and replaced all files' open functions' second comma (,) to the OR (|). made necessary removal so that my dev c++ compiler can compile ur turbo C++ code (for example, i removed all clrscr(), gotoxy() etc functions). it compiles well though gives some warning. as stober said, replace ALL second commas inside each file's open function.
:
: now compile and if u get any error, post the error along with the portion of the code where the error will occur.
:
: also, dont ignore warnings.
:

:
~Donotalo()
:
:


Thank you.
Abhishek
http://freepgs.com/abhishek

Report
Re: Modifying a record in a file Posted by Donotalo on 28 Jan 2006 at 2:13 AM
: i have changed that. now a strange thing is happening. some of the characters is properly showing up when the putdata() function is run but in place of others, some strange symbols are showing up. i think it is because the compiler is not opening it in binary mode.
: there are three warnings displayed now. all the three are same, that of being 'j' not used in the functions search_byname(), search_bycity() and search_bypn(). no errors are shown. my compiler is turbo c++ 3.0.
:
:

if u dont use j, remove the declaration of j. there shud be no problem.

i was also faced weird troubles in turbo c++ 3. cudn't find any proper solution. let me suggest u something.

instead of using fstream, where u need writing to file, use ofstream and where u need reading a file, use ifstream. see what happens.

if u want to use fstream, which is by default (if i am not wrong) handles files capable of both reading and writing. so opening a file with ios::in or ios::out is redundant. try to remove these ios::in and ios::out.

try any (or both, not simultaneously) of the above. remove every files except ur source file (that is, other *.obj etc files created by the compiler), and recompile. see what happens.


~Donotalo()

Report
Re: Modifying a record in a file Posted by adeydas on 29 Jan 2006 at 8:58 AM
i have done as you said. now the modify_record() is changing the record set but three extra record sets are added after that containing the previous values of that record set. also the values of email and nickname variables are shown together when putdata() is run. can you please help me out. the new code is here: http://code.deydas.net/TELE_ORI.TXT

also can you please suggest some new compiler that will compile this code (no variations are allowed by my college though).


: : i have changed that. now a strange thing is happening. some of the characters is properly showing up when the putdata() function is run but in place of others, some strange symbols are showing up. i think it is because the compiler is not opening it in binary mode.
: : there are three warnings displayed now. all the three are same, that of being 'j' not used in the functions search_byname(), search_bycity() and search_bypn(). no errors are shown. my compiler is turbo c++ 3.0.
: :
: :
:
: if u dont use j, remove the declaration of j. there shud be no problem.
:
: i was also faced weird troubles in turbo c++ 3. cudn't find any proper solution. let me suggest u something.
:
: instead of using fstream, where u need writing to file, use ofstream and where u need reading a file, use ifstream. see what happens.
:
: if u want to use fstream, which is by default (if i am not wrong) handles files capable of both reading and writing. so opening a file with ios::in or ios::out is redundant. try to remove these ios::in and ios::out.
:
: try any (or both, not simultaneously) of the above. remove every files except ur source file (that is, other *.obj etc files created by the compiler), and recompile. see what happens.
:

:
~Donotalo()
:
:


Thank you.
Abhishek
http://deydas.net

Report
Re: Modifying a record in a file Posted by Donotalo on 29 Jan 2006 at 9:20 AM
: i have done as you said. now the modify_record() is changing the record set but three extra record sets are added after that containing the previous values of that record set. also the values of email and nickname variables are shown together when putdata() is run. can you please help me out. the new code is here: http://code.deydas.net/TELE_ORI.TXT

....
a.open(file,ios::in|ios::binary);
while(a)
{
k++;
if(pos==k)
....

turbo c++ is too old, so dont take the chance of detecting eof like the one u r using. again, i'm not sure whether this is really the problem or not, but try !a.eof() as the condition. if that doesnt work, thoroughly check ur code of file operation. if u cannot detect what is the wrong going on, try simplify the file operation and rewrite the code.

:
: also can you please suggest some new compiler that will compile this code (no variations are allowed by my college though).

sorry, no idea. other modern c++ compilers will not allow u to work in dos like turbo c++.


~Donotalo()

Report
Re: Modifying a record in a file Posted by adeydas on 30 Jan 2006 at 1:31 AM
thanks a lot for your advice. you are a life saver. i have implemented the things you told and have re-written the code. now all of functions are working. however, a strange thing is happening. while search_byname() function is working, if i put the same code (since the searching logic for all of them is basically the same) for search_bycity(), search_bysurname() and search_bypn ; its not working. its really strange. can you please suggest a way out of this one.
also i'd like to make a delete function which deletes a specified record. can you please provide some ideas as to how i should do it?
the new code is here: http://code.deydas.net/TELE_MOD.TXT and the old one still remains here: http://code.deydas.net/TELE_ORI.TXT


: : i have done as you said. now the modify_record() is changing the record set but three extra record sets are added after that containing the previous values of that record set. also the values of email and nickname variables are shown together when putdata() is run. can you please help me out. the new code is here: http://code.deydas.net/TELE_ORI.TXT
:
:
: ....
: a.open(file,ios::in|ios::binary);
: while(a)
: {
: k++;
: if(pos==k)
: ....
: 

: turbo c++ is too old, so dont take the chance of detecting eof like the one u r using. again, i'm not sure whether this is really the problem or not, but try !a.eof() as the condition. if that doesnt work, thoroughly check ur code of file operation. if u cannot detect what is the wrong going on, try simplify the file operation and rewrite the code.
:

: :
: : also can you please suggest some new compiler that will compile this code (no variations are allowed by my college though).
:
: sorry, no idea. other modern c++ compilers will not allow u to work in dos like turbo c++.
:

:
~Donotalo()
:
:


Thank you.
Abhishek
http://deydas.net

Report
Re: Modifying a record in a file Posted by Donotalo on 30 Jan 2006 at 4:32 AM
: thanks a lot for your advice. you are a life saver. i have implemented the things you told and have re-written the code. now all of functions are working. however, a strange thing is happening. while search_byname() function is working, if i put the same code (since the searching logic for all of them is basically the same) for search_bycity(), search_bysurname() and search_bypn ; its not working. its really strange. can you please suggest a way out of this one.
: also i'd like to make a delete function which deletes a specified record. can you please provide some ideas as to how i should do it?
: the new code is here: http://code.deydas.net/TELE_MOD.TXT and the old one still remains here: http://code.deydas.net/TELE_ORI.TXT
:

they all shud be work in the same way. however, probably u dont know strcmp() function. it takes two parameters, both of them are chararacter array. if the both of the character arrays contain same string, it returns 0, otherwise, a non-zero value is returns (probably 1 or -1 depending on the lexicographical analysis). so u can redefine ur search_byname() function easily. try this on other search functions.
void operation::search_byname()
{
    int flag=0;
    char naam[30];
    char y;
    clrscr();
    gotoxy(1,10);
    textcolor(2);
    cout<<"\n Enter name to search: ";
    gets(naam);     <-- try to use fgets().
                             gets() dont check array boundary.
                             it can be easily overrun.
    int n=strlen(naam);    <-- no need, as u can see
    ifstream p;
    p.open(file,ios::in);
    p.seekg(0);     <-- completely unnecessary.
    p.read((char*)&z,sizeof(z));
    while(!p.eof())
    {
/*      for(int j=0;((z.name[j]!=' ')&&(z.name[j]!='\o')&&(j<n));j++)
        {
            if (z.name[j]!=naam[j])
           {
               flag=0;
           }
           else
           {
               flag=1;
           }
        }*/
        if (strcmp(z.name, naam))    <-- nonzero implies unequal
            flag = 0;
        else flag = 1;
        ...
    }

for delete a particular record:

*** read all data from file
*** search for the data to be deleted (by user input if u prefer)
*** delete the data from the data structure (if u use array, then the array is ur data structure)
*** save all data from ur data structure to the file

i'm bad at understanding others code, so i didnt go through ur code.


~Donotalo()

Report
Re: Modifying a record in a file Posted by adeydas on 30 Jan 2006 at 1:24 PM
now all the functions except the delete_record() are working. for deleting records, i have taken a structure with all the variables required to take the data from the file. then i have made an array and have inputted all the values from the file into the structure array except the one that is to be deleted. the compiler is giving an error of 'LValue Required' in the shown lines:
struct del {
char adress[50], nickname[10], email[30];
char pn[50];
char city[10], name[30]; int pos;
};
//====================================================

void operation::delete_record()
{
struct del d[350];
int k=1;
int loc=z.pos;
ifstream i;
i.open(file,ios::in);
i.read((char*)&z,sizeof(z));
while(!i.eof())
{
if (loc!=k)
{
d[k].pos=z.pos;  
d[k].name=z.name;  <== here the LValue required error is given
d[k].nickname=z.nickname;  <== here the LValue required error is given
d[k].adress=z.adress;  <== here the LValue required error is given
d[k].email=z.email;  <== here the LValue required error is given
d[k].city=z.city;  <== here the LValue required error is given
d[k].pn=z.pn; <== here the LValue required error is given
}
i.read((char*)&z,sizeof(z));
k++;
}
i.close();
//--------------------------------------------------
ofstream j;
j.open(file,ios::out);
int n=1;
while(n!=k)
{
j.write((char*)&d[n],sizeof(d[n]));
n++;
}
j.close();
};
//====================================================

the whole code is here: http://code.deydas.net/TELE_MOD.TXT



: : thanks a lot for your advice. you are a life saver. i have implemented the things you told and have re-written the code. now all of functions are working. however, a strange thing is happening. while search_byname() function is working, if i put the same code (since the searching logic for all of them is basically the same) for search_bycity(), search_bysurname() and search_bypn ; its not working. its really strange. can you please suggest a way out of this one.
: : also i'd like to make a delete function which deletes a specified record. can you please provide some ideas as to how i should do it?
: : the new code is here: http://code.deydas.net/TELE_MOD.TXT and the old one still remains here: http://code.deydas.net/TELE_ORI.TXT
: :
:
: they all shud be work in the same way. however, probably u dont know strcmp() function. it takes two parameters, both of them are chararacter array. if the both of the character arrays contain same string, it returns 0, otherwise, a non-zero value is returns (probably 1 or -1 depending on the lexicographical analysis). so u can redefine ur search_byname() function easily. try this on other search functions.
:
: void operation::search_byname()
: {
:     int flag=0;
:     char naam[30];
:     char y;
:     clrscr();
:     gotoxy(1,10);
:     textcolor(2);
:     cout<<"\n Enter name to search: ";
:     gets(naam);     <-- try to use fgets().
:                              gets() dont check array boundary.
:                              it can be easily overrun.
:     int n=strlen(naam);    <-- no need, as u can see
:     ifstream p;
:     p.open(file,ios::in);
:     p.seekg(0);     <-- completely unnecessary.
:     p.read((char*)&z,sizeof(z));
:     while(!p.eof())
:     {
: /*      for(int j=0;((z.name[j]!=' ')&&(z.name[j]!='\o')&&(j<n));j++)
:         {
:             if (z.name[j]!=naam[j])
:            {
:                flag=0;
:            }
:            else
:            {
:                flag=1;
:            }
:         }*/
:         if (strcmp(z.name, naam))    <-- nonzero implies unequal
:             flag = 0;
:         else flag = 1;
:         ...
:     }
: 

: for delete a particular record:
:
: *** read all data from file
: *** search for the data to be deleted (by user input if u prefer)
: *** delete the data from the data structure (if u use array, then the array is ur data structure)
: *** save all data from ur data structure to the file
:
: i'm bad at understanding others code, so i didnt go through ur code.
:

:
~Donotalo()
:
:


Thank you.
Abhishek
http://deydas.net

Report
Re: Modifying a record in a file Posted by Donotalo on 30 Jan 2006 at 6:25 PM
to copy one c string to another, u have to use strcpy() function. it has two parameters - both of them are char* though the second one is constant. its job is to copy the character string of the second parameter to the first one. for example,
char *a = "Some text", b[10];
strcpy(b, a);

the above will copy "Some text" to b. change ur code according to it.


~Donotalo()

Report
Re: Modifying a record in a file Posted by adeydas on 30 Jan 2006 at 11:52 PM
works like a charm now. thanks a lot for your help, you rock!!!
i didn't use structures to make an array and store the values. that way there would have been a limitation to the number of elements in the array. i have, instead, written the all the data on a temporary file except the one to be deleted and has transferred it back to the required file after that.
below is the code:
void operation::delete_record()
{
int h=z.pos;
ifstream j;
j.open(file,ios::in);
ofstream temp;
temp.open("temp.abc",ios::out);
j.read((char*)&z,sizeof(z));
while(!j.eof())
{
if (h!=z.pos)
{
temp.write((char*)&z,sizeof(z));
}
j.read((char*)&z,sizeof(z));
}
j.close();
temp.close();
//-----------------------------------------------------

ifstream io;
io.open("temp.abc",ios::in);
ofstream ko;
ko.open(file,ios::out);
io.read((char*)&z,sizeof(z));
while(!io.eof())
{
ko.write((char*)&z,sizeof(z));
io.read((char*)&z,sizeof(z));
}
io.close();
ko.close();
}; //end of delete_record() of class operation

the full code is here: http://code.deydas.net/TELE_MOD.TXT


: to copy one c string to another, u have to use strcpy() function. it has two parameters - both of them are char* though the second one is constant. its job is to copy the character string of the second parameter to the first one. for example,
: char *a = "Some text", b[10];
: strcpy(b, a);
: 

: the above will copy "Some text" to b. change ur code according to it.
:

:
~Donotalo()
:
:


Thank you.
Abhishek
http://deydas.net

1 2  Next



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.