2011-10-13 63 views
1

我有一个txt文件,其中包含名称,身份证号码,mobilenumber和位置以逗号分隔的行。 例如 罗比,7890,7788992356,123威斯敏斯特 汤姆,8820,77882345,124京士顿街 我的任务是找回如何操作使用C++ STL的txt文件HOmework

通过名称查找所有的员工的信息。 按ID查找员工的所有信息。 添加员工的信息。 更新员工的信息。

到目前为止我已经读取了该文件并将其存储在向量中。代码如下所示。 对于任务 1)按名称查找员工的所有信息。我将遍历矢量并打印包含名称的信息。我将能够做到这一点 2)在文本文件simialry我会寻找id并打印有关信息。 但我无能点3 & 4.

我通过分割CSV行成独立的字段,然后张贴下面

void filter_text(vector<string> *words, string name)  
{  
vector<string>::iterator startIt = words->begin();  
vector<string>::iterator endIt = words->end();  
if(!name.size())   
    std::cout << " no word to found for empty string "; 

while(startIt != endIt)  
{  
    string::size_type pos = 0;  
    while((pos = (*startIt).find_first_of(name, pos)) != string::npos)  
     std:cout <<" the name is " << *startIt<< end;  
    startIt++;  
} 
}  

int main()    
{    
// to read a text file  
    std::string file_name; 
    std::cout << " please enter the file name to parse" ;  
    std::cin >> file_name; 

    //open text file for input 
    ifstream infile(file_name.c_str(), ios::in) ; 
    if(! infile) 
    { 
    std::cerr <<" failed to open file\n"; 
    exit(-1); 
    } 
vector<string> *lines_of_text = new vector<string>; 
string textline; 
while(getline(infile, textline, '\n')) 
{ 
    std::cout <<" line text:" << textline <<std::endl; 
    lines_of_text->push_back(textline); 
} 
filter_text(lines_of_text, "tony"); 
return 0; 
} 
+2

这里有很多不完整的句子和想法。 –

+0

如何标记每行(以逗号分割)并将结果存储在'std :: map '中? –

+0

如果这是作业,请添加'作业'标签。 –

回答

1
#include <string> 
#include <iostream> 
#include <vector> 
#include <stdexcept> 
#include <fstream> 

struct bird { 
    std::string name; 
    int weight; 
    int height; 
}; 

bird& find_bird_by_name(std::vector<bird>& birds, const std::string& name) { 
    for(unsigned int i=0; i<birds.size(); ++i) { 
     if (birds[i].name == name) 
      return birds[i]; 
    } 
    throw std::runtime_error("BIRD NOT FOUND"); 
} 

bird& find_bird_by_weight(std::vector<bird>& birds, int weight) { 
    for(unsigned int i=0; i<birds.size(); ++i) { 
     if (birds[i].weight< weight) 
      return birds[i]; 
    } 
    throw std::runtime_error("BIRD NOT FOUND"); 
} 

int main() { 
    std::ifstream infile("birds.txt"); 
    char comma; 
    bird newbird; 
    std::vector<bird> birds; 
    //load in all the birds 
    while (infile >> newbird.name >> comma >> newbird.weight >> comma >> newbird.height) 
     birds.push_back(newbird); 
    //find bird by name 
    bird& namebird = find_bird_by_name(birds, "Crow"); 
    std::cout << "found " << namebird.name << '\n'; 
    //find bird by weight 
    bird& weightbird = find_bird_by_weight(birds, 10); 
    std::cout << "found " << weightbird.name << '\n'; 
    //add a bird 
    std::cout << "Bird name: "; 
    std::cin >> newbird.name; 
    std::cout << "Bird weight: "; 
    std::cin >> newbird.weight; 
    std::cout << "Bird height: "; 
    std::cin >> newbird.height; 
    birds.push_back(newbird); 
    //update a bird 
    bird& editbird = find_bird_by_name(birds, "Raven"); 
    editbird.weight = 1000000; 

    return 0; 
} 

显然不是员工,因为那会让你的功课太容易了。

+0

谢谢鸭帮助我。 BUT我只是想知道将函数find_bird_by_name将工作,如果我有多个相同名称的条目,我想检索所有的同名信息 – samprat

+0

Mope,我编码它只找到第一个匹配。要返回多个,它必须创建一个匹配的'std :: vector '和'push_back'所有的'''',然后返回'vector'。 –

+0

谢谢Mooing,我已经解决了这个问题。非常感谢大家帮助我 – samprat

0

开始我的代码填充一个结构与此数据

如:

struct Employee 
{ 
    std::string name; 
    std::string id_number; 
    std::string mobilenumber; 
    std::string location; 
}; 

std::vector<Employee> employees; // Note you dont need a pointer 

查看字符串方法find_first_of,substr和friends。

0

所以,首先,我不认为你应该将信息存储在一个字符串的矢量中。这种任务的全部要求使用的

struct employee { 
    int id; 
    std::string name; 
    std::string address; 
    //... more info 
}; 

和存储的员工的情况下,在

std::vector<employee> 

你看,使用存储的线条,搜索“威斯敏斯特”的战略,将净我罗比,因为他的文字确实包含了这个子字符串,但他的名字根本不是威斯敏斯特。将数据存储在员工结构向量中可以消除这个问题,并且可以使整个事情更加有序,结构化。

当然,你需要实际解析文件来获取信息到矢量中。我建议使用如下策略:

while(getline(infile, textline, '\n')) { 
    std::stringstream l(textline); 
    getline(l,oneEmp.name, ','); //extract his name using getline 
    l >> oneEmp.id; //extract his id 
    //extract other fields from the stringstream as neccessary 
    employees.push_back(oneEmp); 
} 

至于添加信息:当用户输入数据时,只需将其存储在您的员工向量中;当你需要更新文件时,你可以简单地用一个新数据文件覆盖原始数据文件,方法是打开它以写入&将数据转储到那里(这显然是一个相当浪费的策略,但对于学校作业(我假设它是学校作业))。