Solved

Creating a text file from a string

Posted on 2009-04-08
2
316 Views
Last Modified: 2012-05-06
Hello,

I'm an experienced VB.NET developer trying to work on some C++ console code.  

I'm trying to write to a text file, but it doesn't seem to be working.  I am getting the following error: "error C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const char *'"

I'm assuming it's telling me I can't use a String variable to do this...  So, how do I use a string variable to create the file name?
#include "StdAfx.h"

#include "Dictionary.h"

#include <iostream>

#include <fstream>

#include <string>

using namespace std;
 

bool myClass::CreateTextFile(string fileName)

{

	ofstream file_out("C:\\" + fileName + ".txt", ios::out);

	if (! file_out) 

		return false;
 

	return true;

}

Open in new window

0
Comment
Question by:Torrwin
2 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 24101070
The ofstream constructor takes a const char* as first parameter, not an std::string, so :
std::string filename = "C:\\" + fileName + ".txt";

std::ofstream file_out(filename.c_str(), ios::out);

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24101085
One of the ugly downsides that people have griped about for years. I keep wondering when C++0x is going to fix this?

You can try this 1-liner

ofstream file_out((string("C:\\") + fileName + ".txt").c_str(), ios::out);
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now