c++ - 使用带有指针和文件概念的类 为什么我的代码在第一种情况下不起作用?

标签 c++

我正在尝试使用我们指向其成员的类的文件处理来使用我们学校项目中的概念,但我不知道为什么但它不起作用 我没有输出。为什么? PS 我使用的是非常老的 Turbo c++ 3.0 ver 编译器

#include<fstream.h>
#include<conio.h>
class Q
{
    char*q;
    char*a;
    public:
    void display()
    {
        cout<<q<<'\n'<<a<<'\n';
    }
}obj[28]={"What is the actual color of coca cola","green","Why was donald duck banned from finland","He doesn't wear pants","In which superhero comic will you find the line \"With great power, comes great Responsibility?\"","Spiderman","What is the dot over the letter \"i\" and \"j\" called? ","tittle","What is the world's most popular first name? ","Muhammad"," In Hong Kong, what is a wife, who has been betrayed by her adulterous husband, allowed to do, legally? ",
    "The wife may kill her husband, but with bare hands"," Which big Cat does NOT roar?","Cheetah","Which country released the world’s first 3-D Postage stamp?","Bhutan","What does the term 'piano' mean?","To be played softly","What would you be eating if you ordered LENGUA in Spain?","Tongue","Which animal is known to kill more people than plane crashes? ","Donkeys","Where was the fortune cookie actually invented? ","America","What is the tiny plastic covering of the tip of a shoelace called?","Aglet",
    "In Star Trek, what are Tribbles?","Little furry creatures","The three key combination Ctrl-Alt-Delete can be used to reboot your personal computer or to summon its task manager. Computer geeks sometimes use which expression from the Star Trek TV series to descibe this?","Vulcan Nerve Pinch","What is the land of giants called in Gulliver's Travels?","Brobdingnag","Who lives in a pineapple under the sea? ","Spongebob",
    "Who quoted \"Plato is my friend - Aristotle is my friend - but my greatest friend is truth.\"?","Isaac Newton","How many squares/spaces on a chess board?","64","Japanese three-line verses called Haiku contain how many syllables?","17","Which novel begins \"It is a truth universally acknowledged that a single man in possession of a good fortune must be in want of a wife...\"? ","Pride and Prejudice","What was the carburetor of the first Harley-Davidson, built in the 1903, made of?",
    "Tomato Can","GOLF was abbreviated from originally","Gentlemen Only Ladies Forbidden"," If you were to spell out numbers, how far would you have to go until you would find the letter “A”?","One Thousand","What do bulletproof vests, fire escapes, windshield wipers, and laser printers all have in common?","They are made by women","What is the only food that doesn’t spoil?","Honey","Which cartoon character is famous for the line, \"What's up, Doc?\"?","Bugs Bunny",
    "\"Andale! Andale! Arriba! Arriba!\"  What's the name of this lively cartoon charactor ? ","Speedy gonzales"};
int main()
{
    clrscr();
    ofstream filout("fio.txt");
    for(int q=0;q<28;++q)
        filout.write((char*)&obj[q],sizeof(obj[q]));
    filout.close();
    ifstream filin("fio.txt");
    Q oj;
    while(filin.read((char*)&oj,sizeof(oj)))
        oj.display();//doesn't display content in file
    getch();
    return 0;
}

最佳答案

这个:

filout.write((char*)&obj[q],sizeof(obj[q]));

Q 类的字节写入文件。它包含两个指针,而不是任何可读文本,因此您最终不会在文件中看到任何文本。

要写入字符串,请使用格式化输出:

filout << obj[q].q << '\n' << obj[q].a << '\n';

回读更加棘手,因为您使用的是指向字符串文字的原始指针,并且没有简单的方法从文件中读取兼容的内容。我建议您使用 std::string 而不是 char*;然后你可以把它读回来

while (std::getline(filin, oj.q) && std::getline(filin, oj.a)) {
    oj.display();
}

这假设您早已被遗忘的编译器支持标准(ish)字符串类型。如果没有,您可以通过存储字符缓冲区而不是指针来避开它:

char q[MAX_Q_SIZE];
char a[MAX_A_SIZE];

其中的大小足以容纳可能放入其中的任何字符串。这将适用于 filin.getline(q)(如果您有这样的东西)或您的代码。但请注意,如果任何字符串太长,这将出错。

无论如何,我强烈建议您升级到本世纪的编译器之一;今天几乎没有人能用这种古老的方言帮助您。

关于c++ - 使用带有指针和文件概念的类 为什么我的代码在第一种情况下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183349/

相关文章:

c++ - 找到等于方程式的所有数字对

c++ - 将字符数组转换为整数

c++ - 这两种对象实例化方法有什么区别?

C++ lambda函数调用纯虚函数

c++ - 如何将 3 个变量合并为 1 个变量

C++ 罗马附加形式转换

c++ - 如何在类构造函数中初始化匿名结构?

c++ - 不接受静态成员函数作为 constexpr 参数

c++ - Boost Array 数据问题

c++ - 为项目文本文件定义宏