Update File

Hi ,
I have a text file with say the following contents

abcd 123
def 456
ghi 678

I want to be able to read each line and then update at the end of each line of the file. My updated file should have the following contents.

abcd 123 fruits
def 456 flowers
ghi 678 leaves

How do i accomplish this ?
I am looking for actual code.


rashmi_beAsked:
Who is Participating?
 
smpoojaryCommented:
rashmi be,

    what Axter says is right. Do onething update the points.
Answer for your question:
If you are using binary mode file then it is easy to implement. I think you want text mode file (for see the content inside the file). Modify following code to your purpose

//Open file in Text mode
int Data::Initialize()
{
      fptrData.open("in.dat",ios::in | ios::out);
      if(!fptrData.is_open())
      {
            err_status = ERR_UNOPEN;
            return ERR_UNOPEN;
      }
      return 0;
}

//Create a file
void Data::CreateDataBase()
{
      if(err_status != ERR_UNOPEN)
      {
            memset(caBuff,0,100);
            strcpy(caBuff,"abcd 123");
            fptrData.write((const char *)caBuff,100);
            fptrData.flush();
            memset(caBuff,0,100);
            strcpy(caBuff,"def 456");
            fptrData.write((const char *)caBuff,100);
            fptrData.flush();
            memset(caBuff,0,100);
            strcpy(caBuff,"ghi 678");
            fptrData.write((const char *)caBuff,100);
            fptrData.flush();
      }
}

//Update file
void Data::UpdateDataBase()
{
      if(err_status != ERR_UNOPEN)
      {
            fptrData.clear();
            i=0;
            while(i<nTotal)
            {
                  fptrData.seekg(i*100,ios::beg);
                  memset(caBuff,0,100);
                  fptrData.read(caBuff,100);
                  strcat(caBuff," fruits");
                  fptrData.seekp(i*100,ios::beg);
                  fptrData.write((const char*)caBuff,100);
                  fptrData.flush();
                  ++i;
            }
      }
}

//Display the contents
void Data::Display()
{
      if(err_status == ERR_UNOPEN) return;
      fptrData.clear();

      i=0;
      while(i<nTotal)
      {
            fptrData.seekg(i*100,ios::beg);
            fptrData.read(caBuff,100);
            cout << caBuff << endl;
            ++i;
      }
}

-Mahesh
0
 
AxterCommented:
Hi rashmi_be,
You're asking a lot for 20 points.
And to make matters worse, you have a bad grading history and a history of abandoning your questions.

Please start awarding appropriate points and grade to your questions.

David Maisonave :-)
Cheers!
0
 
AxterCommented:
FYI: To other experts.

This member's grading history is very misleading.
If you look at each question posted, you'll see that the only questions that have received an A grade are questions that have been force accepted by a moderator.
Unfortunately, that is the case with the majority of the questions posted by this member.

The following is the grading history using only questions that have been accepeted by this member:
C B B C C B

All of the above grades have been given to questions assigned 20 to 75 points.

This is a very poor grading history.

rashmi_be,
Please read the grading guidelines and grade appropriately and assign appropriate points.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
rashmi_beAuthor Commented:
Sure i will make a note of it.
0
 
rashmi_beAuthor Commented:
Thanks for the info Mahesh.I will test and let you know..

0
 
rashmi_beAuthor Commented:
Mahesh ,Thanks for your response. I found another solution also. I created a temp file. Copied the contents of the old file to a .bak file. Then open the file for reading . Then created the new file and then updated the new file ...
0
 
smpoojaryCommented:
I thought you want to update in current file. YOu can also do, delete and rename the new file also.

But problem arises if your file size is very large to handle. Like database file. Like if your file contains more than 10000 lines. It takes time to copy the file. For learning purpose your method is okay (But not good idea).
With warm regards
-Mahesh
0
 
smpoojaryCommented:
Think about efficiency and optimization of the code.
-Mahesh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.