读取和显示文件
Reading and displaying a file
我在显示此文件时遇到问题。我正在尝试创建一个文件并将其显示在输出屏幕上。但是getline不起作用。它在第40行不断地给我一个"getline未声明"。我试过改变,但我做的都不管用。问题出在哪里?
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
char filename[] = "Hello.txt";
string line = "Hello, this is my output file";
ofstream OutFile;
OutFile.open(filename);
if(OutFile.fail()) // check for successfully open ,
{
cout << "file named can not be found n";
exit(1);
}
OutFile << line;
if (OutFile.is_open())
OutFile.getline(line);
OutFile.close();
system("pause");
}
std::getline()
是一个自由函数,而不是成员函数。成员函数版本适用于原始字符数组,由于您使用的是std::string
,因此free函数是合适的。
std::getline(Outfile, line);
还要注意,std::getline()
仅适用于std::istream
类型的对象,并且由于您尝试执行输入,因此Outfile
对象不应该是std::ofstream
请将其更改为输入文件流std::ifstream
将Outfile
更改为std::fstream
,这是一个双向文件流。
相关文章:
- 正在将csv文件读取为双精度矢量
- gtkmm 显示文件内容
- 将从格式化文本文件读取的文本数据存储到链表
- 如何在 MFC 中显示文件的所有行
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 从文本文件读取到显示到多个联系人
- 打印从 TXT 文件读取的 2D 数组在 C++ 中显示错误
- 从文本文件读取矩阵并显示
- 读取和显示文件
- 使用SDI应用程序(MFC)从文本文件读取数据并显示数据
- C++将文本文件读取为二进制文件并在控制台中显示
- 读取并显示文件,但输出不正确
- 从二进制文件读取双精度(到类中)-小数不显示
- 显示并读取目录下的所有文件