void writingTransactionToFile(string transaction)
{
	string temp;
	int test;
	ifstream myfile("transactionlog.txt");
	ofstream tempfile ("tempfile.txt", ios::app);
	if (tempfile.is_open())
	{
		tempfile << transaction;
		if(myfile.is_open())
		{
			while(!myfile.eof())
			{
				getline(myfile,temp);
				cout << temp;
				tempfile << temp << endl;
			}
		} else {
			cout << "Unable to open file.";
		}
		tempfile.close();
		myfile.close();
	} else 
		cout << "Unable to open file";
	test = rename("tempfile.txt", "transactionlog.txt");
	if(test != 0)
		perror("Error: ");
	cout << test;
}

Ok so its mainly the rename function that doesn't work. It just returns the value -1. perror doesn't return any error.

Not sure why it doesn't like it?

call me silly but why not just send the data straight to the correct file name. Also i would recomend you do all file copying in binary mode to prevent loss of any data.

Chris

call me silly but why not just send the data straight to the correct file name. Also i would recomend you do all file copying in binary mode to prevent loss of any data.

Chris

Because I want the latest data to be at the top and this is the only way to do it.

What exactly do you mean by 'at the top'?

Chris

What exactly do you mean by 'at the top'?

Chris

At the top of the text file.

i.e. its a list of transactions and I want the most recent transactions at the top. At the bottom of the file is the first transaction.

My text file would look something like this:

17/11/2008 - Something happened
14/11/2008 - Something happened before
11/11/2008 - etc.
10/11/2008 - etc.

So when I then print the stuff from the text file on screen it looks like this.

The only otherway I could think of is to read the text file backwards - but I have no idea how to do this either and cannot find a way of doing it. (whereas the method above seems to work, but the rename bit isn't, even though the error returns 'File Exists' :-/)

Are you deleting the previous transactionlog.txt somewhere? Else rename will fail because the file already exists.

You should also avoid using eof().
If you change this:

while(!myfile.eof())
{
    getline(myfile,temp);
    cout << temp;
    tempfile << temp << endl;
}

to this:

while(getline(myfile,temp))
{
    cout << temp;
    tempfile << temp << endl;
}

The eof() function is gone, and it also looks a lot cleaner :)

commented: very useful poster +1

1. No I thought it would overwrite it (like if you try to open a file, if it doesn't exist, it creates one)... guess it doesn't. Will look up file delete later (I funnily enough have a C++ tutorial now)

2. Ok will change code to that, not much difference - doesn't eof check if there is a line available? So its just doing the same (I could be wrong)

Ok realising I am retarded. I should have taken the 'Error Message' literally. It kept saying 'File Exists', which I then thought yes, it does exist, so rename it to that! Raging at my laptop.

However it now works. Thanks.

2. Ok will change code to that, not much difference - doesn't eof check if there is a line available? So its just doing the same (I could be wrong)

EOF can return true for more than that reason i.e certain characters can cause it to return true. I believe '\v' is one of those. This is why it should be avoided.

Chris

The documentation for rename() plainly states that if a file with the new name already exists, results are implementation-defined. You are better off deleting any existing 'new name' file first.

The problem with eof() is more insidious. Imagine what would happen to his code if an error flag were set in the middle of that loop. (An infinite loop.)

Always check against good(), and if that fails, you can check to see which flag caused the failure: whether a benign (and wanted) eof() or an obnoxious bad() or fail() of some sort.

Ctrl-D is the EOF character on *nix and in ASCII (EOT).
Ctrl-Z is considered an EOF character on DOS.
However, all this is system dependent and you should not generally see these characters when doing file I/O --as EOF is technically a condition, not a part of a stream.

Hope this helps.

1. No I thought it would overwrite it (like if you try to open a file, if it doesn't exist, it creates one)... guess it doesn't. Will look up file delete later (I funnily enough have a C++ tutorial now)

You're declaring an ofstream with the append flag (ios::app) which appends to the file, opens it and adds text instead of overwriting the content. Try it out without that flag, see what happens.

ofstream tempfile ("tempfile.txt", ios::app);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.