Solved

Creating a text file from a string

Posted on 2009-04-08
2
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 how to clear a vector as well as how to detect empty vectors in C++.

624 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