从文件C ++读取字符串 [英] Reading a string from file c++

查看:268
本文介绍了从文件C ++读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图为我父亲的餐厅建立计费系统,以供练习。因此,我面临这样的问题,即我一次无法读取完整的字符串。例如,如果txt文件中有鸡肉汉堡,则编译器会读取它们,但将它们分成两个字。
我正在使用以下代码,并且该文件已经存在。

I'm trying to make billing system for my father's restaurant just for practice. So, I'm facing the problem that I can't be able to read the complete string one time.e.g If there were Chicken burger in txt file than compiler read them but break them into two words. I'm using the following code and the file is already exist.

std::string item_name;
std::ifstream nameFileout;

nameFileout.open("name2.txt");
while (nameFileout >> item_name)
{
    std::cout << item_name;
}
nameFileout.close();


推荐答案

要阅读整行,请使用

std::getline(nameFileout, item_name)

而不是

nameFileout >> item_name

您可以考虑重命名 nameFileout ,因为它不是名称,是输入而不是输出。

You might consider renaming nameFileout since it isn't a name, and is for input not output.

这篇关于从文件C ++读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆