Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ File management help

Started by 3dburns Nov 16, 2004 at 11:30 PM 3 replies 950+ views
Original Post
3dburns
3dburns
I'm writing a baseball game and I'm trying to manage the teams by having all the teams and their players in separate files. What I need to do is load the right file according to what team the player picks. For example if a player picks the Cubs, my program needs to automatically load C:/Teams/Cubs.txt. Same thing with the Astros , C:/Teams/Astros.txt. I thought that I could just pass a string of the teamd and create a string with the file name from that but when I try to load a istream I get an error. I'm assuming that an istream declaration can't have a string. Can anyone else think of a better way to load a corresponding according to what team the player picks?? Thanks
stormrunner
stormrunner
well, have you tried :
#include <iostream>#include <fstream>#include <string>void LoadTeamFile(std::string teamname){   // concatenate the ".txt" extension   // to the teamname - assuming teamname   // is in correct format   teamname += ".txt";   // open an ifstream   // you must use teamname.c_str() -    // ifstream only accepts char* s as   // args   std::ifstream teamFile(teamname.c_str());   // while not end of file   while(!teamFile.eof())   {      // read in data      // and assign it to some      // data structure   }}int main(int argc, char** argv){   std::string team;   std::cout << "Choose your team name : ";   std::cin >> team;   std::cout << endl;   LoadTeamFile(team);   // continue with program   return 0;}

as to where the file is stored (C:\Teams\ect) you can use a global or something for that.
cheers.
stormrunner
JWalsh
JWalsh
3dburns,

I'm not fully understanding what you're trying to do, perhaps you can post some code?

If you're trying to do what I think, the idea is solid. Just obtain the team name from the user, then form a string by concatenating the prefix + teamName + fileExtension.

For easier use of string manipulation, use std::string. You can use the std::string::c_str() function to return a const char* when you want to open the file. Here's some Pseudo-code

std::string teamName;// Get team name from user...std::string prefix = "C:/Teams/";std::string extension = ".txt";std::string fileName = prefix + teamName + extension;// Now open the file with the new namestd::ifstream file( teamName.c_str() );

This is just pseudo-code, but should give a you clear idea of what I'm saying. If I'm missing your question, please feel free to post some clarification code.
GameDev135
GameDev135
You should be able to just load a file based on a string name.....
For example....

#include <fstream>#include <iostream>int main(int argc, char* argv[]) {   int x;    char *p = "textfile.txt";    ifstream f;    f.open(p);     f >> x;    f.close();   cout << x << endl; // prints the first number in textfile.txt   return 0;}
Feel free to email me at NYYanks432@hotmail.com if you have any questions
3dburns
3dburns
[edit] Thanks that fixed it

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.