infile.getline() infile.get() 出现的问题,还没有解决.换成 getline(infile,str) 就正常了。所以用高级类型更安全,以后应该用高级类型string等

  

 

infile.getline()为什么会导致infile.fail()等于 1 呢??

并且问什么infile.eof() 为什么一直为0

c++ reference :

flagerror
eofbit

The end of the source of characters is reached during its operations.

failbit

No characters were extracted because the end was prematurely found.
This is also set if the function stops extracting because n-1 characters were extracted (n including the terminating null-character).
Notice that some eofbit cases will also set failbit.

badbit

An error other than the above happened.

 

当 while(infile.good()换成  while(infile.eof()) 时将是无限死循环。为什么?为什么infile.eof() 总不是 1

infile.getline()   infile.get()   出现的问题,还没有解决.换成 getline(infile,str) 就正常了。

所以用高级类型更安全,以后应该用高级类型——string等。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的代码示例展示了如何使用`ifstream`和`getline`函数从文本文件中读取多行数据。在这个代码示例中,首先使用`ifstream`打开一个名为`inFile`的文件,然后通过循环使用`getline`函数逐行读取文件中的数据并将其存储在字符数组`c`中,最后将每行数据输出到终端。 引用中的代码示例也展示了如何使用`ifstream`和`getline`函数从文本文件中读取多行数据。与引用不同的是,该代码示例将`getline`函数放在一个循环中,以连续读取文件中的多行数据。在每次迭代中,读取的一行数据存储在字符数组`c`中,并将其输出到终端。 引用中的代码示例同样展示了如何使用`ifstream`和`getline`函数从文本文件中读取一行字符串。这个代码示例与引用相似,但只读取一行数据,并将其存储在字符数组`c`中,最后将该行数据输出到终端。 总结来说,`inFile.getline`是一个用于从文件中读取一行字符串的函数,它可以帮助我们逐行读取文件中的数据。我们可以通过循环使用`getline`函数来读取多行数据,也可以使用一次`getline`函数读取单行数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++:使用getline读取文本文件](https://download.csdn.net/download/weixin_38646230/14017701)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++ getline():从文件中读取一行字符串](https://blog.csdn.net/weixin_45063703/article/details/127783393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值