0
#include <iostream.h>

#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>
int view(void)
{ 
  ifstream in("NewFaculty.txt");

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  char str[255];

  while(in) {
    in.getline(str, 255);  // delim defaults to '\n'
    if(in) cout << str << endl;
  }

  in.close();
  getch();
  return 0;
}

int main()
{
  view();
}

I have this code for retrieving data from text file in C++. Using this code I am getting entire file data as output, like:

          name     address    id
          xxx      hyd       0001
          yyy      hyd       0002

But I need output like only particular line of data by accepting input from keyboard. Need output like:

          name     address    id
          xxx      hyd        0001

Here my input is name. Please anyone help in that way.

Biffen
  • 5,354
  • 5
  • 27
  • 32

1 Answers1

0

I would store the values of each line in a structure like this:

struct RecordLine
{
   std::size_t lineNo; // optional
   std::string name;
   std::string address;
   std::string id;
};

And I would check then the keyboard input, and if so, then I print the line. Be careful to first read the keyboard input, then search in the file. But you can use a class, with methods for reading the line (it would be nicer)

sop
  • 3,025
  • 6
  • 34
  • 79