# Problem with the tutorials

This topic is 3728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well its been along time since I've been to the forums. I see that they have been beautifully updated. Nice job guys. Here's my problem though...I didn't see a forum for this so I'll post it in general. I'm not going to post the entire program as it is in the Writing to a File Tutorial that Digiben wrote.
fout.open(szName".txt");
Does not work. At all. I can't figure out why. I've tried everything I can think of and this code won't compile at all. I get a: expected ')' before string constant, and a no matching function to call to std::basic(blah blah blah).

##### Share on other sites
If szName is a std::string then to add the string ".txt" you have to write szName + ".txt" and not szName".txt"

##### Share on other sites
Still didn't work. I decided to use a character array to see if it would work since the error message says it will only accept a char. I know strings are just character arrays anyways, but it almost worked. Now the error message I get is "Expected primary expression before ']' token".

Here's the code. I have added iostream, fstream, and string preprocessors of course.

int main()												// A standard and plain main (hey, still rhymes!) :){														// Start of our program	ofstream fout;										// Here we create an ofstream and we will call it "fout", like "cout".														// "ofstream" stands for "output file stream".  That means we are sending data to a file.	string szLine ="";									// Lets create a string to hold a line of text from the file	string szWord = "";									// This will hold a word of text	string szName = "Adol";                             // This holds the players name	char cName[4];	cName[0] = 'A', cName[1] = 'd', cName[2] = 'o', cName[3] = 'l', cName[4] = '\0';	int health=100, gold = 75;							// We holds the players health and gold	fout.open(cName[]".txt");							// We call a function from our ofstream object called open().														// We just tell the function open() the file name we want to open or create.														// If the file doesn't exist, it will create it for us once we start writing to it.														// Below, we use fout just like cout.  Instead of writing to the screen though, it writes to our open file.	fout << "Player: " << szName << endl;				// This prints "Player: Adol" to our file	fout << "Health: " << health << endl;				// This prints "Health: 100" to our file	fout << "Gold: "   << gold   << endl;				// This prints "Gold: 75" to our file														// close() closes the file for us. (very important)	fout.close();										// We must always close the file after we are done with it.  It frees memory and make's it so we can access it again.	return 0;											// Return with zero problems}

##### Share on other sites
the open Method expects a const char* i think...

you can achive this by doing something like that:
string szName = "Adol";string tmpfileName = szName + ".txt"; //i hope this worksfout.open(tmpfileName.c_str());

The c_str() returns a const char* of the string Object so this should work.

hope it helps

PS: i didn´t try it and don´t have time to look it up so i might be wrong

[Edited by - Noobico on November 5, 2007 8:50:47 AM]

##### Share on other sites
Excellent. That works perfectly. Thanks for all the help.

##### Share on other sites
There's more truth in your title than you know.

But as for your real question:
The C++ way with std::string
#include <string>#include <fstream>...// your parameter, wherever it comes fromstd::string szName("c:\\Hello");// the real filenamestd::string filename(szName + ".txt");// open the filestd::ofstream fout(filename.c_str());// write somethingfout << "Hello World";

The C-ish way with char strings.
#include <string>#include <fstream>...// your parameter, wherever it comes fromconst char *szName = "c:\\Hello";// construct the filename, first make a real stringstd::string filename(szName);// then append the extensionfilename.append(".txt");// open the filestd::ofstream fout(filename.c_str());// write somethingfout << "Hello World";

File is opened when the ofstream is created.
The file is never explicitly closed - it'll happen once fout goes out of scope. Most of the time, this is best practice.

String manipulation is done using std::string class - char strings are bad no matter what.

fstream classes expect const char * parameter, which is conveniently provided by c_str().

##### Share on other sites
Quote:
 Original post by Dark_Glitch... char cName[4]; cName[0] = 'A', cName[1] = 'd', cName[2] = 'o', cName[3] = 'l', cName[4] = '\0';`

A side note: that line has a memory overrun - the cName only has 4 chars (0..3)
and you are overwriting the 5th byte, which is very bad and can cause many different effects, from endless string to a crashing process.

Also, many would consider it an ugly style to put several statements separated by a comma unless you have to (ie. in a "for" loop).

Iftah