Thread: Writing to a file

  1. #1

    Question Writing to a file

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    void main()
    {
       string playername;
    
       cout << "What is the players name";
       cin >> playername;
    	
    	ofstream fin(playername + ".txt");
    	fin << "Hello World";
    }
    can anyone tell me what is wrong with the above code? When I try to compile it my compiler(I tried MSVC++ 6.0, Borland c+ 5.02, MinW, and Cygwin) always complaines about ofstream fin(playername + ".txt"); is this not the correct way to do this?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
       string playername;
    
       cout << "What is the players name";
       cin >> playername;
    	playername += ".txt";	
    	ofstream fin(playername.c_str());
    	fin << "Hello World";
    	fin.close();
    	return 0;
    }
    I see what you mean....but if you turned it to a C-Style string as above it will work

  3. #3

    Talking .

    Hey Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM