2017-10-10 31 views
0

我想在關閉程序後創建保存數據的動物數據庫。所以我認爲它應該保存在一個文本文件中。C++中簡單的數據庫文本文件

1)是否有一個已經制作的C++函數?

2)如果不是,我們該如何解決問題?

到現在爲止,我有這樣的:

#include <iostream> 
#include <string> 

class Animal { 
    public: 
    std::string name, color1, color2, race, notes; 
    int age; 
}; 

void writeInTheDatabase(Animal const& ani); 
int readAgeFromTheDataBase(Animal const& ani); 

int main() { 

    Animal cat1; 
    cat1.name = "silvester"; 
    cat1.color1 = "black"; 
    cat1.color2 = "white"; 
    cat1.age = 4; 

    Animal dog1; 
    dog1.name = "Arnold"; 
    dog1.color1 = "brown"; 
    dog1.color2 = "black"; 
    dog1.age = 3; 

    writeInTheDatabase(dog1); 
    std::cout << "the age is: " << readAgeFromTheDataBase(dog1) << std::endl; 
} 

void writeInTheDatabaseTextFile(Animal const& ani) { 
    // this function should put all the info in a text file 
} 

int readAgeFromTheDataBase(Animal const& ani) { 
    int age; 
    // this function should return the age of an animal given 
    return age; 
} 
+0

那究竟是什麼問題? – Mureinik

+1

@Mureinik AFAICT他問「是否已經有預定義的功能」。到OP:見http://en.cppreference.com/w/ – sehe

+0

「動物」是誤導,因爲它似乎一次只包含一個動物。它也應該是「物種」而不是「種族」。 – tadman

回答

0

不能有針對定義的類東西預製。

對於在文件(如cout,fprintf,fwrite等)中編寫標準數據類型(文本或二進制文件)有多種解決方案,但需要爲您的類編寫一個方法來處理它的所有部分,採用你想要的格式和順序。