Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating a text file from a string

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

715 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