6 Replies - 793 Views - Last Post: 13 May 2015 - 04:30 AM Rate Topic: -----

#1 nomads77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-October 14

How to edit particular data in the file ?

Posted 03 May 2015 - 06:40 AM

I have a file "PlayerHighScores.txt" which appends and write Name and scores in the file.
The problem that I'm having is I'm not able to replace/modify a particular score of a player. For instance:

For the time being "PlayerHighScores.txt" looks like:

Name Score
Adam | 500|
Rick |491|
Tom |488|

Now when Adam plays the game again it'll just simply append to the end, which will look like this:
Adam | 500|
Rick |491|
Tom |488|
Adam |1000|

My Question is can I Modify the data straight away by replacing the previous high score ? Like this :
Adam | 1000|
Rick |491|
Tom |488|

Is This A Good Question/Topic? 0
  • +

Replies To: How to edit particular data in the file ?

#2 hbn.developer   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 38
  • Joined: 26-April 15

Re: How to edit particular data in the file ?

Posted 03 May 2015 - 06:50 AM

1) handling data files C++
2) handling data files C++
Was This Post Helpful? 1
  • +
  • -

#3 nomads77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-October 14

Re: How to edit particular data in the file ?

Posted 04 May 2015 - 02:40 PM

View Posthbn.developer, on 03 May 2015 - 06:50 AM, said:



Thanks for the help.
I was successful to rename in MAC OS but I tried to do it in windows, I was denied permission any idea to slove this problem.

#include <fstream>
#include <iostream>
#include <cstdlib>
#include "File.h"
#include "Fstream.h"
using namespace std;


void Fstream::copy(string name,string newscore)  //<----GETS NAME AND SCORE {name,new score}
{
    int result;
    bool found =false;
    bool replace =false;
    char oldname[] ="Copy.txt";
    char newname[] ="Data.txt";
    
   
    //string newscore="2000000";
    ifstream fin("Data.txt");
    
    if(fin.good()){  // goes inside if file already exists
        ofstream fout( "Copy.txt", ios::app);
    while(getline(fin,line,' '))
    {

        if(line==name)
        {
            replace=true;
            found=true;
            fout<<line;            //break;
        }
        else if(replace){
            //check for line < newscore if yes then
            int oldScore=stoi(line); // string to integer
            int newScore=stoi(newscore); // "       "
            
            //=========
            if(newScore>oldScore){  // checking for high score
            fout<<" "<<newscore<<" "; // print this
            }
            else
            fout<<" "<<line<<" ";
            
            replace=false;
        }
        else
           fout<<line<<" ";
         //fout<<" "<<name<<" "<<newscore<<" "<<endl;
    }
        if(!found)
        fout<<"\n"<<" "<<name<<" "<<newscore<<" "; /// if name doesn't exist in file then
    result=rename(oldname,newname);  //<------problem HERE [Works perfectly fine in Mac but permission denied in]
    }                                 //+windows
    else{  // if no file then create it
        ofstream fout( "Data.txt" );
        fout<<""<<name<<" "<<newscore<<" "<<endl;
    }
}



This post has been edited by nomads77: 04 May 2015 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6281
  • View blog
  • Posts: 21,597
  • Joined: 05-May 12

Re: How to edit particular data in the file ?

Posted 04 May 2015 - 02:44 PM

Yes, you need to close the file before you try renaming or deleting it.
Was This Post Helpful? 0
  • +
  • -

#5 nomads77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-October 14

Re: How to edit particular data in the file ?

Posted 12 May 2015 - 08:33 AM

View PostSkydiver, on 04 May 2015 - 02:44 PM, said:

Yes, you need to close the file before you try renaming or deleting it.


It didn't work. Any idea.
My code::
/*
  File Name: Fstream.cpp
  ---------------------------------
  This portion of the program will demostrate File I/O functionality.
  When copy function is invoke, this function will print the score earned and name in the file.   


*/

#include <fstream>
#include <iostream>
#include <cstdlib>
#include "File.h"
#include "Fstream.h"
using namespace std;
/*
-Code not working in some of the PC due to access problem
Change: 
-To basic append flag rather than replace; display all the score allowing repeatation 
*/

void Fstream::copy(string name,string newscore)  //<----GETS NAME AND SCORE {name,new score}
{
    int result;
    bool found =false;
    bool replace =false;
    char oldname[] ="Copy.txt";
    char newname[] ="Data.txt";
    
   
    //string newscore="2000000";
    ifstream fin("Data.txt");
     // ofstream fout( "Copy.txt");
	 ofstream fout( "Copy.txt", ios::app);
    if(fin.good()){  // goes inside if file already exists
        //ofstream fout( "Copy.txt", ios::app);
    while(getline(fin,line,' '))
    {

        if(line==name)
        {
            replace=true;
            found=true;
            fout<<line;            //break;
        }
        else if(replace){
            //check for line < newscore if yes then
            int oldScore=stoi(line); // string to integer
            int newScore=stoi(newscore); // "       "
            
            //=========
            if(newScore>oldScore){  // checking for high score
            fout<<" "<<newscore<<" "; // print this
            }
            else
            fout<<" "<<line<<" ";
            
            replace=false;
        }
        else
           fout<<line<<" ";
         //fout<<" "<<name<<" "<<newscore<<" "<<endl;
    }
        if(!found)
        fout<<"\n"<<" "<<name<<" "<<newscore<<" "; /// if name doesn't exist in file then
       /* 
       fout.close();
        fin.close();
        result=rename(oldname,newname);
		*/  //<------problem HERE [Works perfectly fine in Mac but permission denied in]
    }                                 //+windows
    else{  // if no file then create it
        ofstream fout( "Data.txt" );
        fout<<""<<name<<" "<<newscore<<" "<<endl;
        //fout.close();
    }
	    fout.close();
        fin.close();
        result=rename(oldname,newname); 
}



Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5519
  • View blog
  • Posts: 17,109
  • Joined: 25-December 09

Re: How to edit particular data in the file ?

Posted 12 May 2015 - 10:44 AM

Instead of playing with replace why not just read in the entire input file into a std::vector then do all the searching and replacing in memory. Then just close the input stream and open the same file for output, truncating the information, and write the modified information.

If you intend to stick to the method you're trying to use remember when you open the file in append mode everything is written to the end of the file. You probably should be opening your output file using the default open mode that deletes anything in that file when opened. Then "copy" the "original" information to the output file, changing anything that requires changing before writing that information.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 nomads77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 02-October 14

Re: How to edit particular data in the file ?

Posted 13 May 2015 - 04:30 AM

View Postjimblumberg, on 12 May 2015 - 10:44 AM, said:

Instead of playing with replace why not just read in the entire input file into a std::vector then do all the searching and replacing in memory. Then just close the input stream and open the same file for output, truncating the information, and write the modified information.

If you intend to stick to the method you're trying to use remember when you open the file in append mode everything is written to the end of the file. You probably should be opening your output file using the default open mode that deletes anything in that file when opened. Then "copy" the "original" information to the output file, changing anything that requires changing before writing that information.

Jim


Thanks for the help. I'm not that proficient in Programming. Fairly new to this stuff.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1