View Full Version : [C++]how to create files?
SledgeHammer_999
November 13th, 2007, 09:00 PM
I am a total noob in C++.
How to create files in C++? How to check if a file exists? Is it possible to do it using fstream?
Kadrus
November 13th, 2007, 09:04 PM
Euuh..I might be getting this wrong?But do you want to compile files in C++ or something like that?Run the program that you code?
SledgeHammer_999
November 13th, 2007, 09:07 PM
Sorry what I meant was how do I create files/folders using C++. Are there any functions that I can use to create a file let's say in "/home/username/sadf.txt"?
Martin Witte
November 13th, 2007, 09:26 PM
take a look at the ofstream class, for an example see eg. http://www.cplusplus.com/doc/tutorial/files.html. another useful example is here http://www.cprogramming.com/tutorial/lesson10.html, it shows also how to check if a file exists
SledgeHammer_999
November 13th, 2007, 09:31 PM
I have already read this(pretty good tutorials). But this shows me how to output data to an existing file. But I am looking for a way to create a file before being able to output data to that file.
After a quick test a fstream.open doesn't automatically create the file am I passing it.
smartbei
November 13th, 2007, 09:36 PM
Simplest way is using cstdio:
#include <cstdio>
using namespace std;
int main()
{
FILE * file = fopen("newfile.txt","w");
fprintf(file, "TESTING!!!");
fclose(file);
return 0;
}
aks44
November 13th, 2007, 09:43 PM
Simplest way is using cstdio
Ans this is supposed to be C++? :-\"
Sorry I couldn't help. :p
The preferred C++ way is to use std::fstreams, or boost::filesystem.
smartbei
November 13th, 2007, 09:47 PM
Haha, true. That doesn't change the fact that that is the simplest way :).
Anyway, if you are having trouble with fstream, see: http://www.cplusplus.com/doc/tutorial/files.html
or (for more newbie-friendly): http://www.daniweb.com/forums/post31214.html
aks44
November 13th, 2007, 09:52 PM
Haha, true. That doesn't change the fact that that is the simplest way :).
Duh? This is *even* more simple:
#include <fstream>
int main()
{
std::ofstream file("newfile.txt");
file << "TESTING!!!";
return 0;
}
Couldn't help again... :p
smartbei
November 13th, 2007, 10:01 PM
=D>
You're right, of course. One line less.
Still, for some reason when I need to quickly write a script in c++ that prints something basic to a file I reach for cstdio. I'll have to work on that :).
SledgeHammer_999
November 13th, 2007, 10:03 PM
haha this is interesting.
I was using a std::fstream object and fstream.is_open returned false. But now that I used a std::ofstream object ofstream.is_open returns true.
Can I make std::fstream to automatically create the file like std::ofstream does?
aks44
November 13th, 2007, 10:14 PM
One line less.With the added benefit of C++ type safety. :)
Still, for some reason when I need to quickly write a script in c++ that prints something basic to a file I reach for cstdio.Indeed, habits are hard to break. Even after 2 years on the project I work on, I still find myself using plain C++ types (int, double, ...) instead of the typedefs we all agreed on (gInt, gReal, ...). Oh well, as long as I remember to search/replace before committing, that doesn't harm anyone... ;)
aks44
November 13th, 2007, 10:21 PM
Can I make std::fstream to automatically create the file like std::ofstream does?
std::ofstream is just a std::fstream with a special constructor. Check both docs, the difference lies in the second parameter (std::ios::cant_remember_what).
Sporkman
November 14th, 2007, 03:48 PM
I use C methodology for IO, myself ( printf, fprintf, fopen, etc. ). I know it, it's simple, and it works.
dwhitney67
November 15th, 2007, 06:10 AM
haha this is interesting.
I was using a std::fstream object and fstream.is_open returned false. But now that I used a std::ofstream object ofstream.is_open returns true.
Can I make std::fstream to automatically create the file like std::ofstream does?
Think of the ofstream as a sub-class of fstream. If you wanted to use fstream and the is_open() method, then you would need to specify that you are opening the file for "output" when calling the fstream constructor. For example:
fstream myFile( "file.txt", ios::out );
if ( myFile.is_open() )
{
...
}
An easier solution is which obviates the need to specify the second constructor arg is:
fstream myFile( "file.txt" );
if ( myFile )
{
...
}
If you know ahead of time that your file will only be used for output, then use ofstream. Similarly, if you only plan to read from a file, use ifstream. In case where writing and reading may be performed on the same file, and you wish to use the same file handle, then use the base fstream.
SledgeHammer_999
November 15th, 2007, 05:51 PM
This merely works.
Keep in mind that I want to create the file automatically if it doesn't exist.
This works as I want:
fstream myFile( "file.txt", std::ios::out);
But this doesn't:
fstream myFile( "file.txt", std::ios::out|std::ios::in);
In the first case the file is created(.is_open=true) in the second it isn't created(.is_open=false).
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.