简体   繁体   English

C ++:使用逗号分隔文件中的名字和姓氏

[英]C++: separating first and last name from a file, separated by a comma

I have this as a file: 我有这个文件:

Collins,Bill 80
Smith,Bart 75
Allen,Jim 82
Griffin,Jim 55
Stamey,Marty 90
Rose,Geri 78
Taylor,Terri 56
Johnson,Jill 77
Allison,Jeff 45
Looney,Joe 89
Wolfe,Bill 63
James,Jean 72
Weaver,Jim 77
Pore,Bob 91 
Rutherford,Greg 42
Javens,Renee 74
Harrison,Rose 58
Setzer,Cathy 93
Pike,Gordon 48
Holland,Beth 79

As you can see the last name and first name are separated with a comma. 如您所见,姓氏和名字之间用逗号分隔。

Right now I have this in my code: 现在我的代码中有这个:

ifstream inputFile;
inputFile.open ("students.txt");

string studentNames;            // Student names - Last name followed by first
int studentMarks = 0;           // Student mark for text/exam

// Read in data from students.txt
inputFile >> studentNames;
inputFile >> studentMarks;

// IF file has too much data, output WARNING
if (numElts >= NUM_NAMES)
{
    cout << "Error: File contains too many lines of data, check the file." << endl << endl;
}
else
{
    names[numElts] = studentNames;
    marks[numElts] = studentMarks;
    numElts++;
}

I want to bypass these commas, I want to store it as firstname and then last name which then I will just merge into name like name = firstname " " last name . 我想绕过这些逗号,我想将其存储为名字,然后是姓氏,然后我将其合并为name = firstname " " last name How should I do that, please? 请问该怎么办?

std::string::size_type commaPos = studentName.find(",");
std::string studentLastName = studentName.substr(0, commaPos);
std::string studentFirstName = studentName.substr(commaPos + 1);

use this function to remove the , and get the first name last with space between, 使用此功能删除,并获取姓氏,中间用空格隔开,

    string output;
    int position = studentNames.find(',');
    for(int i=position+1;i<studentNames.size();i++)
        output+=studentNames[i];
    output+=' ';
    for(int i=0;i<position;i++)
        output+=studentNames[i];
    studentNames = output;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 C++ 中的文件中读取用逗号分隔的全名? - How do I read a full name that is separated by a comma from a file in C++? C ++ - 读入以逗号分隔的文件行 - C++ - Read in file lines separated by a comma 逗号用C ++分隔浮点数 - Comma separated floats in C++ 如何从txt文件访问其他逗号分隔的值。 C ++ - How to access other comma separated values from a txt file. C++ 从文本文件中对逗号分隔的整数求和,然后存储到 C++ 中的数组中 - Summing comma separated ints from a text file and then storing to an array in C++ C ++如何读取文件并解析逗号分隔的值 - C++ how to read a file and parse the comma separated value 从 C++ 中的文本文件中读取带有杂散空格的逗号分隔值 - Read comma separated values with stray whitespaces from a textfile in c++ 我如何摆脱最后一个逗号? c ++正在从文件中读取并向后读取数字 - How do i get rid of the last comma? c++ is reading from a file and reading the numbers backwards c++ 中的 stringstream 有助于从字符串中提取逗号分隔的整数,但不能使用向量从空格分隔的整数中提取,为什么? - stringstream in c++ helps to extract comma separated integers from string but not space separated integers using vectors,why? C++:如何输入以逗号(,)分隔的值 - C++: how to input values separated by comma(,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM