所以我正在编写一个处理读取和写入文件的程序。我使用 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" 等等。难道我做错了什么。任何帮助将非常感激。