Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Update File

Posted on 2005-04-29
8
Medium Priority
?
187 Views
Last Modified: 2010-04-01
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.


0
Comment
Question by:rashmi_be
  • 3
  • 3
  • 2
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13897843
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
 
LVL 30

Expert Comment

by:Axter
ID: 13897922
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
 

Author Comment

by:rashmi_be
ID: 13897947
Sure i will make a note of it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
smpoojary earned 120 total points
ID: 13907368
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
 

Author Comment

by:rashmi_be
ID: 13908612
Thanks for the info Mahesh.I will test and let you know..

0
 

Author Comment

by:rashmi_be
ID: 13910202
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
 
LVL 4

Expert Comment

by:smpoojary
ID: 13915073
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
 
LVL 4

Expert Comment

by:smpoojary
ID: 13915077
Think about efficiency and optimization of the code.
-Mahesh
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question