#### Archived

This topic is now archived and is closed to further replies.

# create directories and files in c++

This topic is 5464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do I create directories in an plattform independent way? Is _mkdir the solution? I think that fopen followed by a fclose will create a file in a plattform independent way but how do i remove it? I get confused when reading about this in the books I''ve looked in...

##### Share on other sites
quote:
Original post by peer
How do I create directories in an plattform independent way?
Is _mkdir the solution?

Not quite. There''s no way of doing this mandated by the C++ Standard, but mkdir() (no underscore) is part of the POSIX.1-1988 Standard, which means you should be able to get mkdir() implementations on several platforms. Under Windows, you should look for direct.h for directory manipulation functions, under *nix systems, look for sys/types.h and sys.stat.h.
quote:

I think that fopen followed by a fclose will create a
file in a plattform independent way but how do i remove
it?

There is a function called remove() in cstdlib for that purpose.

You need to be careful with notions of platform independent''. Its not really possible to write something that is truly platform independent. Rather, you write code which is multi-platform dependent. That is, you have to make assumptions about what you are targeting else you cannot know what will be present or what will be missing. To do that, decide which platforms you wish to target and then write code (i.e. compile and test) for those platforms simultaneously. You should end up with a single code base that will compile and work across those platforms, and will probably need less work to port to a new platform than if you only target a single platform.

##### Share on other sites
boost::filesystem::create_directory

- Magmai Kai Holmlor

Not For Rent

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]
[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
[Free C Libraries | zlib ]

##### Share on other sites
Boost''s filesystem currently only has a POSIX implementation. That covers an awfull lot, though.

##### Share on other sites
quote:
Original post by SabreMan
Its not really possible to write something that is truly platform independent. ... decide which platforms you wish to target and then write code (i.e. compile and test) for those platforms simultaneously.

int main (void){  return 0;}  

I haven't decided which platform to run this on, yet I know with certainty that it will run on any conforming freestanding implementation, with no porting required.

PS. You could say 'but you're targetting the 'conforming freestanding implementation' pseudoplatform' -- it might not work on another pseudoplatform. However, any conformant ANSI C implementation supports the conforming freestanding implementation pseudoplatform. Therefore, there are no other pseudoplatforms to consider.

QED.

[edited by - Mayrel on October 8, 2003 7:18:11 AM]

##### Share on other sites
quote:
Original post by Mayrel
I haven''t decided which platform to run this on, yet I know with certainty that it will run on any conforming freestanding implementation, with no porting required.

However, you don''t know with all certainty that you have an implementation which conforms in the respects that concern you until you take a look at what you have (or use knowledge you''ve already attained). At that point, you are targeting a specific platform. It''s all very well saying my code is 100% ISO compliant'', but that won''t get you very far if your compiler can''t handle it. The problem increases as you move beyond idiotic examples to real-world code that has to do something useful, particularly as the C++ Standard does not describe all those things which you would generally want to do in any useful program, like putting up a GUI, connecting to a database, communicating with other processes, etc.

##### Share on other sites
Well, that''s fine, yes. There''s no genuinely portable way to access features not proscribed by the standard. I was responding to your blanket claim that "it''s not really possible to write something that is truly platform independent."

It is possible to write a great many programs that use only features in the standard. And although it''s true to say that not all compilers correctly handle the standard, they are *meant* to.

I wouldn''t consider my code to be non-portable because it doesn''t compile on a non-conformant compiler, although naturally I would consider it to be non-portable if I used features that aren''t standardised.

It''s kinda pointless to judge the portability of a C program on whether or not it can be compiled by a broken C compiler: I''d say a broken C compiler isn''t really a C compiler. To greatly exaggerate things, it''s like saying a C program is non-portable because it doesn''t work on a FORTRAN compiler.

##### Share on other sites
You guys arwe making way harder then it should be. Just insert the following code:
//add this to your include#include <tchar.h>#include <fstream>#include <iostream>

And then insert this in your int main() function
CreateDirectory( _T( "Directory name" ), NULL);ofstream datafile001("test.txt");datafile001 << "Text here" << endl<< "more text here" << endl;datafile001.close();

But lets say u want to make a person make a directory?
char ipt1[80];string file = ipt1;cout << "Directory name: ";cin.getline(ipt1, 80);CreateDirectory( _T( file.c_str() ), NULL);

and there you have it.

##### Share on other sites
How comes you think the code above would be platform independent?

##### Share on other sites
Platform independent? Use import java.io.*;
Then look at File class. It''s mkdir(); or public boolean mkdirs();
Hey it''s not a C, but it would work.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632632
• Total Posts
3007524
• ### Who's Online (See full list)

There are no registered users currently online

×