In our class we made a program that writes your name and such to a file. Now we need to search for a file by the person's last name. I've looked around and havent been able to find any help. Can anyone help?
In our class we made a program that writes your name and such to a file. Now we need to search for a file by the person's last name. I've looked around and havent been able to find any help. Can anyone help?
How do you write to the file (the format, etc)?
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
it was a sequential record
Details, please. We need to know the exact content of the file that you're trying to read. Is it like ...
EDIT: Read this thread for some inspirationCode:Susan Deli Jack Nicholson Bob Hammer .....(and so on), or Susan Deli | Jack Nicholson | Bob Hammer | ....(and so on), or whatever format it is
Last edited by alphaoide; 01-22-2004 at 02:32 PM.
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
First name
Last name
Address
Phone number
Assuming you write in text mode,
You still need to add/make changes to read only last-name section of the file. It's a class assignment anyway, right?Code:ifstream inFile("data.txt"); bool found = false; string keyName = "Chan"; // last name to be found string lastName; while(getline(inFile, lastName) && found = false) { if (lastName == keyName) { found = true; } if (lastName > keyName) break; }
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
yeah its for class but our teacher doesnt help me and 3 other people because we are so far ahead.
Well, at least he/she is better than MY teacher. Who goes: "Sup gurls, need some help? " (and simply type up the whole program for the gurls) ; and " HEY, YOU GUYS, wat r u doing on the internet? Wat? Your stuck? THINK 'BOUT IT"Originally posted by TeamGreen
yeah its for class but our teacher doesnt help me and 3 other people because we are so far ahead.
An Unofficial Cristiano Ronaldo Website : Ronaldo 7
Best to treat every four lines as a single entity as long as this formatting is consistent. It simplifies your program greatly.First name
Last name
Address
Phone number
Code:struct record { string first; string last; string address; string phone; }; ... // // Retrieve a record from file (very simple impl.) // bool get_record(record& r, ifstream& fin) { getline(fin, r.first); getline(fin, r.last); getline(fin, r.address); getline(fin, r.phone); if (!fin.good()) return false; return true; } ... ifstream fin("file.dat"); record r; if (!fin.is_open()) { // Handle file open failure } while (get_record(r, fin)) { if (r.last == search_key) { // Process successful search } }