fstream

This is a discussion on fstream within the C++ Programming forums, part of the General Programming Boards category; what is wrong with this? Code: #include <iostream> #include <fstream> using namespace std; ifstream fin; ofstream fout; int main() { ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    fstream

    what is wrong with this?

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    	ifstream fin;
    	ofstream fout;
    	
    int main()
    {
    	char* tname = 0;
    	cout << "Enter the name of the file to change :" << endl;
    	cin >> tname; 
    	fout.open(tname);
    	if (! fout.is_open())
    	{
    		cout << "Error opening file";
    		return 1;
    	} 
    	fout << "yo bob!";
    	fout.close();
    	return 0;
    }
    my app crash when i try to run it..?
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    Your not allocating space for your pointer...your just decalring a pointer. Either use new to create some space or don't use a pointer.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use std::string:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    	
    int main()
    {
    	ofstream fout;
    	string tname;
    	cout << "Enter the name of the file to change :" << endl;
    	cin >> tname; 
    	fout.open(tname.c_str());
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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