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

## Recommended Posts

in order to access a library of data I need to read information from specific files, but I need it to read the file pathname based on a variable. for example: char Name; //holds the filename to be gathered from the program somewhere then I need to read a pathname like this: "..\\data\\Name.file" I have no idea how to get it to input what's in the variable in that place so I'm coming to anyone here for help :)

##### Share on other sites
First off, you must specify what language you are programming in. In general, opening files is done by calling a function which handles most of the opening for you. You must simply supply it with the location, and the read/write type of the file. You wanted to insert the value of the variable as the pathname to your file? No problem. Below is C++ code:
#include <iostream>#include <fstream>using namespace std;int main () {  char Name = "place/somewhere/hi.txt";  ifstream fin;  //declare   fin.open (Name, ifstream::in);//opens the file at the location specified by Name  //do stuff with data here  fin.close();}

Hope that helps. Further help can be given if we know what language you are programming in.

##### Share on other sites
Quote:
 Original post by AdamGLFirst off, you must specify what language you are programming in. In general, opening files is done by calling a function which handles most of the opening for you. You must simply supply it with the location, and the read/write type of the file. You wanted to insert the value of the variable as the pathname to your file? No problem. Below is C++ code:*** Source Snippet Removed ***Hope that helps. Further help can be given if we know what language you are programming in.

Quickly! Before anyone sees! Use std::string instead of char. Or if you must, char const []. (hint: your code will not compile)

OP: Indeed, we need to know the programming language and perhaps even some more details about the problem. What have you tried that doesn't work?

jfl.

##### Share on other sites
ok let me be more clear. I'm using c++ they are char[32] variables used to store a players name for their data file so when loaded it will read the file path as
"..\\Data\\PlayersName.dat" so I need to somehow slap the char[32] where "PlayersName" is.

##### Share on other sites
Are you trying to do this?

std::string Name;cin >> Name;string path = "..\\data\\" + Name + ".file";fstream myfile ( path.c_str(), ios::in );

Or

std::string Name;cin >> Name;fstream myfile ( Name.c_str(), ios::in );string path;getline ( path, myfile, "\n" );string path2;copy ( path.find_first_of( "\"" ), path.find_last_of ("\""), path2 );

sorry, but your statement is still kinda unclear as to what you are looking to do.

##### Share on other sites
Quote:
 Original post by Shenjokuok let me be more clear. I'm using c++ they are char[32] variables

Don't use char[]s (or even char*s) if you're using C++. Instead, use std::strings. They're much easier, as you'll see.
Quote:
 used to store a players name for their data file so when loaded it will read the file path as"..\\Data\\PlayersName.dat" so I need to somehow slap the char[32] std::string where "PlayersName" is.

Consider the following, that does exactly what you want:
#include <string>#include <iostream>int main( ){	std::string strPath = "../Data/PlayersName.dat";	const std::string strToReplace = "PlayersName";	std::string strPlayerName = "jouley";	strPath.replace(strPath.find(strToReplace, 0), strToReplace.size( ), strPlayerName);	std::cout << strPath << std::endl;	return 0;}

The one long line could have been broken up into smaller ones, but you'd be hard pressed to do anything so easily without a String library with the capabilities of std::string.

Hooray for std::strings! Boo for chars!
-jouley

[Edit: Pretty much what KulSeran said, just another approach.]

##### Share on other sites
Quote:
 Original post by Shenjokuok let me be more clear. I'm using c++ they are char[32] variables used to store a players name for their data file so when loaded it will read the file path as"..\\Data\\PlayersName.dat" so I need to somehow slap the char[32] where "PlayersName" is.
In C++ you would use std::string, along with std::ifstream as demonstrated in AdamGL's code. A very simple example:

std::string path = ".../Data/";path += player_name; // player_name could be a std::string, or a null-terminated C-style stringpath += ".dat";std::ifstream file(path.c_str());assert(file); // More sophisticated error checking goes here...// Do stuff with file...// File is closed automatically when 'file' goes out of scope

[Edit: So very slow...]

##### Share on other sites
see I would be using string except for some reason my compiler decided that no matter, string header file or not, it won't read string as a type of storage class. I have no idea why. I include the header file and everything but it still says string doesn't exist when I try to use it. hence all the chars.

##### Share on other sites
Quote:
 Original post by Shenjokusee I would be using string except for some reason my compiler decided that no matter, string header file or not, it won't read string as a type of storage class. I have no idea why. I include the header file and everything but it still says string doesn't exist when I try to use it. hence all the chars.

What compiler, and what are the errors?

##### Share on other sites
Quote:
 Original post by Shenjokusee I would be using string except for some reason my compiler decided that no matter, string header file or not, it won't read string as a type of storage class. I have no idea why. I include the header file and everything but it still says string doesn't exist when I try to use it. hence all the chars.

Are you *sure* the compiler knows you are using C++ (hint: some compilers will base the language off the file extension, .c vs .cpp )

Are you #include'ing <string> (and not string.h)

Are you typing std::string?

Ths code should work on all valid c++ compilers:
// source.cpp#include <string>int main(){    std::string str;}

If that doesn't work on your compiler, post

1) compiler name
2) the errors (hint: if you ever have a programming problem, post the errors)

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 18
• ### Forum Statistics

• Total Topics
632161
• Total Posts
3004505

×