GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2007, 07:00
rajeev nair rajeev nair is offline
Junior Member
 
Join Date: Aug 2006
Posts: 97
rajeev nair is an unknown quantity at this point

Problem inserting (appending) in the middle of text file


Hi,

I have a C++ coding which prompts user to enter a username and password which is then compared with username,password bank account no and bank balancestored in a text file (new.txt).If the username and passowrd is correct then the user account no and balance are displayed.

After which I replace the balance in the text file with a string str3="51907".I use
the function seekg to point to the location from where i need to write and then clear the previous value (balance) by writing blankspace (" ").After which I write the string str3 again using seekg() function to point to the exact location.


Now the problem is sometimes if the string (str3) is larger or longer in length when I write this value to the text it overwrites on to the next character (next line).In other words I am not able to append from middle of text without erasing the next character in text file (if string exceeds size).



My code:

[
CPP / C++ / C Code:

 class bank
{


    public:

    char username[100], password[16];

            void prompt()
           {
		system("cls");
		cout<<"\n ENTER YOUR USERNAME :";
	        cin>>username;

		cout<<"\n ENTER YOUR PASSWORD: ";
		cin>>password;
           }

	    void check_pass(char[],char[]);

};


void bank::check_pass(char a[100],char b[16])
{

    char str[100] , str1[16], str3[100]= "51097" ;
    float i;
     long pos; /* stores current file position */

          /* open text file in both read/write mode */     

         fstream b_file("new.txt",ios_base::in|ios_base::out);

          /*checks for username till end of file */
	     while( (strcmp(a,str) != 0)&&(! b_file.eof() ) )
	    {
		b_file>>str;
	       b_file>>str1;
            }

              /* checks username and password */
              if( (strcmp(a,str) == 0) && (strcmp(b,str1) == 0) )

             {
                b_file>>str1;
                cout<<"\n USER :  " <<a;
                cout<<"\n ACCOUNT NO: "<<str1;
                b_file>>str1;

               cout<<"\n BALANCE : "<<str1<<" Rs";


                 pos = b_file.tellg();  /* gets position */
                
                 /* made to point to exact location */

                b_file.seekg( pos- strlen(str1) ); 


                    for(int i = 0; i <= strlen(str1) ; i++)

                   {
                      b_file<<"  "; /* clears the present balance (in file) */
	            }

                    b_file.seekg( pos - strlen(str1) );

                    b_file<<str3; /*writes the string to exact location */
              }

             else
             {

              system("cls");
              cout<<"\n\n CHECK YOUR USER/PASSWORD IS CORRECT \n\n";
             }

             b_file.close();

}




int main()
{

	 bank b1, b2;

	  char  ex;

           for(;;)

          {

	    b1.prompt();

            b2.check_pass( b1.username, b1.password );

            cout<<"\n\n\n Enter 'e' for exit :";

            cin>>ex;

            if(ex=='e') exit(1);

         }

}







new.txt (file) looks like this:

Quote:
username password Account no bank balance

nick rks 566777 777777
ram rksn124 777675 786889
raj fgh 656566 77868




If I happen to insert/append this way the string (str3) sometimes overwrites to next immediate character in file.In other words the next character gets erased.Any ideas on avoiding overwriting next character and simply append?...

Help appreciated

Regards,
Rajeev
  #2  
Old 12-Apr-2007, 08:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,712
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem inserting (appending) in the middle of text file


Quote:
Originally Posted by rajeev nair
If I happen to insert/append this way the string (str3) sometimes overwrites to next immediate character in file.
If the number of characters that you want to write into a particular place in a file is greater than the number of characters that you want to overwrite, you simply can't do it without overwriting subsequent characters. Period. Full stop.

The way to do it is:

1. Open Original File.

2. Open a brand New File.

3. Read things from the Old File and write into the New File up to the point right before the place for your replacement text.

4. Read and discard the items to be replaced from the Old File. Write the new data to the New File.

5. If there are additional new items to be written, then repeat steps 3 and 4 until there are no more new items.

6. Read everything else from the Old File from the Old file and write them into the New File until the Old File has nothing else to read.

7. Rename the Old File to some name that indicates it is a backup file. (If you don't want to keep backup file, then just delete it.

8. Rename the New File to whatever name the Original File had.


Regards,

Dave
  #3  
Old 12-Apr-2007, 13:35
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: Problem inserting (appending) in the middle of text file


If you are in control of the format of the text file, make each item a fixed width and pad with blanks. That way you can overwrite in the middle of the file without messing up other lines.
  #4  
Old 17-Apr-2007, 00:55
rajeev nair rajeev nair is offline
Junior Member
 
Join Date: Aug 2006
Posts: 97
rajeev nair is an unknown quantity at this point

Re: Problem inserting (appending) in the middle of text file


Hi,

Thanks for all those responses.I think I have found a possible solution If I happen to use "\n" at the end of string (by string concatenation) which I am inserting in the middle of a text file I was able to avoid overwriting of the next immediate string as this string is shifted to a new line.

My idea is to build a database (for a bank) which keeps on updating the user's financial transactions (credits/debits).I was able to achieve what I needed by using "\n" new line character symbol (only if the string exceeded size) while writing into the file thereby avoid overwriting of next character (if string length exceeded previous length) in the text file.


Thank You,

Regards,
Rajeev Nair.
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
CD Burner Help - Power Calibration Error.... JonBoy420 Computer Hardware Forum 110 05-Feb-2008 18:34
Help! Problems encountered while burning CD's RayDarkness Computer Software Forum - Windows 1 18-Nov-2006 04:54
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.