Thread: Creating a New File

  1. #1
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Creating a New File

    Hi,
    I need to create a new file and then name it a number.
    I would be using ofstream()

    Thanks

  2. #2

  3. #3
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47
    Okay,
    This is what I used that works:

    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <iostream.h>
    #include <windows.h>
    #include <conio.h>


    int main()
    {


    char str[30];
    cin>>str;
    strcat(str,".txt");
    ofstream a_file(str);
    a_file << "sadfasf";

    return 0;
    }

    Now, what do I do if I want to delete the file?
    Thanks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    a_file.close();

    -Prelude
    My best code is written with the delete key.

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Jesus... that is a lot of includes...


    Code:
    #include <iostream> // for standard i/o
    #include <fstream> // for file i/o
    #include <stdlib> // for system call
    using namespace std;
    
    int main()
    {
    
       ofstream outs;
       char answer = 'n';
    
       outs.open("file.txt");  // open/create file
    
       outs << "This is some text in a file";
    
       outs.close(); //close file
    
       cout << "do you want to delete file (y/n):";
       cin   >> answer;
    
       if ((answer == 'y')||(answer == 'Y'))
           system ("del file.txt"); // delete file
    
       return 0;
    
    } // end main
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM