C++ 找出一个单词后面是空格还是换行
当前代码:C++ 找出一个单词后面是空格还是换行,c++,C++,当前代码: void LinkedList::readFunct( string file ) { string word; string trailing_char; stringstream ss; ifstream infile ( file.c_str() ); while ( getline( infile, word)) { cout << "The line is " << word << endl; ss << w
void LinkedList::readFunct( string file ) {
string word;
string trailing_char;
stringstream ss;
ifstream infile ( file.c_str() );
while ( getline( infile, word)) {
cout << "The line is " << word << endl;
ss << word;
while ( getline ( ss, word, ' ' )) {
trailing_char = "space";
cout << "Word: " << word << endl << "Trail: "<< trailing_char << endl;
}
ss.str( string() );
ss.clear();
}
}
应该让abc后面跟一个空格,ghi和pqr后面跟一个新行(我知道事实上不会,但我会将所有内容都分配到一个链表中,以便以后进一步使用,我需要知道这是该行的结尾)
我几个小时来一直想弄明白这个谜团,但我不知所措。帮助?嗯,
getline
默认情况下由换行符分隔,所以这应该是您所需要的全部内容。例如:
std::ifstream infile("text.txt");
for (std::string line; std::getline(infile, line); )
{
std::istringstream iss(line);
bool firstword = true;
for (std::string word; iss >> word; )
{
if (!firstword) { std::cout << "SPACE\n"; }
std::cout << word;
firstword = false;
}
std::cout << "NEWLINE\n";
}
std::ifstream infle(“text.txt”);
for(std::string-line;std::getline(infle,line);)
{
标准::istringstream iss(线);
bool firstword=true;
对于(std::string word;iss>>word;)
{
如果(!firstword){std::coutWell,getline
默认情况下由换行符分隔,那么这应该就是您所需要的全部内容。例如:
std::ifstream infile("text.txt");
for (std::string line; std::getline(infile, line); )
{
std::istringstream iss(line);
bool firstword = true;
for (std::string word; iss >> word; )
{
if (!firstword) { std::cout << "SPACE\n"; }
std::cout << word;
firstword = false;
}
std::cout << "NEWLINE\n";
}
std::ifstream infle(“text.txt”);
for(std::string-line;std::getline(infle,line);)
{
标准::istringstream iss(线);
bool firstword=true;
对于(std::string word;iss>>word;)
{
如果(!firstword){std::cout您首先使用std::getline(in,word)
读取字符串,这将占用所有换行符。然后使用std::getline(in,word'))
最后一个单词后面可能紧跟着一个零,即它在一行边界处。检查换行符和空格之间的差异的方法是检查内部的std::getline()是否
由于空格或到达字符串末尾而停止,在这种情况下,由于以下字符是换行符而停止:
while (std::getline( infile, word)) {
std::cout << "The line is '" << word << "'\n";
ss.clear();
ss.str(word);
while (std::getline (ss, word, ' ' )) {
trailing_char = ss.eof()? "newline": "space";
cout << "Word: " << word << endl << "Trail: "<< trailing_char << '\n';
}
}
while(std::getline(infle,word)){
std::cout首先使用std::getline(in,word)
读取字符串,这将消耗所有新行。然后使用std::getline(in,word“”)
最后一个单词后面可能紧跟着一个零,即它在一行边界处。检查换行符和空格之间的差异的方法是检查内部的std::getline()是否
由于空格或到达字符串末尾而停止,在这种情况下,由于以下字符是换行符而停止:
while (std::getline( infile, word)) {
std::cout << "The line is '" << word << "'\n";
ss.clear();
ss.str(word);
while (std::getline (ss, word, ' ' )) {
trailing_char = ss.eof()? "newline": "space";
cout << "Word: " << word << endl << "Trail: "<< trailing_char << '\n';
}
}
while(std::getline(infle,word)){
我编辑了你的代码:
void LinkedList::readFunct( string file ) {
string word;
string trailing_char;
stringstream ss;
ifstream infile ( file.c_str() );
while ( getline( infile, word)) {
cout << "The line is " << word << endl;
ss << word;
bool firsttime = true;
while ( ss >> word ) {
if (!firsttime)
cout << "Trail: space" << endl;
cout << "Word: " << word << endl;
firsttime = false;
}
if (!firsttime)
cout << "Trail: NEWLINE" << endl;
else
cout << "empty line." << endl;
ss.str( string() );
ss.clear();
}
}
void LinkedList::readFunct(字符串文件){
字符串字;
字符串拖尾字符;
细流ss;
ifstream infle(file.c_str());
while(getline(infle,word)){
我编辑了你的代码:
void LinkedList::readFunct( string file ) {
string word;
string trailing_char;
stringstream ss;
ifstream infile ( file.c_str() );
while ( getline( infile, word)) {
cout << "The line is " << word << endl;
ss << word;
bool firsttime = true;
while ( ss >> word ) {
if (!firsttime)
cout << "Trail: space" << endl;
cout << "Word: " << word << endl;
firsttime = false;
}
if (!firsttime)
cout << "Trail: NEWLINE" << endl;
else
cout << "empty line." << endl;
ss.str( string() );
ss.clear();
}
}
void LinkedList::readFunct(字符串文件){
字符串字;
字符串拖尾字符;
细流ss;
ifstream infle(file.c_str());
while(getline(infle,word)){
难道我是个白痴吗?我从来没有想过使用for循环。我想这是我对不睡觉的惩罚:P谢谢!我是个白痴。我从来没有想过使用for循环。我想这是我对不睡觉的惩罚:P谢谢!我从来没有真正看过peek函数。我需要好好阅读一下。每天学习新的东西。我从来没有真正看到过窥视功能。我需要仔细阅读。每天学习新的东西。