C ++ ifstream I/O?
C ++ ifstream I/O?
本文关键字:ifstream 更新时间:2023-10-16
我正在试验c++文件I/O,特别是fstream。我写了下面的代码,到目前为止,它告诉我没有getline成员函数。我被告知(并且仍然坚持)有一个成员函数getline。有人知道如何使用fstream的getline成员函数吗?或者另一种从文件中一次获取一行的方法?我在命令行中使用两个具有唯一文件扩展名的文件参数。
。/fileIO foo。代码foo.encode
#include <fstream>
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <string>
#include <cassert>
using namespace std;
int main( int argc, char *argv[] )
{
// convert the C-style command line parameter to a C++-style string,
// so that we can do concatenation on it
assert( argc == 2 );
const string foo = argv[1];
string line;string codeFileName = foo + ".code";
ifstream codeFile( codeFileName.c_str(), ios::in );
if( codeFile.is_open())
{
getline(codeFileName, line);
cout << line << endl;
}
else cout << "Unable to open file" << endl;
return 0;
}
getline(codeFileName, line);
应该getline(codeFile, line);
你传入的是文件名,而不是流。
顺便说一下,您使用的getline
是一个自由函数,而不是成员函数。事实上,应该避免使用成员函数getline
。它更难使用,并且可以追溯到标准库中没有string
的日子。
字体
getline(codeFileName, line);
应该getline(codeFile, line);
我想教训是你必须学会如何解释编译器错误消息。
相关文章:
- ifstream什么都没读
- ifstream文件在从行中读取时被清除
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- 将 ifstream 拆分为 n 个流?
- 如何将 std::ifstream 转换为 std::basic_istream<CharT, Traits>&?
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 是否可以在没有真实文件的情况下创建 ifstream
- 虽然循环不读取整个 ifstream 文件
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- Ofstream和IFSTREAM有什么区别?
- 如何将QByteArray转换为std::istream或std::ifstream?
- ifstream 跳过数字,只写 0
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- 在函数中声明的 ifStream 对象在发布模式下不起作用
- fstream / ifstream / ofstream 对象如何转换为布尔值
- ifstream 尝试读取 9 到 13 之间的无符号字符时非常奇怪的行为
- 尝试在 C++ 中为 ifstream 提供一个变量
- ifstream 读取的字符与文件中写入的字符不同
- 如何使用 ifstream 检查文件中变量的类型?