Create a new NotePad document

Okay, so Ive learned how to read a file and write into a file, but I want to know how to create a NEW file. How would I do this? Im using windows Vista and I want to create a new Notpad Document. Thanks for the help guys! :)
I could see where this would trip up a newer user it is a little odd.

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<fstream> /*This is the important one*/

int main()
{
   std::ofstream NewFile("TheDocument.txt", std::ios_base::out); /*I'm not using any namespaces */
/*Code Code Code*/


NewFile.close();
return 0;
}


You see when windows sees a file, it checks the extention of that file against a collection of known extentions kept in the registry. In this case ".txt" would be notepad so any file you create with a ".txt" extention will be opened with notepad.exe.

The interesting part is that it doesn't actually matter what is in that file, what it was meant to do or if the application that is registered to that extention is even capable of opening it. If you take your application and change it from "a.exe" to "a.doc" Windows will try to open it with what ever ".doc" is registered to.

EDIT: I forgot to mention that unless you specify the UNC path for the document it will be created in your current execution path when the application is run.

REEDIT: It just occured to me that you could mean you want to create a new FOLDER, is that the case?
Last edited on
No, you had it right. I want to create a new notepad.txt document. But where would I put the name, and where do I put where its located? (Documents, Picutres, Music)
You put that data right where I have "TheDocument.txt" written on Line 6. Any directories you specify will have to be in UNC format as quoted strings with double backslashes though so you should read up on that. http://en.wikipedia.org/wiki/Uniform_Naming_Convention#Uniform_Naming_Convention

EDIT: The thing about backslashes being used as an escape character for strings applies to C++, so you'll have to double up on them.
Last edited on
Ooh, okay thanks. BUt what does the 'out' do? and isnt the ios_base included in a namespace?
Topic archived. No new replies allowed.