helllo everyone,

i am writing a C++ program using ifstream. There will be two text file_ one with balance of the customer's account, and the other with the customer's purchase. Just like this.

100 1000.00
101 2000.00
102 3000.00
103 4000.00

the above is the balance.txt with customer nos (100 to 103) and their balances ( 1000 to 4000)


and the purchase.txt will look like below.

100 0000.00
101 0000.00
102 0000.00
103 0000.00

int main ()
{int key; 
double balance, purchse;


cout<<" 4 Customer Account Types \n-*-*-*-*-*-*-*-*-\n 100, 101, 102, 103\n " ;
do{
	
	cout<<" \nEnter one of the account numbers above : ";
    cin>>key;


if (((key-100)<=3)  && ((key-100) >= 0))
byte = (((key%100) * 18 ) + 10) ;  // this is for seekg(byte) function to seek the value of balance from  balance.txt 


else {cout<<"Invalid Entry, Please Try again! \n"; } 
}while (!((key-100)<=3  && (key-100) >= 0));

inFile.open("balance.txt");   // balance.txt is opened and seek the balance for coresponding customer
  inFile.seekg(byte);
  inFile >> balance;
  cout<< "The balance is "<<balance<<endl;
  inFile.close();  


cout<< "Enter purchase value :";   // this is asking the user to enter the purchase amount
	cin>>purchase;

	if (purchase<balance || purchase == balance)   // checking whether the balance limit exceeds
	{
		(?????????)// *******  if the purchase is approved i want to seek the byte corresponds to each customer and overwrite the purchase in purchse.txt 

if i entered 1500 for purchase amount for account 101then it will show like this in purchase.txt and the others' will be still 0000.00

100       0000.00
101       1500.00
102       0000.00
103       0000.00

The same problem for the balance.txt file  I want to sustract the purchase value from original balance value and Put back the new balance value in balance.txt file only for coresponding customer account  without touching other account nos.


	    cout<< "Purchase Approved"<<endl ;
	}

else cout<< "!!!!!!  Purchase Exceeds the limit !!!!!"<<endl;
}



}

My Question is in red inside the program.

i don't know how to overwrite only one value and the other values remains unchanged.
One of my fascillitator said that I can use array to store all the values inside the text file and change whichever I want to change. Then put them back to txt file.


Looking forward to your solution.
If my question is not clear to you, feel free to ask me back.

Thank you in advance .;)
Uthnim

>>if (purchase<balance || purchase == balance)
change that to this: if( purchase <= balance) seekg() moves the file pointer xxx number of bytes. To accomplish this you have to calculate the offset from the beginning of the file, or from the current location of the file pointer. The start of the second account -- 101 -- is 11 + 2 for the line terminator "\r\n" pair in MS-Windows. When using *nix it is only "\n" and MAC its "\r". All those differences is why we don't normally use seek methods on text files even when the text files have fixed-length records. Now to advance to the beginning of the money field just add 4 to account for 3 digit account number plus one space.

A safer way to do it that is portable to all operating systems is to rewrite the entire file and replace only the line that changes.

the line terminator "\r\n" pair in MS-Windows. When using *nix it is only "\n" and MAC its "\r".

"\r" is for enter key and "\n" is for line feed...correct me if i am wrong..
and if not please tell me what is a line feed?? why it is used with "\r"??

i also used it in assembly language the code was 0dh,0ah...someone asked me what does each byte mean? please explain!!

A safer way to do it that is portable to all operating systems is to rewrite the entire file and replace only the line that changes.

wouldn't that be an in-efficient way??

"\r" is for enter key and "\n" is for line feed...correct me if i am wrong..
and if not please tell me what is a line feed?? why it is used with "\r"??

\n Newline. Position the screen cursor to the beginning of the next line.
\r Carriage return, Position the screen cursor to the beginning of the current line; do not advance to the next line.

You can't seek() on a text file unless you use the result of a previous tell(). Because of all the line translation which happens, you can't reliably calculate the start position of any given line (except the first line obviously).

Also, updating a text file one line at a time is only remotely possible when the new string is <= in length to the string it is replacing.

Rewriting the whole file, with changes, may seem a long way to go, but the code is simple, and the result is reliably predictable.

On reflection, AD pretty much summed this up anyway, oh well.

\n Newline. Position the screen cursor to the beginning of the next line.
\r Carriage return, Position the screen cursor to the beginning of the current line; do not advance to the next line.

which one of them is equivalent to pressing enter at the console...means if i wanna check when enter is pressed...which one of them should i compare with...

i also used it in assembly language the code was 0dh,0ah...someone asked me what does each byte mean? please explain!!

0Dh is carriage return. Return to the beginning of current line.
0Ah is a line feed. Drop down to the next line.

In assembly you don't have the luxury of '\n' which does both.

wouldn't that be an in-efficient way??

Somewhat. But it's safe and easy. Trying to change the file on the fly is dangerous, and if you have to add just one extra character, it cannot be done without screwing up the file.

which one of them is equivalent to pressing enter at the console...means if i wanna check when enter is pressed...which one of them should i compare with...

13 is what you should compare with.

Try this, you will see the different:

std::cout << "Hello World\rWhy Why";

and

std::cout << "Hello World\nWhy Why";

invisal
In C and C++ the single character '\n' is considered a 'newline' because it is automatically translated to the appropriate sequence when handling text files.

On DOS/Windows, the '\n' is translated to/from '\r\n'.
On a Mac, the '\n' is translated to/from '\r'.
On Unix, no translation is necessary.

The actual ASCII (and, hence, ISO/IEC 8859) meaning of '\r' is carriage return and the meaning of '\n' is line-feed.

Thus, if you were to configure your console output to binary mode, your test would work properly:

What follows is line feed:
                          _
[U]W[/U]hat follows is carriage return:

Hope this helps.

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.