Appending a file ios::app

May 12, 2013 at 6:47pm
Hi,
This is my first post. Hope someone can help. I am trying to append a txt file I created in wordpad using ios::app. However the file is not appending?

Any advice would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <fstream>

using std::cout;
using std::endl;
using std::string;
using std::ofstream;
using std::ios;

int main()
{
	string info = "\n\n\t\tSpancill Hill";

	ofstream writer ("Lyrics.txt", ios::app);

if(! writer)
{
	cout << "Error opening file: " << endl;
	return -1;
}
writer << info << endl;
writer.close();

return 0;
}
May 13, 2013 at 2:56pm
working for me.
May 13, 2013 at 3:28pm
What's wrong with The code? It works just fine.
May 13, 2013 at 3:34pm
Hey folks,

Thanks for responding. It wasn't working for me. Had to put the full path dir into the code to get it to work.

my initial program to create a file worked but just would not append with this one.

Thanks
May 13, 2013 at 3:40pm
Probably your program was being run with a different working directory than you expected. If you're running from an IDE like Visual Studio, it might set the working directory the project directory instead of the directory where the EXE actually is.
Topic archived. No new replies allowed.