C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 8th, 2013, 02:49 AM
Fares123 Fares123 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2013
Posts: 1 Fares123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 32 sec
Reputation Power: 0
File IO - Search Function

I've been looking for so long trying to figure out how to get this thing to work. All i need to do is ask user to input a name and then it brings out the line from the .txt file containing the information.

For example in my case I'm doing a member search function I'm required to ask user to input the name of the customer and then print out all the details (which consumes 1 text line in the .txt file)

Here is the code, any help would be greatly.. GREATLY appreciated!

This is the write to text file method (100% working)

Code:
cout << "Customer Name: ";
cin >> name; 

// ...

ofstream myfile("customer.txt", ios::app);

// ...

myfile << " ID: " << id << " Name: " << name << " NICN : " << nicn << " gender : " << gender << " Day : " << day 
       << " Month : " << month << " Year : " << year << " Status : " << status << " Charges : " << charges << endl;


My search function..:

Code:
std::string search_customer(const std::string& name, const char* path = "customer.txt")
{

	cout << "enter name: ";
	getline(cin, name);

	std::ifstream myfile(path);
	std::string line;

	while (getline(myfile, line))
	{
		std::istringstream stm(line); // create an input string stream which reads fronm the line

		std::string token;
		while (stm >> token && token != name_prefix); // read upto, and including, prefix

		std::string str_name, str_suffix; // the next token is the name
		if (stm >> str_name && str_name == name && stm >> str_suffix && str_suffix == name_sufffix)
			return line;
	}

	std::cerr << "look up of name failed\n";
	return "";


Any help with fixing it would be greatly appreciated.. thanks

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > File IO - Search Function


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap