Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating a text file from a string

Posted on 2009-04-08
2
Medium Priority
?
386 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

876 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