Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,120 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,787 people online right now. Registration is fast and FREE... Join Now!




creating files with user defined names

 
Reply to this topicStart new topic

creating files with user defined names, <fstream> ofstream, f.open etc...

nirovanton
10 Apr, 2007 - 02:30 PM
Post #1

New D.I.C Head
*

Joined: 12 Feb, 2007
Posts: 12


My Contributions
I was wondering how i could make this work, I have been trying a few different ways and only get compiling errors
what i need to do is prompt the user to input a string that will then be used to name a .txt file like so:

CODE

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
  {

    string file_name;
    
   cout<<"Please enter the name of the file i will create for you"<<endl;
  
   cin>>file_name;   //   filetest.txt

   ofstream f;
  
   f.open(file_name);  // this is the part that is killing me, how can
                                // i enter a user specified string into this field?
  
   f<<"All this text is being written to the file";
  
   return 0;
  }  


thank you in advance for whatever help i can get with this!!

This post has been edited by nirovanton: 10 Apr, 2007 - 04:03 PM
User is offlineProfile CardPM
+Quote Post

vlade
RE: Creating Files With User Defined Names
10 Apr, 2007 - 03:13 PM
Post #2

New D.I.C Head
*

Joined: 3 Apr, 2007
Posts: 1


My Contributions
could you have the user input into an array and then using a while or for loop input the array piece by piece into the file
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Creating Files With User Defined Names
10 Apr, 2007 - 10:36 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
The syntax for open is:
void open ( const char * filename, ios_base::openmode mode );

so the only problem you seem to have is that you are passing a string rather than a char array... lucky for you, the string has a neat conversion utility built in:

CODE
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

    string file_name;

    cout<<"Please enter the name of the file i will create for you"<<endl;

    cin>>file_name;   //   filetest.txt

    ofstream f;
    f.open(file_name.c_str());
    if (f.is_open())
    {
        f << "All this text is being written to the file" << endl;

        f.close(); //remeber ot close your files.
        ifstream inFile;
        string stuff;
        
        inFile.open(file_name.c_str());
        if (inFile.is_open())
        {
            while (!inFile.eof())
            {
                inFile >> stuff;
                cout << stuff <<endl;
            }
            
            inFile.close();
        } else
        {
            cout << "Could not open the file for reading" <<endl;
        }


    
    } else
    {
        cout << "Could not open file for output!"<<endl;
    }

    
    

    return 0;
}

User is offlineProfile CardPM
+Quote Post

nirovanton
RE: Creating Files With User Defined Names
11 Apr, 2007 - 08:06 AM
Post #4

New D.I.C Head
*

Joined: 12 Feb, 2007
Posts: 12


My Contributions
thank you man that was a lot of help!!

QUOTE(NickDMax @ 10 Apr, 2007 - 11:36 PM) *

The syntax for open is:
void open ( const char * filename, ios_base::openmode mode );

so the only problem you seem to have is that you are passing a string rather than a char array... lucky for you, the string has a neat conversion utility built in:

CODE
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

    string file_name;

    cout<<"Please enter the name of the file i will create for you"<<endl;

    cin>>file_name;   //   filetest.txt

    ofstream f;
    f.open(file_name.c_str());
    if (f.is_open())
    {
        f << "All this text is being written to the file" << endl;

        f.close(); //remeber ot close your files.
        ifstream inFile;
        string stuff;
        
        inFile.open(file_name.c_str());
        if (inFile.is_open())
        {
            while (!inFile.eof())
            {
                inFile >> stuff;
                cout << stuff <<endl;
            }
            
            inFile.close();
        } else
        {
            cout << "Could not open the file for reading" <<endl;
        }


    
    } else
    {
        cout << "Could not open file for output!"<<endl;
    }

    
    

    return 0;
}



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:59PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month