Alright, so I'm making my own little DND game, it's going very well so far and I've decided to create some sort of character saving feature, I figure I'll just do this in a simple text file for now. So, here's what I'm thinking, a while back I made some AI in Perl that wrote to a db file, but the syntax is so different between the two languages that I can't figure out what I'm trying to do anymore, ahha.
The char.txt is going to look like this:
charName, charLevel, str, dex, etc...
I need to find out how to find each of these values, I was originally thinking I could use a size_t and do a .find for each comma, separate values... Would this work?
Searching a file
Started by The Midnighter, Nov 26 2007 06:02 AM
2 replies to this topic
#1
Posted 26 November 2007 - 06:02 AM
|
|
|
#2
Posted 26 November 2007 - 09:20 AM
It should work.
#3
Posted 27 November 2007 - 01:05 PM
I lied. It's all good now, check it out:
char.txt looks like:
Output looks like:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string line,charClass,str,dex,hp,lvl,wepID,exp;
char filein[55];
char fileout[55];
string charName;
size_t pos;
size_t comma;
int main()
{
cout << "Enter your file name you want to search in (C:\\Blah.txt): ";
cin >> filein;
ifstream myfile(filein);
if (!myfile.is_open()) { cout << "File could not be opened from: " << filein << "\n"; }
else
{
while(! myfile.eof())
{
getline(myfile,dex,',');
getline(myfile,hp,',');
getline(myfile,str,',');
getline(myfile,exp,',');
getline(myfile,charClass,',');
cout << "Name: " << charClass << "\nHealth: " << hp << "\nStrength: " << str << "\nDexterity: " << dex << "\nExperience: " << exp << "\n";
}
myfile.close();
}
system("PAUSE");
return EXIT_SUCCESS;
}
char.txt looks like:
50,5000,500,10000,Red-Tipped Dragon
Output looks like:
Enter your file name you want to search in (C:\Blah.txt): C:\char.txt Name: Red-Tipped Dragon Health: 5000 Strength: 500 Dexterity: 50 Experience: 10000 Press any key to continue . . .


Sign In
Create Account


Back to top









