Jump to content

Searching a file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts
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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It should work.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
The Midnighter

The Midnighter

    Newbie

  • Members
  • PipPip
  • 14 posts
I lied. It's all good now, check it out:

#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 . . .