ifstream get() 不填充字符数组
ifstream get() not filling char array
我不知道会做什么
#include "fstream"
#include "iostream"
using namespace std;
#define out(a) cout << #a << ": " << a << 'n'
void print(string s)
{
cout << s << 'n';
}
int main()
{
ifstream readt1;
readt1.open("test1.yaml");
while(readt1.good())
{
char cc[128];
readt1.get(cc,128);
out(cc);
}
readt1.close();
}
那个代码...输出此内容:
cc: version: 0.14.1
cc:
与测试.yaml 是这个
version: 0.14.1
name: scrumbleship
author: dirkson
description: >
A minecraft like game that allows you
to build your own spaceship!
我已经尝试了很多方法来让它工作,但它根本没有
如果你在 get(( 之后添加一个readt1.ignore();
,它应该可以工作:
while(readt1.good())
{
char cc[128];
readt1.get(cc,128);
readt1.ignore(); // <--- add this to ignore newline
out(cc);
}
这解决了眼前的问题,但使用std::getline
和std::string
会更好C++。像这样:
while(std::getline(readt1, line)) {// Do stuff}
你应该使用 getline()
通过 ifstream 读取行
另外,我会在循环之外获得第一行,循环应该检查 EOF 以确保您获得整个文件。好((不是只是暗示有一个文件要读取吗?
相关文章:
- 流填充字符的默认定位
- 将十六进制值设置为用于填充的字符变量时出现问题
- 将空填充但不以空结尾的固定长度字符数组转换为 std::string 的最佳方法
- 填充字符数组的最佳方法(获取与cin.getline)
- 给定一个填充无符号字符**的 C 函数,如何在没有中间副本的情况下用数据填充 std::vector
- 如何填充项目为 8 个字符的集合?(std::set<char [8]>)
- 用字符串填充字符数组?C++
- 如何填充二维字符数组?C++
- 在计算使用了多少个数组索引的同时,如何填充一个来自用户输入的字符的数组?(使用cin.get)
- 我如何用单词填充二维字符阵列
- C/C++ 从字符缓冲区读取以填充结构
- 从 std::vector<无符号字符中的数据填充结构>
- 数组的第一个字符填充了不正确的数据C++
- 用字符填充二维数组
- 是否可以在C++中动态填充字符*数组
- ifstream get() 不填充字符数组
- 提升精神继承的字符串属性填充<int> ASCII 字符中的向量
- 使用 ifstream 剂量读取大型二进制文件不会填充字符缓冲区C++
- 分配后如何用字符串填充字符
- C++用多个字符串和浮点填充字符数组