substr 无缘无故地切断了部分字符串。或者至少原因是在躲避我

substr is cutting off part of the string for no reason. or at least the reason is eluding me

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    string line = "";
    string firstName = "";
    string middleName = "";
    string lastName = "";
    ifstream myFile("Ch7_Ex9Data.txt");
    if(myFile.is_open()){
        while( getline (myFile, line) ){
            size_t pos = line.find(" ");
            lastName = line.substr(0, pos);

            size_t index = 0;
            for(int i = 0; i < 2; ++i){
                index = (line.find(" ", index)) + 1;
            }
            index = index - 1;
            firstName = line.substr(line.find(" "), line.find(" "));
            middleName = line.substr(index);
            cout << firstName << endl;
            //cout << firstName << " " << middleName << " " << lastName << endl;
        }
        myFile.close();
    }else{
        cout << "unable to open file" << endl;
    }
   return 0; 
}

我的文本文件是这样设置的(lastName、firstName、middleName)

Miller, Jason Brian
blair, Lisa Maria
Gupta, Anil Kumar
Arora, Sumit Sahil
Saleh, Rhonda Beth

但是它一直切断 "Rhonda" 中的 "a" 但正确显示每个其他人的名字?

当我使用不同的编译器 (visual studio 2017) 时,它只打印名字 Jason 然后我得到错误:

testing.exe 中 0x76F917D2 处的未处理异常:Microsoft C++ 异常:std::out_of_range 在内存位置 0x010FF108。

我不明白你的代码。例如,您在 for 循环中尝试什么:

for(int i = 0; i < 2; ++i){
    index = (line.find(" ", index)) + 1;
}

或行

firstName = line.substr(line.find(" "), line.find(" "));

表示:从行中截取子串,起始索引为line.find(" "),字符数为line.find(" "),这是错误的。因为您必须在此处显示必须剪切的字符数。文档:https://en.cppreference.com/w/cpp/string/basic_string/substr

这就是为什么我自己解决了你的问题:

while( getline (myFile, line) ){
    // Miller, Jason Brian
    // Getting Miller 
    lastName = line.substr(0, line.find(" ") - 1); // find(" ") - 1 for removing , symbol
    line = line.substr(lastName.length() + 2); // cut out lastname from line
    firstName = line.substr(0, line.find(" "));
    middleName = line.substr(line.find(" ") + 1); // find(" ") + 1 for remove leading whitespace
    cout << firstName << " " << middleName << " " << lastName << endl;
}

C++17 解决方案使用 string_view:

#include <string>
#include <string_view>

std::string line = "Saleh, Rhonda Beth";
const auto first_space = line.find(' ');
const auto last_space = line.rfind(' ');
std::string_view lastName(line.c_str(), first_space - 1);
std::string_view middleName(line.c_str() + last_space + 1);
std::string_view firstName(line.c_str() + first_space + 1, last_space - first_space - 1);

std::cout << firstName << ' ' << middleName << ' ' << lastName << std::endl;
  • substr 不是您想要做的事情的正确工具。

  • 不要使用长 ifs。而不是 if(myFile.is_open()) {...} 使用:

    if(!myFile) return -1; // error
    
  • getline 有第三个参数 - delimiter - 它允许您指定哪个字符将停止提取。所以你实际上可以让它在找到 ‘,’
  • 时停止
  • 跳过空格 使用ws 函数或操纵符:

    ws( is ); // function
    is >> ws; // manipulator
    

你的固定程序(假设文件内容格式正确):

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
  ifstream is{ "Ch7_Ex9Data.txt" };
  if (!is)
    return -1;

  string s;
  while (ws(is), getline(is, s, ',')) // skip the white-spaces, then read up to ','
  {
    cout << "First Name: " << s << endl;

    ws( is ), getline(is, s, ' ');
    cout << "Middle Name: " << s << endl;

    ws(is), getline(is, s, '\n');
    cout << "Last Name: " << s << endl;
  }

  return 0;
}