【问题标题】:Addressbook writing to file通讯录写入文件
【发布时间】:2013-10-30 01:09:59
【问题描述】:

我有一个地址簿 C++ 程序,它可以编译所有内容,但我不知道如何将它写入每次退出时保存数据的文件。这是我的代码:

 //AddressBook Program
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <stdio.h>                                                                

using namespace std;

class AddressBook{

public :
   AddressBook()
      {
         count = 0;
      }

   void AddEntry();
   void DisplayAll();
   void DisplayEntry(int i); // Displays one entry                                   
   void SearchEntry();

   int MainMenu();

   struct Entry_Struct
   {
      char firstName[ 15 ] ;
      char lastName[ 15 ] ;
      char birthday[ 15 ] ;
      char phone[ 15 ] ;
      char email[ 15 ] ;
   };

   Entry_Struct entries[100];
   unsigned int count;
};


void AddressBook::AddEntry()
{
   cout << "Entry number " << (count + 1) << " : " << endl;

   cout << "Enter First Name: ";
   cin >> entries[count].firstName;

   cout << "Enter Last Name: ";
   cin >> entries[count].lastName;

   cout << "Enter Date of Birth: ";
   cin >> entries[count].birthday;

cout << "Enter Phone Number: ";
   cin >> entries[count].phone;

   cout << "Enter Email: ";
   cin >> entries[count].email;

   ++count; // tally total entry count                                               
}


void AddressBook::DisplayEntry(int i)
{
   cout << "Entry[" << i + 1 << "] : " << endl; // states # of entry                 
   cout << "First name : " << entries[i].firstName << endl;
   cout << "Last name : " << entries[i].lastName << endl;
   cout << "Date of birth : " << entries[i].birthday << endl;
   cout << "Phone number : " << entries[i].phone << endl;
   cout << "Email: " << entries[i].email << endl;
}

void AddressBook::DisplayAll()
{
   cout << "Number of entries : " << count << endl;

   for(int i = 0;i < count;++i)
      DisplayEntry(i);
}

void AddressBook::SearchEntry()
{
   char lastname[32];
   cout << "Enter last name : ";
   cin >> lastname;

   for(int i = 0;i < count;++i)
   {
      if(strcmp(lastname, entries[i].lastName) == 0)
      {
         cout << "Found ";
         DisplayEntry(i);
         cout << endl;
      }
   }
}


// Your class                                                                        
AddressBook my_book;

int MainMenu()
{
   int num;
   bool bQuit = false;

   // Put all your code into a while loop.                                           
   while(bQuit == false)
   {                                              

      cout << "+-------------------------------------+" << endl;
      cout << "|         Address Book Menu           |" << endl;
      cout << "|                                     |" << endl;
      cout << "| 1- Add an entry                     |" << endl;
      cout << "| 2- Search for an entry by last name |" << endl;
      cout << "| 3- Display all entries              |" << endl;
      cout << "| 4- Exit                             |" << endl;
      cout << "|                                     |" << endl;
      cout << "+-------------------------------------+" << endl;

      cout << endl;
      cout << "Please enter a number for one of the above options: ";
      cin >> num;
      cout << endl;

      if (num == 1)
         my_book.AddEntry();
      else if (num == 2)
         my_book.SearchEntry();
      else if (num == 3)
         my_book.DisplayAll();
      else if (num == 4)
         bQuit = true;
      else
         cout << "Invalid choice. Please try again" << endl;

      cout << endl;                                                            
   }

   return 0;
}

int main (){
   MainMenu();
   return 0;
}

我一整天都在翻阅我的教科书,但我所做的一切都是徒劳的。

【问题讨论】:

  • 你想读/写地址簿文件吗?然后你应该查找 ostream、istream 和 open。

标签: c++ file file-io addressbook


【解决方案1】:

这是一个打开和写入输出文件的基本示例,

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream outfile;
  outfile.open ("addressbook.txt");
  outfile << "Daffy,Duck,123 Main Street,Anytown,OH,USA,123-456-7890\n";
  myfile.close();
  return 0;
}

【讨论】:

    【解决方案2】:

    您需要为您的班级配备一个插入器。使用operator &lt;&lt; 完成:

    // Inside your class:
    friend std::istream& operator<<(std::ostream& os, const AddressBook ab)
    {
        return os << /* ... */
    }
    

    如您所见,用户定义的operator &lt;&lt; 可以根据预定义的标准插入器来实现。您可以通过以下方式使用它:

    std::ifstream in("yourtxtfile.txt");
    in << my_book;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 2012-11-14
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多