5 Replies - 5242 Views - Last Post: 04 January 2012 - 06:02 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

ofstream will not write folders and files

Posted 03 January 2012 - 06:51 AM

Here is my code:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream outFile;
    cout << "Writing rpgsave.txt." << endl;
    outFile.open("data\\rpgsave.txt");
    outFile << "10 3 0 10 3 0 10 3 0" << endl;
    outFile.close();
    cout << "Writing hasrun.txt" << endl;
    outFile.open("data\\hasrun.txt");
    outFile << "0" << endl;
    outFile.close();
    return 0;
}



It just won't seem to write up these two files (rpgsave.txt & hasrun.txt)... Thank's for reading this thread.

Is This A Good Question/Topic? 0
  • +

Replies To: ofstream will not write folders and files

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4080
  • View blog
  • Posts: 12,590
  • Joined: 25-December 09

Re: ofstream will not write folders and files

Posted 03 January 2012 - 07:07 AM

Probably because the directory data does not exist. The C++ iostream functions will not create a directory. You will need to use operating system specific functions, or maybe look into the boost filesystem library.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: ofstream will not write folders and files

Posted 04 January 2012 - 12:33 AM

Oh that's the case. I thought it does create directories? Or does it only create files?
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: ofstream will not write folders and files

Posted 04 January 2012 - 03:53 AM

When you call open() the file will be created if does not exist or contents will be deleted unless you add 2nd argument to specify that you want to append.
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 780
  • View blog
  • Posts: 2,312
  • Joined: 20-March 10

Re: ofstream will not write folders and files

Posted 04 January 2012 - 05:40 AM

View Posthulla, on 04 January 2012 - 07:33 AM, said:

Oh that's the case. I thought it does create directories? Or does it only create files?


Jim is of course correct..

No it doesn't create directories

Yes it only creates files.

Assuming your running Windows and by your indications you are...

This code example handles all eventualities and will write your .txt files to your project directory in a folder called 'data'.

#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;

int main()
{
	ofstream outFile;
	cout << "Writing rpgsave.txt." << endl;
	outFile.open("data\\rpgsave.txt");
	if (outFile.fail() == false)
	{
		outFile << "10 3 0 10 3 0 10 3 0" << endl;
		outFile.close();
	}
	else
	{
		CreateDirectory("data\\", NULL);
		outFile.open("data\\rpgsave.txt");
		outFile << "10 3 0 10 3 0 10 3 0" << endl;
		outFile.close();
	}


	cout << "Writing hasrun.txt" << endl;
	outFile.open("data\\hasrun.txt");
	if (outFile.fail() == false)
	{
		outFile << "0" << endl;
		outFile.close();
	}
	else
	{
		CreateDirectory("data\\", NULL);
		outFile.open("data\\hasrun.txt");
		outFile << "0" << endl;
		outFile.close();
	}

	cout << "Press Return Key to Continue...." << endl;
	cin.ignore();
	cin.get();
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: ofstream will not write folders and files

Posted 04 January 2012 - 06:02 AM

Oh ok that's cool. There's a function that creates directories. :D Thank you everyone
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1