Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-08-2006, 05:29 AM   #1
LQ Newbie
Registered: Jun 2003
Posts: 10

Rep: Reputation: 0
Question Creat files C++

I'm learning C++ and have encountered a problem. I want my program to be able to create new files and directories. Using fstream I can only open & write to files that already exist, which doesn't happen to be that useful.

So in point form:
Problem: How do I create new files/directories using C++
Preferably: System independant solution, something that will work in both Linux and Windows without any changes (if possible).

Old 05-08-2006, 06:49 AM   #2
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
well, about creating files, you're wrong..
// example to demonstrate it
#include <iostream>
#include <fstream>

using namespace std;

int main()
   ofstream out("filethatdoesntexist.txt");
   out << "howdy" << endl;
   return 0;
Creating directories is a bit more complex, id say you better check out:
man 2 mkdir
There is no OS independant way to do it.. /:
Old 05-08-2006, 07:01 AM   #3
LQ Newbie
Registered: Jun 2003
Posts: 10

Original Poster
Rep: Reputation: 0
Talking Thanks

Thanks Artanicus, that did the trick. I can see where I was making a mistake in my code now.
Old 05-08-2006, 07:33 AM   #4
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96

Rep: Reputation: 15
For creating the dirs and all manipulating action just use GNU C Library.

Here is a link about the file system interface in *unix like OSes that use GCC:
Old 05-09-2006, 08:06 PM   #5
LQ Newbie
Registered: Jun 2003
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks nhydra, that's a really useful site for me.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
creat boot disk ust Linux - General 1 10-07-2005 09:34 PM
unable to creat io-slave Penguin of Wonder Linux - Hardware 0 09-26-2005 02:29 AM
how to creat /dev/loop8 abd_bela Debian 1 12-15-2004 04:45 PM
hello i want to know how to creat tar Paxmaster Linux - General 1 10-29-2004 06:03 PM
how to Creat a User cerberus Slackware 4 05-14-2003 11:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration