0

Using Dev-C++

Im a newbie to c++

Is it possible to save files using a variable as the filename.

What i want to do is setup a text based program like a mud and be able to go from room to room and have it display room discription and available exits.
Only way i can think to do this is have a file for each room and have the code open/read or write/close each time i enter a new room.

Problem is i cant work out how to set this up any help would be greatly appreciated.

I only need the syntax for opening a file with a variable

3
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by cikara21
0

Using Dev-C++

Im a newbie to c++

Is it possible to save files using a variable as the filename.

What i want to do is setup a text based program like a mud and be able to go from room to room and have it display room discription and available exits.
Only way i can think to do this is have a file for each room and have the code open/read or write/close each time i enter a new room.

Problem is i cant work out how to set this up any help would be greatly appreciated.

I only need the syntax for opening a file with a variable

you need to take the c++ string and turn it into a c string

example:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main() {
string fn;
ifstream fin;
cout << "enter file name: ";
cin >> fn;
fin.open(fn.c_str());
.
.
.
}

0
#include <iostream.h>
#include <fstream.h>
//...
char filename[255];
ifstream infile;
cout<<"File name : ";
cin.getline(filename,255);
infile.open(filename);
//...
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.