2013-04-06 49 views
1

我有这个功能,可以根据你选择看哪个扫描我的文件,并打印出一张预先指定的记录:打印出从文件中的信息通过设计的另一个功能循环搜索的文件 - C++

void addressBook::showRecord(int pickNum) { 
    PEOPLE2 p; 
    ifstream indata("vectortest.dat", ios::binary); 
    if(!indata) { 
    cout << "Error opening file for reading " << endl; 
    exit(0); 
    } 
    indata.seekg(pickNum * sizeof(PEOPLE2)); 
    indata.read(reinterpret_cast <char*> (&p), sizeof(PEOPLE2)); 
    cout << p.fName2 << " " << p.lName2 << " " << p.Address2 << " " << endl; 
} 

所以你所要做的就是将showRecord放入main中,然后选择你想打印的名字。说我想看看存储的第二个名字,我会把在

newBook->showRecord(1); 

这就是所有罚款的花花公子,和它的作品完美,但如果我想走得更远一点什么。所以我创建了另一个函数,它可以使用showRecord打印出存储在我的文件中的地址簿中的所有名称。我尝试这样做:

void addressBook::showAll() { 
    ifstream indata("vectortest.dat", ios::binary); 
    for(int i = 0; i < indata.end; i++) { 
    showRecord(i); 
    } 
} 

和它的作品,但只打印出这很难编码到我一个人结构从以前分配的东西:

addressBook *newBook = addressBook::newbookInst(); 

PERSON me[] = {{"First" , "Last", "ADDRESS"}, {"John", "Doe", "1234"}}; 
        newBook->addPerson(me[1]); 

        newBook->addPerson(me[0]); 

这只是奇怪怎么一回事,因为当我进入文件本身,我可以看到所有添加的名称。

那么如何使用它,以便它实际上打印出文件中的所有内容,而不仅仅是永久存储的两个条目?

这是我addressbook.h和addressbook.cpp代码的情况下,你需要一个更深入的了解怎么回事...

\\ addressbook.h ////////

#ifndef _ADDRESSBOOK 
#define _ADDRESSBOOK 
#include <fstream> 
#include <vector> 
#include<string> 
using std::string; 
#include <iostream> 
using namespace std; 

using std::istream; 
using std::ostream; 
namespace CJ 
{ 
const int MAXADDRESS =25; 

struct PERSON 
{ 
    string fName; 
    string lName; 
    string Address; 
}; 
struct PEOPLE2 
{ 
    char fName2[25]; 
    char lName2[25]; 
    char Address2[25]; 
}; 

class addressBook 
{ 
private: 

    vector<PERSON> people; 

    int head; 
    int tail; 

public: 

    addressBook(); 

    addressBook(const PERSON &p); 
    addressBook(const PERSON p[], int size); 
    addressBook(char *fName, char *lName, char *address); 
    bool addPerson(const PERSON &p); 
    bool sortcomp(const PERSON& p1, const PERSON& p2); 
    bool getPerson(PERSON &p); 
    bool findPerson(const string& lastName, PERSON& p); 
    bool findPerson(const string& lastName, const string& firstName, PERSON& p); 
    void bubbleSort(int *array,int length); 
    void printBook(); 
    void sort(); 
    void waitKey(); 
    static addressBook *newbookInst(); 
    static addressBook *tempNew; 
    static PERSON *p(); 
    static PERSON *temPerson; 
    void showRecord(int pickNum); 
    void writeRecord(); 
    void showAll(); 


    friend ostream &operator << (ostream &, addressBook &); 
    addressBook operator =(const string& str); 
    addressBook &operator +=(const PERSON &p); 
    addressBook operator [](int x); 

}; 
} 
#endif 

\\ addressbook.cpp /////

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <conio.h> 
#include <string> 
using std::string; 
#include "addressBook.h" 
#include "menu.h" 
namespace CJ 
{ 
addressBook::addressBook() 
    : head(0), tail(-1) 
{ 

} 

addressBook::addressBook(const PERSON &p) 
    : head(0), tail(-1) 
{ 
    addPerson(p); 
} 

addressBook::addressBook(const PERSON p[], int size) 
    : head(0), tail(-1) 
{ 
    for(int i = 0; i < size; i++) 
     addPerson(p[i]); 

} 

addressBook::addressBook(char *fName, char *lName, char *Address) 
    : head(0), tail(-1) 
{ 
    PERSON tmp; 
    tmp.fName = fName; 
    tmp.lName = lName; 
    tmp.Address = Address; 
    addPerson(tmp); 
} 

bool addressBook::addPerson(const PERSON &p) 
{ 
    people.push_back(p); 

    if(tail == -1) 
     tail++; 
    return true; 
} 

bool addressBook::getPerson(PERSON &p) 
{ 
    if(tail >=0) 
    { 
     if(tail >= people.size()) 
      tail = 0; 
     p = people[tail]; 
     tail++; 
     return true; 
    } 
    return false; 
} 
bool addressBook::findPerson(const string &lastName, PERSON &p) 
{ 
    for(size_t i = 0; i < people.size(); i++) 
    { 
     if(people[i].lName == lastName) 
     { 
      PERSON *p = addressBook::p(); 
      *p = people[i]; 
      return true; 
     } 
    } 
    return false; 
} 
bool addressBook::findPerson(const string &lastName, const string &firstName, PERSON &p) 
{ 
    for(size_t i = 0; i < people.size(); i++) 
    { 
     if(people[i].lName == lastName && people[i].fName == firstName) 
     { 
      PERSON *p = addressBook::p(); 
      *p = people[i]; 
      return true; 
     } 
    } 
    return false; 
} 


void addressBook::printBook() 
{ 
    for(size_t i = 0; i < people.size(); i++) 
    {  
     std::cout << people[i].fName << "\t" << people[i].lName << "\t" << people[i].Address << std::endl; 
    } 
} 


bool addressBook::sortcomp(const PERSON& p1, const PERSON& p2) 
{ 
    int result = (p1.lName.compare(p2.lName)) ; 
    if (result > 0) 
     return true ; 
    if (result < 0) 
     return false ; 
    return (p1.fName.compare(p2.fName)) > 0 ; 
} 

void addressBook::sort() 
{ 
    bool didSwap ; 
    do 
    { 
     didSwap = false ; 

     for (unsigned i=1; i<people.size(); ++i) 
      if (sortcomp(people[i-1], people[i])) 
      { 
       std::swap(people[i-1], people[i]) ; 
       didSwap = true ; 
      } 

    } while (didSwap) ; 
} 

addressBook &addressBook::operator +=(const PERSON &p) 
{ 
    addPerson(p); 
    return *this; 
}; 

addressBook addressBook::operator [](int x) 
{ 
    return people[x]; 
}; 

ostream &operator << (ostream &output, addressBook &ab) 
{ 
    PERSON tmp; 
    ab.getPerson(tmp); 
    output << tmp.fName << " " << tmp.lName << " " << tmp.Address << endl; 
    return output; 
} 

addressBook * addressBook::tempNew = NULL; 
addressBook *addressBook::newbookInst() 
{ 
    if(tempNew == NULL) 
    { 
     tempNew = new addressBook; 
    } 
    return tempNew; 
} 

PERSON * addressBook::temPerson = NULL; 
PERSON *addressBook::p() 
{ 
    if(temPerson == NULL) 
    { 
     temPerson = new PERSON; 
    } 
    return temPerson; 
} 
bool status; 
char lName[50]; 
char fName[50]; 

void addressBook::writeRecord() 
{ 

PEOPLE2 temp; 
ofstream outFile("vectortest.dat", ios::app); 
    if(!outFile) 
    { 
     cout << "Error opening file for writing " << endl; 
     return; 
    } 

     for (vector<PERSON>::iterator iter = people.begin(), end = people.end(); iter != end; ++iter) 
     { 
     strncpy(temp.fName2, iter->fName.c_str(), 25); 
     strncpy(temp.lName2, iter->lName.c_str(), 25); 
     strncpy(temp.Address2, iter->Address.c_str(), 25); 
     outFile.write(reinterpret_cast<const char *>(&temp), sizeof(PEOPLE2)); 
     }  
    outFile.close(); 
} 


void addressBook::showRecord(int pickNum) 
{ 
PEOPLE2 p; 

ifstream indata("vectortest.dat", ios::binary); 
    if(!indata) 
    { 
     cout << "Error opening file for reading " << endl; 
     exit(0); 
    } 

    indata.seekg(pickNum * sizeof(PEOPLE2)); 
    indata.read(reinterpret_cast<char *>(&p), sizeof(PEOPLE2)); 
    cout << p.fName2 << " " << p.lName2 << " " << p.Address2 << " " << endl; 

    indata.close(); 

} 

void addressBook::showAll() 
{ 
ifstream indata("vectortest.dat", ios::binary); 

for(int i = 0; i < indata.end; i ++) 
{ 
    showRecord(i); 
} 

} 



} 
+0

你是什么意思,“它只打印出硬编码到我的PERSON结构从以前的任务”? AND,什么是'indata.end'?这不是记录的数量,是吗? – Arun 2013-04-06 23:49:02

+0

我会在主帖子中弹出该代码,以显示它在做什么 – 2013-04-06 23:53:16

+0

另外,您会发布“PEOPLE2”的声明吗? – Arun 2013-04-06 23:54:44

回答

0

我真的不知道很多关于你计划如何使用您的地址簿类上下文。但是我所做的就是将你所拥有的基本结构重新加入到更加习惯的C++中。这应该有助于指导你在这堂课上的未来工作。从文件问题中读取您遇到问题的问题已得到解决。

#include <iostream> 
#include <iterator> 
#include <fstream> 
#include <cstdlib> 
#include <conio.h> 
#include <string> 
#include <vector> 
#include <algorithm> 

const int MAXADDRESS = 25; 

struct PEOPLE2 
{ 
    char fName2[25]; 
    char lName2[25]; 
    char Address2[25]; 
}; 

struct PERSON 
{ 
    PERSON() 
    {} 
    PERSON(PEOPLE2 p) 
     : fName(p.fName2), lName(p.lName2), Address(p.Address2) 
    {} 
    PERSON(const std::string& first, const std::string& last, const std::string& add) 
     : fName(first), lName(last), Address(add) 
    {} 
    std::string fName; 
    std::string lName; 
    std::string Address; 
}; 
// required for std::sort 
bool operator< (const PERSON &lhs, const PERSON &rhs) 
{ 
    int result = (lhs.lName.compare(rhs.lName)); 
    if(result == 0) 
     return lhs.fName < rhs.fName; 
    return result < 0; 
} 
// required for os << people 
std::ostream& operator<< (std::ostream& os, const PERSON& rhs) 
{ 
    os << rhs.fName << " " << rhs.lName << " " << rhs.Address; 
    return os; 
} 

class addressBook 
{ 
private: 
    std::vector<PERSON> people; 

public: 

    addressBook() 
    { } 

    addressBook(const PERSON &p) 
    { 
     addPerson(p); 
    } 

    template<typename IT> 
    addressBook(IT begin, IT end) 
    { 
     std::copy(begin, end, std::back_inserter(people)); 
    } 

    addressBook(char *fName, char *lName, char *address) 
    { 
     PERSON tmp; 
     tmp.fName = fName; 
     tmp.lName = lName; 
     tmp.Address = address; 
     addPerson(tmp); 
    } 

    addressBook(const std::string& fileName) 
    { 
     std::ifstream indata(fileName, std::ios::binary); 

     PEOPLE2 p; 
     while(true) 
     { 
      indata.read(reinterpret_cast<char*>(&p), sizeof(PEOPLE2)); 
      if(indata.fail()) 
       break; 
      people.push_back(p); 
     } 
    } 

    void addPerson(const PERSON &p) 
    { 
     people.push_back(p); 
    } 

    bool findPerson(const std::string& lastName, PERSON& p) 
    { 
     std::find_if(std::begin(people), std::end(people), [&](const PERSON& in)->bool 
     { 
      if(lastName == in.lName) 
      { 
       p = in; 
       return true; 
      } 
      return false; 
     }); 
    } 

    bool findPerson(const std::string& lastName, const std::string& firstName, PERSON& p) 
    { 
     std::find_if(std::begin(people), std::end(people), [&](const PERSON& in)->bool 
     { 
      if(lastName == in.lName && firstName == in.fName) 
      { 
       p = in; 
       return true; 
      } 
      return false; 
     }); 
    } 

    void printBook() 
    { 
     std::for_each(std::begin(people), std::end(people), [](const PERSON& p) 
     {  
      std::cout << p.fName << "\t" << p.lName << "\t" << p.Address << std::endl; 
     }); 
    } 

    void sort() 
    { 
     std::sort(std::begin(people),std::end(people)); 
    } 

    void waitKey(); 

    void showRecord(int pickNum) 
    { 
     std::cout << people[pickNum] << std::endl; 
    } 

    void writeRecord(const std::string& fileName) 
    { 

     PEOPLE2 temp; 
     std::ofstream outFile(fileName, std::ios::app); 
     if(!outFile) 
     { 
      std::cout << "Error opening file for writing " << std::endl; 
      return; 
     } 

     for(auto iter = people.begin(), end = people.end(); iter != end; ++iter) 
     { 
      strncpy(temp.fName2, iter->fName.c_str(), 25); 
      strncpy(temp.lName2, iter->lName.c_str(), 25); 
      strncpy(temp.Address2, iter->Address.c_str(), 25); 
      outFile.write(reinterpret_cast<const char *>(&temp), sizeof(PEOPLE2)); 
     }  
    } 

    void showAll() 
    { 
     std::for_each(std::begin(people), std::end(people), [](const PERSON& in) 
     { 
       std::cout << in << std::endl; 
     }); 
    } 


    friend std::ostream &operator << (std::ostream &output, addressBook &ab) 
    { 
     std::for_each(std::begin(ab.people), std::end(ab.people), [&](const PERSON& in) 
     { 
      output << in << "\n"; 
     }); 
     return output; 
    } 

    addressBook &operator +=(const PERSON &p) 
    { 
     addPerson(p); 
     return *this; 
    }; 

    PERSON operator [](int x) 
    { 
     return people[x]; 
    }; 

}; 

std::string fileName = "c:\\temp\\test.rec"; 
int main() 
{ 
    addressBook ab(fileName); 
    ab.sort(); 
    ab.showAll(); 

    //addressBook ab; 
    //ab.addPerson(PERSON("Mary", "Smith", "1 West Street")); 
    //ab.addPerson(PERSON("Joe", "Brown", "2 East Street")); 
    //ab.addPerson(PERSON("Harry", "Cooper", "3 South Street")); 
    //ab.writeRecord(fileName); 
} 
+0

哇!这需要花一些时间才能理解你所做的一些改变,但是我所理解的内容是非常有意义的。我一直认为我的PERSON结构应该看起来像你做的那样,但老师并不想那样。我将与此合作,因为您实际上在这里完成了一些东西,看起来似乎比我们一直在做的更有效。当然,它是一门课程,所以保持简单可能就是关键,但我喜欢这个,谢谢! – 2013-04-08 19:26:13

相关问题