Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Overriting previous entries in a c++ text file

  • 17-11-2005 11:38am
    #1
    Closed Accounts Posts: 3


    I am a novice programmer trying to increment a counter and write it to a file, when i do this it adds it to the end of the text file. Is there any way i can overrite the previous entry with the new one


Comments

  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    I gave this a go. I assumed that the counter file existed and the number was the first item on the first line.

    The basic algorithm I used was:
    - fopen counter file, "r+" mode
    - Read line and convert to an integer.
    - Increment integer
    - Rewind file pointer and write new number back to counter file.
    - Close file.

    That should get you going. I won't post the bit of code I wrote unless you get nowhere with the above pseudo code.


  • Closed Accounts Posts: 3 leaprachanus


    In a text file it still doesnt overwrite the previous entry it just adds it in before the first entry


  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    In a text file it still doesnt overwrite the previous entry it just adds it in before the first entry
    Not in my code.
    Post your code so we can point out where it might be going wrong.


  • Closed Accounts Posts: 3 leaprachanus


    This is the origional code


    int main()
    {
    int reference=0;
    fstream hotreftxtfile;

    if(reference==0)
    {
    hotreftxtfile.open("hotref.dat",ios::in|ios::out);
    if (!hotreftxtfile)
    {
    cout << "Error: Can't open the file named data.txt.\n";
    exit(1);
    }

    reference=1;
    hotreftxtfile << reference;
    hotreftxtfile.close();
    }

    hotreftxtfile.open("hotref.dat",ios::in|ios::out);
    hotreftxtfile >> reference;
    reference+=1;
    hotreftxtfile << flush;
    hotreftxtfile.flush();
    hotreftxtfile << endl;
    hotreftxtfile << reference;
    hotreftxtfile.close();

    return 0;
    }


  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    if(reference==0)
    
    This is always true because you initialise reference to 0.
    hotreftxtfile << endl;
    hotreftxtfile << reference;
    
    You are putting the incremented reference on a new line, but only the first line of the file ls read. Lose the endl bit.

    You also need to move the file pointer back to the start of the file before writing reference to the file.
    hotreftxtfile.seekp( 0, ios::beg );
    
    cout << "Error: Can't open the file named data.txt.\n";
    
    Minor: filename in message is not what you are trying to open.

    You should check for read/write errors with the rdstate() function.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    I implemented a solution by opening the file for reading and getting the value for 'reference'. If there was a read error I assumed that the file did not exist and set 'reference' to 0 (it is later incremented to 1). I close the file.

    Then I reopen the file, for writing (and truncate it too) and write 'reference'.

    I was opening it for read/write but rdstate() indicated errors (== 3) when I wrote to the file.


Advertisement