我如何从文件C++中读取并输出它
How do i read from a file C++ and output it
我尝试使用getline
但它给了我一些错误
注意:__ssize_t getline(char**,size_t*,FILE*)
我的线路就像这个
ofstream myfile;
myfile.open("file.txt");
while(!myfile.eof())
{
getline(myfile,sline);
cout << sline;
}
如何让我的C++读取file.txt
确保您有#include <string>
,其中定义了std::getline()
,并且sline
是std::string
。
将循环结构更改为:
while (std::getline(myfile, sline))
{
std::cout << sline << "n";
}
以避免处理失败的读取。
使用std::ifstream
阅读,而不是Karoly在评论中指出的std::ofstream
。
看起来像是#include
d <stdio.h>
或<cstdio>
,因此正在尝试使用C的getline
函数。更改:
getline(myfile,sline);
至:
std::getline(myfile,sline);
以确保您正在使用C++CCD_ 11。
相关文章:
- C++试图读取一个文件并输出到另一个文本文件
- 如何使用C++读取另一个程序中的源代码输出
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 从文件读取并通过矢量输出到控制台
- 从popen()读取的输出是否在pclose()之前完成
- C++ 读取未知数量的整数到 cin 并输出总和
- libmpg123 在我使用整数编码读取 mp3 时强制浮点输出
- 尝试从 txt 文件中读取两条数据并输出它们 [C++]
- 将多个效果与 libSox 链接并读取输出数据的正确方法
- 如何从控制台启动可执行文件并使用Qt读取输出
- C#类过程 - 控制C 应用程序,不可能读取输出
- 如何运行批处理文件和读取输出
- popen 管道执行程序和读取输出的替代方法
- 读取/输出文件时出现问题
- 如何从流读取输出
- 从C/ c++编码的应用程序中读取输出流
- 如果你的程序在运行时几乎立即退出,如何确保控制台窗口保持打开状态以读取输出
- 如果未读取输出,端点将抛出无效参数
- 如何用c++打开、读取输出和输入.jar文件
- 使用 C++ 从 COM 端口读取输出数据