Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: [C++]how to create files?

  1. #1
    Join Date
    Mar 2006
    Beans
    837

    [C++]how to create files?

    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?

  2. #2
    Join Date
    Sep 2007
    Beans
    Hidden!

    Re: [C++]how to create files?

    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?

  3. #3
    Join Date
    Mar 2006
    Beans
    837

    Re: [C++]how to create files?

    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"?

  4. #4
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: [C++]how to create files?

    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
    Last edited by Martin Witte; November 13th, 2007 at 09:30 PM.

  5. #5
    Join Date
    Mar 2006
    Beans
    837

    Re: [C++]how to create files?

    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.

  6. #6
    Join Date
    Nov 2006
    Location
    Israel
    Beans
    765
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [C++]how to create files?

    Simplest way is using cstdio:
    Code:
    #include <cstdio>
    
    using namespace std;
    
    int main()
    {
        FILE * file = fopen("newfile.txt","w");
        fprintf(file, "TESTING!!!");
        fclose(file);
        return 0;
    }
    Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB + 1TB SATA2 / GeForce 6200TC / DL DVD+-RW / Dell 24" U2410

  7. #7
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: [C++]how to create files?

    Quote Originally Posted by smartbei View Post
    Simplest way is using cstdio
    Ans this is supposed to be C++?
    Sorry I couldn't help.

    The preferred C++ way is to use std::fstreams, or boost::filesystem.
    Not even tinfoil can save us now...

  8. #8
    Join Date
    Nov 2006
    Location
    Israel
    Beans
    765
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [C++]how to create files?

    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
    Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB + 1TB SATA2 / GeForce 6200TC / DL DVD+-RW / Dell 24" U2410

  9. #9
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: [C++]how to create files?

    Quote Originally Posted by smartbei View Post
    Haha, true. That doesn't change the fact that that is the simplest way .
    Duh? This is *even* more simple:

    Code:
    #include <fstream>
    
    int main()
    {
      std::ofstream file("newfile.txt");
      file << "TESTING!!!";
      return 0;
    }
    Couldn't help again...
    Not even tinfoil can save us now...

  10. #10
    Join Date
    Nov 2006
    Location
    Israel
    Beans
    765
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [C++]how to create files?

    =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 .
    Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB + 1TB SATA2 / GeForce 6200TC / DL DVD+-RW / Dell 24" U2410

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •