2015-10-05 88 views
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(); 
} 

我有用于从C++文本文件中检索数据的代码。使用此代码我得到整个文件数据作为输出,如:使用C++从文本文件读取特定行

  name  address id 
      xxx  hyd  0001 
      yyy  hyd  0002 

但我通过接受从键盘输入需要这样的数据只有特定的线路输出。需要输出像:

  name  address id 
      xxx  hyd  0001 

这里我的输入是name。请任何人以这种方式帮助。

+0

是的,我需要修复它,但没有得到任何想法。 –

+1

键盘输入(ID,姓名,地址,行号)是什么? – sop

+0

使用std :: istream :: ignore,请参阅已存在的答案中的详细信息:http://stackoverflow.com/a/25012566/492336 –

回答

0

我将每行的值存储在这样的结构:

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

而且我会检查,然后在键盘输入,如果是这样的话,我打印线。首先请仔细阅读键盘输入,然后在文件中搜索。但你可以使用一个类,用阅读线的方法(它会更好)