Hi everyone at daniweb!
First off I want to say thanks for starting this awesome homepage, though googleing ive found many clues and answers in here for my projects, things that were left undocumented or just poorly explained by those books that ive used.
But now, onto the issue!
What I'm trying to do is to create a tool which processes/reads a user database where I then store the result into another file.
Explaining this straight off and step-by-step is somewhat complicated for me, so to give you a bigger understanding of the issue I'm going post the structure of my "users.txt" database, and then explain how I wish to process it.
So my users.txt which contains information looks somewhat like this:
(txt tag didn't display the text correctly (it removed all the whitespaces and put everything on a single line- that's why i'm using code tag))
::::::::
Jenny
::::::::
Female
24 years old
Married
School teacher
::::::::
Jake
::::::::
17 years old
Student
Male
Single
Dog owner
::::::::
Katie
::::::::
Police officer
Single
32 years old
Female
Cat owner
Then what I'd like to do and have searched everywhere for, was to have my program/tool to return a certain result after i give it a specific keyword, for instance at this list, it might be interesting for me to know how many females I have registered and what their names are- So if I were to give the keyword "female" the program should process the stream, find the line "female" and then "rewind"/step back till it reaches the name of the woman, like, go back till "::::::" is found then take another step back and read the whole line and store this to a newly created file called female.txt, then continue looking though the file till it reaches EOF.
Also I'd like the tool to be able to update our female.txt file but in that way it wouldn't create duplicates of an already existing name, like comparing strings but- and i might be asking for the impossible now, in direct access with the file streams, somethings like if (usersIn.name == usersOut.name) cout << "duplicate entry! ignoring name!" << endl;
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
using namespace std;
int main(void)
{
ifstream inUsers("users.txt", ios::in);
if (!inUsers)
{
cout << "The file wasn't found!" << endl;
// we _must_ have a users.txt in order to run the program properly
exit(1);
}
else
{
cout << "File found!" << endl;
}
// This part may look weird and require some explaining, so..
// First, if the file doesn't exist, I wish to generate a new one with NO duplicate entry check
// Second, if the file DOES exist, then I wish to read the file and compare strings
ifstream outFemales("females.txt", ios::in);
if(!outFemales)
{
cout << "The file wasn't found!" << endl;
outFemales.close();
cout << "We shall now generate the file for writing!" << endl;
ofstream outFemales("females.txt",ios::app);
// <code that reads though users.txt and stores the names to females.txt goes here>
}
else
{
cout << "The file was found!" << endl;
outFemales.close();
cout << "We shall now re-open the file for writing!" << endl;
ofstream outFemales("females.txt",ios::in|ios::app);
// <code that compare filedata with users.txt and stores new names goes here>
}
getch();
return 0;
}
Basically that's where I'm at right now and I'd like to let you know that it's not a case like "I don't understand anythings! please please please do this for me!" because it simply wouldn't benefit me, I'm trying to learn as much as possible so that I hopefully in the future can develop both good games and useful applications but ive looked everywhere and I haven't found anything that describes or any book which explains anythings close to what I'm trying to achieve, so- that's why I'm turning here, hopefully someone of you wise men and/or women can shed some light on this matter and help me understand what ive missed, or if you know any good site or book which has the documentation i'm looking for, I'd love it if you could share it D: :)