简体   繁体   English

使用.getline和字符串代替char

[英]Using .getline with string instead of char

So I have some code here that reads from a text document and stores it into char bunz . 所以我这里有一些代码可以从文本文档中读取并将其存储到char bunz I know this sounds like a stupid question but I'd rather use string instead of char. 我知道这听起来像是一个愚蠢的问题,但我宁愿使用string而不是char。 Will .getline accept a string if it is used with ifstream ? 如果将.getlineifstream使用,它会接受字符串吗? Or will I be forced to convert the char to a string afterwords? 还是会被迫将char转换为字符串后缀?

Thanks. 谢谢。

ifstream filler("C:\\bunz.txt");

char bunz[30+1];
filler.getline(bunz, 40);
cout<<bunz;
filler.close();

Notorious for posting answers as comments, chris is spot on . 克里斯因发表评论作为评论而臭名昭著,而克里斯却在现场 Once you use std::getline() , you'll never go back: 一旦使用std::getline() ,您将永远不会返回:

ifstream filler("C:\\bunz.txt");
string bunz;
getline(filler, bunz);
cout<<bunz;
filler.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM