help with fstream

This is a discussion on help with fstream within the C++ Programming forums, part of the General Programming Boards category; I am very new to c++ and need some help with fstream.h Could someone please tell me how to ask ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Question help with fstream

    I am very new to c++ and need some help with fstream.h
    Could someone please tell me how to ask the user for a name and make that the name of a .txt please? I imagine it would be something like this...

    int num;
    cin>>num;
    ofstream a_file(num, ".txt");

    etc...

    If someone could help with this, or direct me to a advanced tutorial, it would be most appreciated.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    
    
    int main(){
    
    	std::ofstream out;
    	
    	std::string str;
    	
    	std::cout << "Enter the name of the file" << std::endl;
    	
    	std::cin >> str;
    	
    	str.append(".txt");
    	
    	out.open(str.c_str(),std::ios::out);
    	
    	if(!out.is_open()){
    		std::cout << "Could not open" << std::endl;
    		return 1;
    	}
    	
    	std::cout << "Success!!! Created " << str << std::endl;
    	
    	out << "This is text";
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 09:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 11:21 AM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21