8

Just started learning c++ today and im pretty boggled. its an amazing language but im having some trouble overwriting a file

#include <iostream>
#include <fstream>

using namespace std;

int main( )
{
    double payIncrease = 7.6;
    double annual;


    double annualIncrease;
    double newAnnual;

    double monthlyIncrease;
    double newMonthly;

    ifstream inStream;
    ofstream outStream;

// heres where the problem lies

        inStream.open("annualSalary.txt" );
        outStream.open("newAnnualSalary.txt");

if i change newAnnualSalary.txt to annualSalary.txt i get some very weird numbers. does anyone know why?

    inStream >> annual;
    inStream.close();
    double monthly = (annual/12);

    annualIncrease = ((annual/100)*payIncrease);

    monthlyIncrease = ((monthly/100)*payIncrease);


    newMonthly = (monthly + monthlyIncrease);
    newAnnual = (annual + annualIncrease);




    outStream <<"annual salary was: "<<  annual << "\n" ;  
    outStream <<"new annual salary is " << newAnnual << "\n ";
    outStream <<"new monthly salary is " << newMonthly <<"\n ";



    outStream.close();
    return 0;

}

im aware this is a very low skill level question but i am just learning.

OVERTONE
  • 11,797
  • 20
  • 71
  • 87
  • 2
    What do you mean by 'weird numbers'? – Jess Sep 16 '10 at 15:22
  • 1
    It _might_ be because you're trying to open annualsalary.txt twice, once inbound and once outbound. Try _not_ opening the file you're trying to overwrite until after you've closed it (i.e. put the outStream.open command after the inStream.close command) to see if that helps. – Michael Todd Sep 16 '10 at 15:26
  • @Michael, the outstream is opening newAnnualSalary.txt. He's good to go :) – Jeff LaFay Sep 16 '10 at 15:27
  • 1
    I understand that, but he mentioned "if i change newAnnualSalary.txt to annualSalary.txt i get some very weird numbers", which implies to me that he changed newannualsalary.txt to annualsalary.txt, the same name as the file he's trying to read. – Michael Todd Sep 16 '10 at 15:29
  • exactly. i tried to change it to annualsalary.txt and it went messay. by weird numbers i mean im getting minus decimals. i wasnt aware you couldnt have an out and in going from the same file. but im learning. thanks guys – OVERTONE Sep 16 '10 at 18:38
  • one side note (unrelated to your issue) - consider using `std::endl` instead of `"\n"` if you would like your stream flushed. See [this question](http://stackoverflow.com/questions/213907/c-stdendl-vs-n) for a good overview. – Mike Ellery Sep 16 '10 at 19:15

3 Answers3

9

You can't open the same file as an istream and an ostream at the same time. Since you are closing the istream pretty early on, why not just put the ostream open call after the istream close? Alternatively you can use an fstream which will allow reads and writes.

Niki Yoshiuchi
  • 16,883
  • 1
  • 35
  • 44
6

The stream classes (well, technically in this case the basic_filebuf class) are caching reads and writes to that file. The different file streams are not guaranteed to be synchronized.

Use a single fstream instead of two seperate streams to the file.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
2

It's because the default open parameter for ofstream is ios::out which destroys the contents of the file. This leaves your inStream object reading garbage into annual variable because it's pointing at the same file which just had it's contents destroyed. Hence your weird numbers.

Have inStream open the file and read the contents, close it, then open the outStream and write. This should fix the problem, but it would be best to ensure that no problems occur during processing before opening and destroying the file contents. If you don't you could encounter an error and end up with nothing in the file. Basically make sure you have good data to write before destroying the previous contents.

To show that what you're doing destroys the file:

#include <fstream>

using namespace std;
int main()
{
    ofstream x;
    x.open("ofTest.txt");
    x.close();
    return 1;
}

%> g++ test.cpp
%> cat ofTest.txt
Test file destruction
Test 1,2,3
%> ./a.out
%> cat ofTest.txt
%>
RC.
  • 27,409
  • 9
  • 73
  • 93
  • Yes, ios::trunc does truncate the file, but so does ios::out. Try it. – RC. Sep 16 '10 at 15:35
  • `ios::in | ios::out` does not truncate the file. `ios::trunc` alone is not a valid open mode. – Potatoswatter Sep 16 '10 at 19:44
  • I would expect ios::in | ios::out to leave the file contents as you're asking to read in the file contents with ios::in. That wouldn't make much sense if the file was truncated. – RC. Sep 16 '10 at 20:58