I have been browsing the web for days now trying to find a solution to my problem and have yet to come up with anything so far so I figured I would ask some professionals.

Basically I need to read a text file which contains records along the lines of:

ClientNum LName FName
123456 XX YY
etc..
etc..

and then let the user input either a range of ClientNum's or one specific ClientNum and display all data for that one record. So far I have been using getline and have successfully displayed all records with a while loop but I cannot figure out the search function. By the way I need to use the BASICS of C++ so basically just loops. I will post my code for you to review and let me know if you can help out thanks!

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
   string getcontent;
   string search;
  
   cout << "Input: ";
   cin >> search;
   
    ifstream openfile ("TEST.txt");
    
     if(openfile.is_open())
     {

        while(!openfile.eof())
        {                   
          getline (openfile,getcontent);//
          cout << get content << endl; // these two lines alone in a while loop display ALL records
          
          
        
        if(search == getcontent ) // this was my theory for searching records but does not display anything to the console
         {
        cout << getcontent << endl;
         }
    
        }
        openfile.close();
    
     }
     else
         {
         cout << "File error\n";
         break;
         }
    

    
    
    
    
    system("PAUSE");
    return 0;
}
Re: Help with searching and displaying record from .txt file 80 80

In order to perform operations on your data, you need to load it in the memory in way it is convenient for you to use. If I were you, I would make a structure or class for that purpose - which can hold the data of one record. Then I would make an "array" of the structure, so I could store all the data from the file. I would use vector , or list from the stl , if you need to insert and remove elements frequently choose list, otherwise vectors are fine. Then read all the data from the file into the vector/list.

#include <vector>
#include <fstream>
#include <string>
#include <sstream>

struct Record
{
	std::string name, address;
	int client_number;
        /*
        .
        other stuff you need to store
        .
        */
};

bool init ( std::vector<Record>& );

int main ()
{
	std::vector<Record>clients;

	if ( ! init ( clients )  ) return -1;
	// if it fails to open the file the program returns with -1
	// since you wouldn't have any data to work with, there is no point
        // running the program any further, or you can check the size of the vector

	/*
	.
	. other function callings
	.
	*/

	return 0;
}

bool init ( std::vector<Record> &v )
{
    Record tmp; // to store each record
    std::ifstream in ( "TEST.txt" );
    std::string line;
    std::stringstream ss;  // to manipulate the string

    if ( !in ) return false;

    while ( getline ( in, line ) )
    {
        // I dont know exactly how  your file is arranged
        // but there must be a deliminator character somewhere between the records.
        // If not, count the lines you've read, and make a check each time you are
        // reading a new record. You can ensure you are reading the right data
        // by checking the line. - all records start with "ClientNum"

        // and depending on who your file looks like, you can extract the string with stringstream
        // ss << line;
        // ss >> tmp.client_number;
        // ss >> tmp.name;
        // or you can use getline ( ss, tmp.name, '\n' ); dependig who you want to extract the stream
        // etc.
        v.push_back ( tmp ); // and finally push back the data into the vector
    }

    in.close ();

    return true; // all went good, return true
}

After you have read all the data into the vector, you can work on it comfortably and implement all your functions.

Re: Help with searching and displaying record from .txt file 80 80

Thank you for your help! I used a string function .find() to read the client number

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.