C++帮助使用 ifstream 的 getline 函数

发布于 2024-08-25 17:11:31 字数 1005 浏览 3 评论 0原文

所以我正在编写一个程序来处理读入和写出文件。我使用 getline() 函数,因为文本文件中的某些行可能包含多个元素。到目前为止,我从来没有遇到过 getline 问题。这就是我得到的。

文本文件如下所示:

  John Smith                       // Client name
  1234 Hollow Lane, Chicago, IL    // Address
  123-45-6789                      // SSN
  Walmart                          // Employer
  58000                            // Income
  2                                // Number of accounts the client has
  1111                             // Account Number
  2222                             // Account Number

代码如下:

ifstream inFile("ClientInfo.txt");
if(inFile.fail())
{
    cout << "Problem opening file.";
}
else
{
    string name, address, ssn, employer;
    double income;
    int numOfAccount;

    getline(inFile, name);
    getline(inFile, address);
    // I'll stop here because I know this is where it fails.

当我调试此代码时,我发现 name == "John",而不是 name == "John Smith",并且 Address == "Smith" 等等。我做错了什么吗?任何帮助将不胜感激。

So I am writing a program that deals with reading in and writing out to a file. I use the getline() function because some of the lines in the text file may contain multiple elements. I've never had a problem with getline until now. Here's what I got.

The text file looks like this:

  John Smith                       // Client name
  1234 Hollow Lane, Chicago, IL    // Address
  123-45-6789                      // SSN
  Walmart                          // Employer
  58000                            // Income
  2                                // Number of accounts the client has
  1111                             // Account Number
  2222                             // Account Number

And the code like this:

ifstream inFile("ClientInfo.txt");
if(inFile.fail())
{
    cout << "Problem opening file.";
}
else
{
    string name, address, ssn, employer;
    double income;
    int numOfAccount;

    getline(inFile, name);
    getline(inFile, address);
    // I'll stop here because I know this is where it fails.

When I debugged this code, I found that name == "John", instead of name == "John Smith", and Address == "Smith" and so on. Am I doing something wrong. Any help would be much appreciated.

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

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

发布评论

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

评论(1

原谅我要高飞 2024-09-01 17:11:31

您显示的代码应该适用于该文件。所以一定有什么东西和你想象的不一样。最可能的罪魁祸首是:

  1. 文件实际上有一个换行符,而您认为它只有一个空格
  2. 代码使用 inFile >>> name 您认为它使用了 getline(inFile,name)

也许您更改了某些内容并忘记保存或重新编译,或者您正在读取与您想象的不同的文件。

顺便说一句,从您的变量声明来看,您可能计划将 getline() 调用与提取运算符调用(如 inFile >>> )混合使用。收入。混合这些需要小心,因为提取运算符会留下 getline() 可能会读取的尾随空白。 底部附近有更多信息。

The code you show should work on that file. So something must be different than you think. The most likely culprits are:

  1. The file actually has a newline where you think it only has a space
  2. The code uses inFile >> name where you think it uses getline(inFile,name)

Perhaps you changed something and forgot to save or recompile or you are reading a a different file than you think.

By the way, from your variable declarations it looks like you may be planning to mix getline() calls with extraction operator calls like inFile >> income. Mixing these requires care because the extraction operator leaves behind trailing whitespace that getline() might then read. There's more information near the bottom of this.

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