whats wrong with my ofstream?

This is a discussion on whats wrong with my ofstream? within the C++ Programming forums, part of the General Programming Boards category; Hi All, Im wondering why the bit of code isnt working ... Basically its a simple function that takes a ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Ottawa
    Posts
    18

    whats wrong with my ofstream?

    Hi All,
    Im wondering why the bit of code isnt working ...
    Basically its a simple function that takes a vector and a string. It is supposed to output the values in the vector to a. the screen and b. a file. It will write the vector's values to the screen alright but writes nothing to the file. What am I doing wrong?

    Many Thanks
    Code:
    string maketmpfile(vector<double> v, string &id)
    {
    	double num;
    	int i;
    
    	ofstream a_file;
    	if (id == "one"){
    		ofstream a_file ("dataone.txt");
    	}
    	else {
    		ofstream a_file ("datatwo.txt");
    	}
    	for (i=0; i < v.size(); i++) {
    		num = v.at(i);
    		cout<<num;
    		cout<<"\n";
    		a_file<<"num";
    	}
    	a_file.close();
    
    	return tmpfilename;
    
       return 0;
    
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    ofstream a_file;
    if (id == "one"){
        a_file.open("dataone.txt");
    }
    else {
        a_file.open("datatwo.txt");
    }
    Your code looks like it creates a new ofstream instance called a_file which due to scoping rules closes the file immediately once the closing brackets are reached (the ofstream object's destructor is called) and before any output statements get called.

    You should also test the stream to make sure it is open before writing to it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Ottawa
    Posts
    18
    Thanks hk_mp5kpdw,
    that did the trick.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The problem with your code is that you declared your ostream object twice:
    Code:
    ofstream a_file;
    if (id == "one"){
    	ofstream a_file ("dataone.txt");
    }
    The first declaration created a variable called a_file that was global to your function. The second declaration created a variable called a_file that was local to the braces surrounding it, i.e the if statement, and which hid the global variable. The local variable is constructed but goes out of existence as soon as you exit the block containing it, leaving you with an unassigned a_file global variable.

    You had the right intention by declaring your a_file variable outside of the if statement, but you mistakenly redeclared it inside the if blocks creating a new variable instead of making an assignment to the existing variable. You don't need to call open(), you just need to fix that problem.
    Last edited by 7stud; 11-07-2005 at 11:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM
  2. Having an ofstream in the private data of a class
    By robquigley in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2003, 01:52 AM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  4. Replies: 9
    Last Post: 06-06-2002, 07:03 PM
  5. Removing *Unchanged* Output File (ofstream) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2002, 06:47 PM

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