我正在使用ifstream,我需要用两个单词作为一个字符串读取而不是分开它们

发布于 2025-01-30 18:32:41 字数 108 浏览 1 评论 0原文

我需要从文件中读取一行,但是我需要将两个字符串作为一个。例如,如果文件的行有某人的名字,例如Bernie Sanders,我想将整个名称保存到字符串变量中,而不仅仅是名字。

I need to read in a line from a file, but I need to save two strings as one. For example, if the file's line had someone's name like Bernie Sanders, I would want to save the entire name into a string variable instead of just the first name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2025-02-06 18:32:41

我认为您正在

  std::string nameString;
  fs >> nameString;

这样做会读取到一个单词的结尾。
相反,

 std::string nameString;
 std::getline(fs, nameString);

这将读取整行

I assume you are doing

  std::string nameString;
  fs >> nameString;

this will read to the end of a word.
do this instead

 std::string nameString;
 std::getline(fs, nameString);

this will read the whole line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文