Thread: Variable filename

  1. #1
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65

    Variable filename

    When I do something like ofstream newfile("file.txt"); in order to create a new file, how can I use a user-input variable as the filename and add .txt to it in order to make a custom-named file?
    \0

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    string input;
    getinput(input); // you implement
    input += ".txt";
    ofstream file(input.c_str());
    is one way to do it
    Last edited by Stoned_Coder; 06-09-2003 at 12:22 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Thanks, I think that will work... but another part of my code is being screwy... when I enter something for the cin, it executes the if fine, but then it skips the next cin ... I know it's because it's reading the '\n' character, but I don't know how to stop it... any suggestions?

    Here's the code for better understanding:

    Code:
    void main() {
    	char name[20];
    
    	char choice;
    
    	cout << "Load file (L) or create New file (N)?";
    	cin >> choice;
    	if (tolower(choice) == 'l') {
    
    		//Does stuff...
    
    	}else{
    
    	cout << "Please enter your name(no more than 20 letters): ";
    	cin.getline(name, 20, '\n');			//It skips this line (or rather, it enters '\n')
    
    }
    \0

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    one way is to place a call to ignore() before every call to getline(). Depending on how persnickity you want to be you can do something simple like this:

    cin >> choice;
    cin.ignore();
    getline(cin, string1);

    which will ignore just one char, or somewhat more sure would be something like this:

    cin >> choice;
    cin.ignore(1000);
    getline(cin, string1);

    which will ignore up to 1000 char or until a newline char is found, whichever comes first. The most robust way is to use the largest size int possible, which is available from the limits.h or climits file and is called something like INT_MAX. It would look something like this:

    #include <climit>
    //do dah
    cin >> choice;
    cin.ignore(INT_MAX, string1);
    getline(cin, string1);

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    148
    >>The most robust way is to use the largest size int possible
    You need the max. possible stream size.

    Code:
        cin.ignore(numeric_limits<std::streamsize>::max(), '\n');

  6. #6
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Thanks, that worked! I have another question ... is there a way to do a tolower() effect on a string?
    \0

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I believe most string classes have a tolower() function that would be used somehting like this:

    string s = "Hello";
    s.tolower();
    cout << s;

    It might be called Tolower() or tolower() or toLower() or whatever, so look at your documentation. the version of tolower() in sting.h (aka cstring) is for single char only, not full C style strings.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    148
    For std::string you can do you something like this
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <functional>
    
    using namespace std;
    
    int main()
    {
       string str = "Hello World";
    
       transform(str.begin(),str.end(),str.begin(),ptr_fun(::toupper));
    
       cout << str << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  2. I/O--Using a variable as a filename
    By jedo in forum C++ Programming
    Replies: 7
    Last Post: 09-06-2005, 10:57 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM