Writing to a file

This is a discussion on Writing to a file within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream> #include <string> using namespace std; void main() { string playername; cout << "What is the ...

  1. #1
    Perverted unanimous's Avatar
    Join Date
    Oct 2001
    Posts
    336

    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?
    Give me a bad reputation!!!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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
    Perverted unanimous's Avatar
    Join Date
    Oct 2001
    Posts
    336

    Talking .

    Hey Thanks
    Give me a bad reputation!!!

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, 01: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, 04:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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