Question:

How do I open a file in C++ and create one if it does not exist?

by  |  earlier

0 LIKES UnLike

Hey im trying to create an inventory. I know how to open a file allready created - ifstream fin (fin.open(filename)). Though im not sure how i create a file if one does not exist. Please help thankyou ben :-).

 Tags:

   Report

3 ANSWERS


  1. Open it withnotepad or some other text editor (i recommend http://notepad-plus.sourceforge.net/uk/s... and then save it as program.c  


  2. This isn't a matter of C++.  It's a matter of the operating system.

  3. Here is the code:

    #include <iostream.h>

    #include <conio.h>

    #include <fstream.h>

    main()

    {

          ifstream myFile;

          myFile.open("Inventory.txt",ios::app); //creating and opening opening the file in append mode

           myFile<<"whatever you want to add";

           myFile.close().

    getch();

    }

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.