Solved

Creating a text file from a string

Posted on 2009-04-08
2
325 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

910 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

22 Experts available now in Live!

Get 1:1 Help Now