如何打印出文本文件的内容
How to print out contents of a text file
我有一个文件,我想打印它的内容,但我无法让我的函数工作,有人可以帮忙吗?
这是我的代码:
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:usersconordocumentscollegec++ programmingmaroonedmaroonedmainapp.cpp 15 1 Marooned
Error 2 error C2664: 'MenuText::load' : cannot convert parameter 1 from 'const char [9]' to 'std::ifstream &' c:usersconordocumentscollegec++ programmingmaroonedmaroonedmainapp.cpp 16 1 Marooned
MenuText::load()
将ifstream&
作为其唯一的参数,而不是const char*
。创建一个ifstream
实例并将其传递给MenuText::load()
:
std::ifstream input("File.txt");
if (input.is_open())
{
text.load(input);
}
此外,close()
output
流以确保在创建ifstream
之前刷新写入的所有数据。
MenuText::load()
不会将文件的全部内容读入内存。它将存储文件中遇到的第二个字符串以mText
因为operator>>
将在第一个空格字符处停止读取。
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何使用 c++ 实现并发文件/文本编辑?
- 我需要转换一些代码,以便它适用于输入和输出文件文本
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 将大型数据文件拆分为多个小文件(文本格式)
- 如何读取输入文件(文本文件)并将输入验证为有效整数
- 从文本文件/文本流中读取Q字符串错误
- 从文本文件(文本解析器)C++中读取行和列的逻辑
- 如何将*.css文件(文本文件)的内容与附加信息存储在新文件中?
- 关于内存映射文件和使用大文件文本编辑器
- 如何在读取文件文本时跳过"n"
- 使用libzip从.zip获取文件(文本除外)