Appended an extension to filename, but now can't use with fstream,I really need help

Started by
18 comments, last by ChaosCommand 18 years, 9 months ago
Basicailly, I want a user to enter a file name, and then append an extension to the end of it.

#include <iostream>
#include <string>
#include <Shlwapi.h>
#include <fstream>

void database_editor_new()
{


        // Other code taken out to save space



	string fname;
	const char *fnameExt = ".txt";
        string fnameComplete;

	cout << "Enter a file name: ";
	
        cin >> fname;
	
	// Appends .txt to the end
        fnameComplete = fname.append(fnameExt);

	
        fstream database;

        // Problem is, is that the compiler complains when i try to use this
        // as a filename
	database.open(fnameComplete, ios::in|ios::out);
	// Error generated

        // I/O Stuff
	database.close();
	
	
}


//The Error:
Error	1	error C2664: 'void std::basic_fstream<_Elem,_Traits>::open(const wchar_t *,
std::ios_base::openmode,int)' :
 cannot convert parameter 1 from 'std::string' to 'const wchar_t *'	
f:\documents and settings\james mowery\my documents\visual studio 
2005\projects\tennis sim\tennis sim\database_editor.cpp	40





So I have three questions. 1. How do I convert the string fnameComplete to a char, and possibly get it working. 2. Is there an easier way to append a file extension to the end of a string, or an array of chars, and have it work with fstream, because this is annoying and holding me up for working on my database editor. lastly 3. My small tennis simulation game/engine is being worked on, and I need to write a database to tie it all together, I have the database planned out (for player attributes, name, and such), but I need to figure out a way to easily read the data and write out statistical data (have not planned that yet). I'm doing this in the command line, so I'm only going to be playing the game out with commentary (if any of you have played Championship Manager, or Football Manager as it is today, you'll know what I mean). So does anyone have any resources for managing databases for games, and reading and writing to them? After this, I hope to move over to an API and do it, maybe one day put together a 2D engine, and so on. Hopefully work my way up to a real football game engine.
Advertisement
fnameComplete.c_str()

Quote:Original post by Telastyn
fnameComplete.c_str()


That didn't really help... care to explain.

I tried putting it right after I assigned fnameComplete to the whole name, but I still got an error.

Error 1 error C2664: 'void std::basic_fstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *' f:\documents and settings\james mowery\my documents\visual studio 2005\projects\tennis sim\tennis sim\database_editor.cpp 40

I think it is the same one
I would try:
std::string fname;std::cout << "Enter a file name: ";std::cin >> fname;fname += ".txt";
Quote:Original post by Roboguy
I would try:
std::string fname;std::cout << "Enter a file name: ";std::cin >> fname;fname += ".txt";


Tried that, deleted all and built it around that.

again:
Error 1 error C2664: 'void std::basic_fstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *' f:\documents and settings\james mowery\my documents\visual studio 2005\projects\tennis sim\tennis sim\database_editor.cpp 42


database.open() is wanting something that it isn't getting, and the suggestions aren't changing it to what it wants. I'd tell you what it was, but I just don't understand MSDN.
MSDN States:
basic_filebuf *open(   const char *_Filename,   ios_base::openmode _Mode);


Parameters
_Filename
The name of the file to open.

_Mode
One of the enumerations in ios_base::openmode.

__________________________________________________________

So,
It wants a const char *_Filename.

So how do I make the filename into that, anyone know?
if something wants a const char* and you have a string you can use string::c_str()

for example
void foo(const char* text);string str = "something";foo(str); //error!foo(str.c_str()); //much better :)
At least for the thin-character version [char, not wchar] calling string.c_str() returns the internal char * version of the text. The error is just saying that fstream.open takes a char *, not a std::string.

Thus:

database.open(fnameComplete.c_str(),...


should pass the internal const wchar_t * representation of the text as parameter one, which is what the function wants.
Quote:Original post by cozman
if something wants a const char* and you have a string you can use string::c_str()

for example
void foo(const char* text);string str = "something";foo(str); //error!foo(str.c_str()); //much better :)



You completely lost me with that. Happen to know how I would get that to work with my code? I am sure it is simple, but, my attempts have failed.
Quote:Original post by Telastyn
At least for the thin-character version [char, not wchar] calling string.c_str() returns the internal char * version of the text. The error is just saying that fstream.open takes a char *, not a std::string.

Thus:

database.open(fnameComplete.c_str(),...


should pass the internal const wchar_t * representation of the text as parameter one, which is what the function wants.



I'll try that right now

This topic is closed to new replies.

Advertisement