Reading and writing to the same file using fstream

R J
void Withdraw(int index, int amount)
{
    int Balindex = 0;
    fstream input("Balance.txt");
    float balance = 0.0;
    while ((!input.eof())&&(Balindex != index))
    {
        balance = 0.0;
        input >> balance;
        Balindex++;
    }
    input >> balance;
    balance = balance - amount;
    input << balance << endl;
}

i am trying to read balance from a text file, and deduct the amount withdrawn. index holds the chronological number of the balance. my file however wont overwrite the existing value with the new one. Any suggestions?

Dietmar Kühl

When switching between input and output for a filestream without an intervening seek, you get undefined behavior. It doesn't matter where you seek to but you need to seek! For example, you can seek to zero characters away from the current position or, more likely, back to the position where the value actually started:

std::streampos start = input.seekg(0, std::ios_base::cur);
if (input >> balance) {
    input.seekp(start);
    input << (balance - amount);
}

Note, however, that the stream won't make space for additional characters, i.e., if what you read is shorter than what you write, you'll overwrite data following the originla input. Likewise, you will only overwrite the characters you overwrite. I'd recommened against doing anything like that. If you want to update a file, make sure you are using fixed width records!

Of course, you also shoudn't use input.eof() to verify if the stream is any good: if the stream goes into failure mode, e.g., due to a misformatted input, you'll never reach the point where input.eof() yields true, i.e., you'd get an infinite loop. Just use the stream itself as condition. Personally, I would use something like

while (input >> balance && Balindex != index) {
    ++Balindex;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

writing back into the same file after reading from the file

From Dev

Is reading and writing to the same file thread-safe?

From Dev

Issue writing to file with fstream

From Dev

c++ Reading string from binary file using fstream

From Dev

Reading files using getline fstream

From Dev

read from a file while another process is writing to it using std::fstream

From Dev

Using a file for reading and writing simultaneously fails

From Dev

Would reading/writing a file take the same time as moving the file?

From Dev

Reading a line from a file into different variables using fstream

From Dev

Reading and writing to the same file using try-with-resources

From Dev

Reading and writing to the same file from two different scripts at the same time

From Dev

writing back into the same file after reading from the file

From Dev

showing an extra string while reading a file in c++ using fstream

From Dev

How to make reading and writing the same file in the same pipeline always "fail"?

From Dev

Java reading and writing to same file

From Dev

Creating undeletable file and reading/writing on it using java

From Dev

C++ Trouble reading and writing to text file using the fstream object

From Dev

c++ Reading string from binary file using fstream

From Dev

Reading and Writing Header to File using MATLAB

From Dev

Reading from a file and writing to another using Awk

From Dev

Perl performance when reading and writing the same file

From Dev

Opening an fstream for reading/ writing (binary)

From Dev

Reading a file using fstream

From Dev

Reading a line from a file into different variables using fstream

From Dev

Reading and writing to the same file using try-with-resources

From Dev

Writing to file With FStream Not Working

From Dev

Using structure in reading a file, calculating, and writing into another

From Dev

Index not the same between writing and reading excel file

From Dev

Problems reading and writing to the same file descriptor in Linux

Related Related

  1. 1

    writing back into the same file after reading from the file

  2. 2

    Is reading and writing to the same file thread-safe?

  3. 3

    Issue writing to file with fstream

  4. 4

    c++ Reading string from binary file using fstream

  5. 5

    Reading files using getline fstream

  6. 6

    read from a file while another process is writing to it using std::fstream

  7. 7

    Using a file for reading and writing simultaneously fails

  8. 8

    Would reading/writing a file take the same time as moving the file?

  9. 9

    Reading a line from a file into different variables using fstream

  10. 10

    Reading and writing to the same file using try-with-resources

  11. 11

    Reading and writing to the same file from two different scripts at the same time

  12. 12

    writing back into the same file after reading from the file

  13. 13

    showing an extra string while reading a file in c++ using fstream

  14. 14

    How to make reading and writing the same file in the same pipeline always "fail"?

  15. 15

    Java reading and writing to same file

  16. 16

    Creating undeletable file and reading/writing on it using java

  17. 17

    C++ Trouble reading and writing to text file using the fstream object

  18. 18

    c++ Reading string from binary file using fstream

  19. 19

    Reading and Writing Header to File using MATLAB

  20. 20

    Reading from a file and writing to another using Awk

  21. 21

    Perl performance when reading and writing the same file

  22. 22

    Opening an fstream for reading/ writing (binary)

  23. 23

    Reading a file using fstream

  24. 24

    Reading a line from a file into different variables using fstream

  25. 25

    Reading and writing to the same file using try-with-resources

  26. 26

    Writing to file With FStream Not Working

  27. 27

    Using structure in reading a file, calculating, and writing into another

  28. 28

    Index not the same between writing and reading excel file

  29. 29

    Problems reading and writing to the same file descriptor in Linux

HotTag

Archive