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
std::fstream::open takes a const char* as an argument. You need to convert the std::string to const char* with it's (std::strings) c_str() function.
Advertisement
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.


It didn't give me an error, but the file wasn't created either. I don't understand why.
Quote:Original post by ChaosCommand
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.


It didn't give me an error, but the file wasn't created either. I don't understand why.


You might want to print the string [to make sure it was made correctly], and look around. If you're not using explicit paths, the 'current working directory' might not be what you think it is, especially if run from an IDE.
Quote:Original post by Telastyn
Quote:Original post by ChaosCommand
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.


It didn't give me an error, but the file wasn't created either. I don't understand why.


You might want to print the string [to make sure it was made correctly], and look around. If you're not using explicit paths, the 'current working directory' might not be what you think it is, especially if run from an IDE.



Everything works fine, it just simply doesn't create the file

I tried another example and instead used ofstream, and it worked, but fstream didn't work.

Why is that?

Can someone put this in there compiler, and tell me if it works. Everything for me compiles fine, but it just isn't creating the file.

I don't know if it is VS screwing up, or if it is something I'm doing wrong.

#include <iostream>#include <string>#include <fstream>#include <Shlwapi.h>using namespace std;int main(){	string str;	string fname;	const char *fnameExt = ".txt";	string fnameComplete;	cout << "Enter a file name: ";	cin >> fname;	// Extension added	fnameComplete = fname.append(fnameExt);	cout << fname << endl;	fstream database1;	database1.open(fnameComplete.c_str(), ios::in|ios::out);	cout << endl;	database1.close();	return 0;}
And how would I specify a directory to save the file, like the directory the program is running from, or a specific directory?
Is there a bug in fstream

Because I seriously cannot create a file without using ofstream or ifstream...
Or is it supposed to be that way, or is my version of VS screwed up, I really need to know, this is the most annoying problem I have ever had and my patience is running out.
remove the ios::in. I don't remember the details, but some flags don't like other flags...

This works for me:
#include <fstream>#include <iostream>#include <string>using namespace std;int main(){	string fn="moocow";	string ext=".moo";	string filename=fn+ext;	cout << fn << " + " << ext << " == " << filename << "\n";	fstream	f;	f.open(filename.c_str(),iostream::out);	f.close();}
Quote:From MSDN:
· ios_base::in becomes "r" (open existing file for reading).
· ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
· ios_base::out | app becomes "a" (open existing file for appending all writes).
· ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
· ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
· ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).

Note that it says "open existing file for reading and writing". So it will fail if the file doesn't already exist. You'll need to use a different mode for opening, or check to see if the file exists before hand, and create it if it doesn't.

//Open the file as input-onlydatabase.open(fnameComplete.c_str(), ios::in);if (!database.is_open()){  //if it doesn't already exist, open it in output-only  //to force it to be created  database.open(fnameComplete.c_str(), ios::out);}//Close it so that we can open it in read/write modedatabase.close();//Open it in read/write modedatabase.open(fnameComplete.c_str(), ios::in|ios::out);
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Quote:Original post by Agony
Quote:From MSDN:
· ios_base::in becomes "r" (open existing file for reading).
· ios_base::out or ios_base::out | ios_base::trunc becomes "w" (truncate existing file or create for writing).
· ios_base::out | app becomes "a" (open existing file for appending all writes).
· ios_base::in | ios_base::out becomes "r+" (open existing file for reading and writing).
· ios_base::in | ios_base::out | ios_base::trunc becomes "w+" (truncate existing file or create for reading and writing).
· ios_base::in | ios_base::out | ios_base::app becomes "a+" (open existing file for reading and for appending all writes).

Note that it says "open existing file for reading and writing". So it will fail if the file doesn't already exist. You'll need to use a different mode for opening, or check to see if the file exists before hand, and create it if it doesn't.

*** Source Snippet Removed ***



Nice, that worked, seems kinda strange that it just doesn't create it with both flags, but, at least it works now. Now I can finally start working on this thing and hopefully make progress.

Thanks for all the help.

This topic is closed to new replies.

Advertisement