hello guyz!need help..
I have a txt file written like this

1,ben, 200.00
2,mike,300.00

I want to create a program that can search in the text file. I just only enter the ID number or the first number in the text file then the program will search that No. The name and amount will display after searching the No. Only line by line of searching..
Please help..
I need it very much in my assignment..
Thank you so much!!

You will have to read the file sequentially -- from start to finish -- until you find the line you want. Here is one solution to the problem, which may not actually work well if there are no spaces in each of the lines.

ifstream in("myfile.txt");
int id;
std::string name;
float amount;
char comma;
while( in >> id >> comma >> name >> comma >> amount )
{
    // do something
}

thanks sir, but i don't to view all the data. I just want to view the the data that I search through ID number..Please help..

You can't just view one piece of data without reading the entire file.

How to do that?
I am beginner of this language..
Please help me..

I already gave you the code in my previous post.

Ok, here is a better solution. It doesn't do everything you want but shows you how to read the file and split the lines up into individual fields.

#include <fstream>
#include <iostream>
#include <string>


using namespace std;

int main()
{
  ifstream in("textfile1.txt");
  int id;
  string name;
  string line;
  float value;

  if( in.is_open() )
  {
    // while not enf-of-file
    while( getline(in, line, ',') )
    {       
        id = atoi(line.c_str());
        getline(in, name, ',');
        getline(in, line, '\n');
        value = (float)atof(line.c_str());
        cout << id << " " << name << " " << value << "\n";
    }
  }
}
#include <fstream>
#include <iostream>
#include <string>
 
 
using namespace std;
 
int main()
{
  ifstream in("file.txt");
  int id, id_ko;
  string name;
  string line;
  float value;
 
  if( in.is_open() )
  {
    // while not enf-of-file
    while( getline(in, line, ',') )
    {       
        id = atoi(line.c_str());
        getline(in, name, ',');
        getline(in, line, '\n');
        value = (float)atof(line.c_str());
        cout << id << " " << name << " " << value << "\n";
    }
  }


cout<<"Enter Id Number:";
cin>>id_ko;
if (id_ko==id)
{
cout << id << " " << name << " " << value << "\n";
}else{
	cout<<"not found!";
}

}

this is the txt file:
1,ben, 200.00
2,mike,300.00
3, nahtan, 200.00
4, roy, 1000.00

sir i followed that codes, but when I entered the id of ben, the record of roy that will display.

Please help..thank you so much!!

you have to ask for the ID before reading the file. You have the cart before the horse. Then you have to remove that cout line that displays the data and replace it with a check for the ID that the user entered against the ID that was just read from the file.

#include <fstream>
#include <iostream>
#include <string>
 
 
using namespace std;
 
int main()
{
  ifstream in("file.txt");
  int id, id_ko,x;
  string name;
  string line;
  float value;

	cout<<"Enter Id Number:";
	cin>>id_ko;


 
  if( in.is_open() )
  {
    // while not enf-of-file
    while( getline(in, line, ',') )
    {       
        id = atoi(line.c_str());
        getline(in, name, ',');
        getline(in, line, '\n');
        value = (float)atof(line.c_str());
        cout << id << " " << name << " " << value << "\n";
    }
  }


  if (id_ko!=id){

cout << id << " " << name << " " << value << "\n";
}else{
cout<<"not found!";
}


}

how?please..
can I use strcmp?
Thanks!!!

>>can I use strcmp?

There is no need to use strcmp(). You can compare two std::string objects by their == operator

std::string s1 = "Hello";
std::string s2 = "World";
if( s1 == s2 )
{
   // blabla
}
#include <fstream>
#include <iostream>
#include <string>
 
 
using namespace std;
 
int main()
{
  ifstream in("file.txt");
  int id, id_ko,x;
  string name;
  string line;
  float value;
 
	cout<<"Enter Id Number:";
	cin>>id_ko;
 
 
 
  if( in.is_open() )
  {
    // while not enf-of-file
    while( getline(in, line, ',') )
    {       
        id = atoi(line.c_str());
        getline(in, name, ',');
        getline(in, line, '\n');
        value = (float)atof(line.c_str());
        cout << id << " " << name << " " << value << "\n";
    }
  }
 
 
  if (id_ko!=id){
 
cout << id << " " << name << " " << value << "\n";
}else{
cout<<"not found!";
}
 
 
}

What do u think of this code?can I use this to search of Id?
Thanks..

This is the correct code and it works fine.

#include <fstream>
#include <iostream>
#include <string>


using namespace std;

int main()
{

  int id, id_ko;
  string name,na;
  string line;
  float value,va;
 int    offset; // where it was found (or not (-1))


    cout<<"\n\n\n\t\tEnter Id Number:";
    cin>>id_ko;

 cout<<"\n\n\n\t\t";
  ifstream in("file.txt");
  if( in.is_open() )
  {
    // while not enf-of-file
    while(!in.eof() && getline(in, line, ',') )
    {       

         if ((offset = line.find(id_ko, 0)) == string::npos) {
            id = atoi(line.c_str());
            getline(in, name, ',');
            getline(in, line, '\n');
            value = (float)atof(line.c_str());


            if (id_ko==id){

                id_ko = id;
                na= name;
                va= value;
                break;          

            }
         }
    }
  }


    if (id_ko==id){
        cout<<"\n\n\n\t\tRecord Found   \n";
        cout << "\n\t\t Number:  " << id;
        cout << "\n\t\t Name:  " << name;
        cout << "\n\t\t Value:  " << value;
    } else {
        cout<<"\n\n\n\t\tRecord Not Found   \n";
    }

    system("pause>nul");
    return 0;
}

You are only 2 years too late.

line 26: you don't need to test for eof() because getline() will do that for you.

line 29: You are giving find() an integer intead of a string or char, so find() won't work. You need to convert that integer into a string before giving it to find(), or convert the first word in the line to an integer then compare the two integers.

Be a part of the DaniWeb community

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