Custom Output File Name

Allowing the user to choose what the output file name is.

Page 1 of 1

3 Replies - 19913 Views - Last Post: 19 December 2010 - 03:42 AM Rate Topic: -----

#1 Enthused?   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-December 10

Custom Output File Name

Posted 18 December 2010 - 07:01 PM

Well this will officially be my first post on D.I.C. I've had fun looking at tutorials and other tid bits so far. In advance, thank you for your generous help!

Over Christmas break I have been experimenting with file input/output and I came across a question. Can you write a file output program where the user chooses what to call the file where the information is sent? For example, I included some code below which demonstrates what I would like to do (even though it fails horribly). When I debug/run the program on Visual C++ 2010 it gives no errors but skips the "blah" portion of the following code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX_CHAR = 20;

int main()
{
    char file_name[MAX_CHAR]; // Stores the user's desired file name.
    ofstream ofile;

    cout << "What would you like to call your file? ";
    cin >> setw (sizeof (file_name)) >> file_name;

    ofile.open(file_name); // Creates and opens a file with a name: 'file_name'.

    // Blah...

    ofile.close();

    return 0;
}



There's really no rush for your responses, but if and/or I do receive one thank you again!

-A.L.

Is This A Good Question/Topic? 0
  • +

Replies To: Custom Output File Name

#2 Alex6788   User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Custom Output File Name

Posted 18 December 2010 - 07:16 PM

This should do what you're asking. Read the comments in the code.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX_CHAR = 20;

int main()
{
    string file_name; // strings are better to use than char
    ofstream ofile;

    cout << "What would you like to call your file? ";
    cin >> file_name;

    ofile.open(string(file_name+".txt").c_str()); // will make a file named after the user and save it as a text file and the function open doesn't take a variable of type string so .c_str converts it to type const char *

    // Blah...

    ofile.close();

    return 0;
}




I hope this helps.

This post has been edited by Alex6788: 18 December 2010 - 07:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 Enthused?   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-December 10

Re: Custom Output File Name

Posted 18 December 2010 - 07:30 PM

Of Course it helped Alex6788!

The only other addition needed was the string header: #include<string>. Other than that you answered my question. Thanks again.

A.L.
Was This Post Helpful? 0
  • +
  • -

#4 Alex6788   User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Custom Output File Name

Posted 19 December 2010 - 03:42 AM

I'm glad i could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1