problem;
im currently in highschool, and the school uses an older version of visualc++ that does not support strings with all of the functions. the function im talking about is the one when you try to "open" a file to write onto it.
what i need to do is find a way to convert "strfilename = strName + ".sav";" into a character array capable of useing the function "ofstream outf(strfilename);"

note: when i change strName and strfilename into a character array, i get an error about two pointers not able to join together.

anyone have any advice??

P.S. sorry if this post does not make much sense, i'm trying my hardest to explain the situation

I think you described it very clearly. What I don't understand is whether or not you have access to the functions in the C++ <string> header (or even <cstring> also named in older versions <string.h>).

im using a different, most recent version of visual studio on my home desktop... but as far as i remember, the school version does

I believe the method yourstringhere.data() behaves like a character array in situations like this. I thought originally you meant that you wanted to concatenate those two strings filename+".sav" but you mean that you want to convert a string to char array?

i need to make the variable "filename"(a character array of your name) and the string ".sav" into a one whole character array, so that i can use it with the older versions on c++, since the older versions don't completely support strings.

however, what you previously said seems to work with my current 2010 version, so i can only hope that it will work with the visual studio 6.0 version the school has....
thanks a lot, you're a life saver....and you just proved my teacher is a idiot xD

Edited 7 Years Ago by jonathanYoung: n/a

Before I answer, can you show how you tried to do this, so I can get
a better feel of what you are trying to achieve.

all right, i believe the only way is to actually post the code.
i don't think its TOO long;

note: i have a function created to change a string to a integer value
note: my include statemaents for the whole project are as follows:
include <cstdio>
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <fstream>
#include <string.h>

a lot, i know xD

void DiskFileIO()
{
	cout << "do you want to save, or load?\n";
	cout << "1)save \n";
	cout << "2)load\n";
	cin >> intChoice;
	string strfilename;
	strfilename = strName + ".sav";
	

	if(intChoice == 1)
	{

		cout << "your file name is: " << strfilename << endl;
		cout << "saving....\n";



		ofstream outf(strfilename.data());

		outf << intAttack << endl;	//character attack
		outf << intDefence  << endl;//defence of the character
		outf << intMaxHealth << endl;	//the maxmimum health
		outf << intHealth << endl;  //current health
		outf << intMaxSP << endl;//the maximum SP	
		outf << intSP << endl;//the current SP
		outf << intLevel << endl;//the current character level
		outf << intExp << endl;//for determining the level
		outf << intExpNext << endl;//experience needed for the characters next level
		outf << intCurrCash << endl;//the current amount of money your character holds
		outf << dblSpellDmg << endl;//innitializes how much damage the spell is going to do
		outf << intInventory[0] << endl;
		outf << intInventory[1] << endl;
		outf << intInventory[2] << endl;
		outf << intInventory[3] << endl;
		outf << intInventory[4] << endl;
		outf << intWeaplevel << endl;//the current level of your weapon
		outf << intArmorlevel << endl;//the current level of your armor
		outf << intAccesorylevel << endl; //the current level of your accesories


		outf.close();
	}

	else if(intChoice == 2)
	{
		cout << "your file name is " << strfilename << endl;
		cout << "loading\n";

		ifstream inf(strfilename.data());

		if (!inf)
		{
			cerr << "Uh oh, your file does not exist could not be opened for reading!" << endl;
			exit(1);
		}
		
		getline(inf, strstring);
		intAttack = GetIntVal(strstring);
		getline(inf, strstring);
		intDefence = GetIntVal(strstring);
		getline(inf, strstring);
		intMaxHealth = GetIntVal(strstring);
		getline(inf, strstring);
		intHealth = GetIntVal(strstring);
		getline(inf, strstring);
		intMaxSP = GetIntVal(strstring);
		getline(inf, strstring);
		intSP = GetIntVal(strstring);
		getline(inf, strstring);
		intLevel = GetIntVal(strstring);
		getline(inf, strstring);
		intExp = GetIntVal(strstring);
		getline(inf, strstring);
		intExpNext = GetIntVal(strstring);
		getline(inf, strstring);
		intCurrCash = GetIntVal(strstring);
		getline(inf, strstring);
		dblSpellDmg = GetIntVal(strstring);
		getline(inf, strstring);
		intInventory[0] = GetIntVal(strstring);
		getline(inf, strstring);
		intInventory[1] = GetIntVal(strstring);
		getline(inf, strstring);
		intInventory[2] = GetIntVal(strstring);
		getline(inf, strstring);
		intInventory[3] = GetIntVal(strstring);
		getline(inf, strstring);
		intInventory[4] = GetIntVal(strstring);
		getline(inf, strstring);
		intWeaplevel = GetIntVal(strstring);
		getline(inf, strstring);
		intArmorlevel = GetIntVal(strstring);
		getline(inf, strstring);
		intAccesorylevel = GetIntVal(strstring);
	}

}

this is compatible with the 2010 version of visual studio, what i'm trying to do is make it compatible with the visual studio 6.0 version(not as function able with strings)

Edited 7 Years Ago by jonathanYoung: n/a

again, thanks, i did not know you could concat strings that way,
i thought it didn't go much farther than strstring + strstring2

strfilename.data() returns a pointer. Instead, use strcpy and c_str to convert to c string.

This article has been dead for over six months. Start a new discussion instead.