Thread: Another newbie Question>Ifstream, Ofstream, DamnStream.

  1. #1
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Another newbie Question>Ifstream, Ofstream, DamnStream.

    Hey there, I'm back again.
    Well, I've finally finished my c++ book but it leaves LOTS of things unanswered such as how to work with files so I'm kind of winging it from other source code I've been looking at over the past few days.
    What I'm now trying to do is to make a settings file, well, for now just a text file that is made based on the last line of a record file. Kind of a loop but with files?, anyways, here's the code I've made and if anyone would be kind enough to tell me what I'm doing wrong it would be appreciated!

    Code:
    #include "stdafx.h"
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    #using <mscorlib.dll>
    using namespace System; // for later
    using namespace std;
    
    int main(int argc,char *argv[])
    {
    	char* Title = "data_";
    	char* Ext = ".dat";
    	int i = 1;
    
    	//Make fileTitle
    	ofstream fileTitle;
    	fileTitle.open ("file.log");
    	fileTitle << Title << i << Ext << "\n";
    	fileTitle.close();
    
    	//Read fileTitle
    	string line;
    	string fn;
    	ifstream fileTitleA ("file.log");
    	getline (fileTitleA,line);
    	cout << line << endl;
    	fn = line;
    	fileTitleA.close();
    
    	//Make file based on file.log's contents (Can't get this to work)
    	ofstream makeFile;
    	makeFile.open (fn);		//Pointer or static_cast here?
    	makeFile << fn << "\n";
    	makeFile.close();
    
    	cout << "\n" << "fn = " << fn << "\n";	// Just for test purposes
    
    	system("pause"); // should be using cin.get() I know, just found out :)
        return 0;
    }
    As you can see there is errors with the makeFile.open(fn). I know (think) I have to use string for this but I get errors that it can't convert std::char amongst others if I try to change it.

    Also, Now that I've finally finished basic c++
    (and if/ofstream wasn't included, or how to use any of the libraries) so, where do I go now? I can't find any books and very much NEED to learn more!

    Thanks for your help,

    Rob Sitter
    <<The not so newbie newbie lol>>

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the first argument to open() method is const char* and you are sending it std::stream. use the string's c_str() method to send const chr*
    Code:
    makeFile.open (fn.c_str());

  3. #3
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    'Twas all too simple :)

    Thanks, it worked

    Oh, now, I just realized, that little string you replied with opens lots of doors for me, thanks soo much!

    I've really got to find a book on these commands.

    Thanks again,

    Rob Sitter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  2. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 01:10 PM
  3. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 10:06 PM
  4. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM
  5. Removing *Unchanged* Output File (ofstream) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2002, 07:47 PM