0

我有一个文件,我想打印它的内容,但我的功能无法正常工作,谁能帮忙?

这是我的代码:

int main()
{
  MenuText text;
  string test = "Champion";
  ofstream output("File.txt");
  text.save(output);
  fstream output ("File.txt");
  text.load("File.txt");//This is an error.
  text.print();


MenuText::MenuText()
{
    mText = "Default";

}
MenuText :: MenuText(string text)
{
mText = text;
}
void MenuText::print()
{
cout<< "Story= " << mText<< endl;
cout<< endl;
}
void MenuText::save(ofstream& outFile)
{
outFile<<   "/         .   \  \____    \\   \\    _ -. "
            //"/    /__        -    \/\_______\\__\\__\ \"
            "__\  /\   __   \     .      \/_______//__//"
            "__/\/__/ \  \   \_\  \       -   ________  "
            "___    ___  ______  \  \_____/     -  /\    "
            "__    \\   -.\    \\   ___\ \/_____/    ."
            "\  \  \__\   \\   \-.      \\   __\_        "
            "-   \ _\_______\\__\  `\___\\_____\           "
            ".     \/_______//__/    /___//_____/ "<< mText<< endl;
cout<< endl;
outFile<< endl;
}
void MenuText::load(ifstream& inFile)
{
string garbage;
inFile>> garbage >> mText;
}
The errors are:
Error   1   error C2371: 'output' : redefinition; different basic types c:\users\conor\documents\college\c++ programming\marooned\marooned\mainapp.cpp  15  1   Marooned
Error   2   error C2664: 'MenuText::load' : cannot convert parameter 1 from 'const char [9]' to 'std::ifstream &'   c:\users\conor\documents\college\c++ programming\marooned\marooned\mainapp.cpp  16  1   Marooned
4

1 回答 1

2

MenuText::load()将 aifstream&作为其唯一参数,而不是 a const char*。创建 an 的实例ifstream并将其传递给MenuText::load()

std::ifstream input("File.txt");
if (input.is_open())
{
    text.load(input);
}

此外,确保所有写入数据close()output流在创建ifstream.

MenuText::load()不会将文件的全部内容读入内存。它将文件中遇到的第二个字符串存储到mTextoperator>>在第一个空白字符处停止读取。

于 2012-09-24T11:11:58.153 回答