|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
Do I need to set paths to files?
What I need to know is if in the following code I need to set a path to files, i.e. /path/to/users.dat.
I am a self taught programmer and mainly deal with PHP and CFML with MySQL and not C++. This is designed for a unix based system and designed to compile using g++. It updates the backend to an online game and is run every X minutes via cron. Here is the code: #include <string> #include <stdio.h> #include <string.h> #include <iostream.h> #include <fstream.h> #include <stdlib.h> #include <time.h> #include <math.h> #include "Lupdate.h" // WARNING: MAX structures[5] = 50000 Player::~Player() { delete []player_path; delete []island; delete []empire; } Player::Player(char *path) { char *new_path; player_path = new char[strlen(path)+1]; new_path = new char[strlen(path)+6]; strcpy(player_path, path); strcpy(new_path, path); strcat(new_path, "/main"); strcpy(most_powerful, "/home/httpd/cgi-bin/most_powerful.dat"); island = new char[5]; empire = new char[5]; // load player, and if inactive, delete player if(load_player(new_path)) { delete_player(path); } delete []new_path; } bool Player::load_player(char *path) { ifstream fin; string data_type; int activity, data, slashes, j, k, i; fin.open(path); if(fin.fail()){ cerr << "Unable to open file " << path << endl; exit(1);} //cout << "The file name is " << path << endl; //cout << "Player Path is " << player_path << endl; So every do I need to insert a /path/to/file.dat every time I see the word "path" or "*path"? Thank you, Kirk Fickert
__________________
Why? Because Forms just look cooler in OS X... Dutch, it's like German...but not! Last edited by kfickert : April 17th, 2003 at 10:54 AM. |
|
#2
|
||||
|
||||
|
From my understanding of your problem, if the file is in the same directory as your program, you need not specify the path to it. If it is in a different directory, but is relative to the directory of your program, you could use something like "./datadirectory/files.dat", or "../data/file.dat". If it is in some arbitrary directory, then you need to specify the full path to it. I believe that this should be the path to which the user running the prorgam can see, and not the absolute path. For instance, if you need to access "/home/user1/datadir/file.dat", and user1 is running the prorgam, and he sees /home/user1 as the root, then you would need the program to access "/datadir/file.dat", since if you put "/home/user1/datadir/file.dat", the program would think that this is a directory off of user1's root, which is: "/home/user1/home/user1/datadir/file.dat". So, it depends on what the user can 'see' as root.
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#3
|
||||
|
||||
|
thanks, that helps to clarify things a bit.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Do I need to set paths to files? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|