首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据空格和逗号拆分输入字符串

根据空格和逗号拆分输入字符串
EN

Stack Overflow用户
提问于 2016-05-02 12:04:03
回答 2查看 12.1K关注 0票数 3

我正在从文件中读取格式化的行,需要对其进行解析并将一些结果输出到文件中。我遇到的问题是正确解析输入以处理空格和逗号。文件中的行格式可以是以下格式:

代码语言:javascript
复制
a r3, r2, r1
ah r8, r5, r6, r7
ai r10, i10, r127

所以我可以有4-5个不同的元素。第一个元素是"command",然后是一个空格,然后是用逗号和空格分隔的值。

我目前的实现如下:

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <bitset>
using namespace std;

void main()
{
    string instruction;
    vector<string> myString;
    ifstream inFile;
    inFile.open("myfile.txt");
    int i = 0;


    if (inFile.is_open()){
        cout << "test" << endl;
        //Read until no more lines in text file to read
        //while (getline(inFile, instruction))
        while (inFile >> instruction)
        {

            istringstream ss(instruction);
            string token;
            //Separate string based on commas and white spaces
            while (getline(ss, token,',')){
                //getline(token, token, ' ');

                //Push each token to the vector
                myString.push_back(token);
                cout << myString[i] << endl;
                i++;
            }

        }
    }
    system("pause");
}

但这是行不通的。如果我用a r3, r2, r1测试它,我只得到a,程序会停止读取,因为它看到了空格。

我尝试的另一个版本是将文件的读数更改为while (getline(inFile, instruction)),但这会将我的字符串拆分为:

代码语言:javascript
复制
a r3
r1
r2

它认为(空格)r3是一个元素。它根据逗号正确地拆分了整行,但最初的读取是不正确的,因为我还需要它拆分a和r3。我该怎么做呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-02 12:23:51

这将完成这项工作。

代码语言:javascript
复制
if (inFile.is_open()){
    cout << "test" << endl;
    //Read until no more lines in text file to read
    while (getline(inFile, instruction))
    {

        istringstream ss(instruction);
        string token;
        //Separate string based on commas and white spaces
        getline(ss,token,' ');
        myString.push_back(token);
        cout << myString[i] << endl;
        i++;

        while (getline(ss, token,',')){
            ss.ignore();
            myString.push_back(token);
            cout << myString[i] << endl;
            i++;
        }

    }
}

点数:

getline(inFile,instruction)会给你整条线

getline(ss,token,' ')读取到空格

getline(ss,token,',')读取到逗号

最后是ss.ignore(),用来忽略流中的字符。

重要的是要注意,如果到达流的末尾,getline将停止。

默认情况下,您的inFile>>instruction会读取while空格。要读取整行内容,必须使用getline。

票数 7
EN

Stack Overflow用户

发布于 2016-05-02 12:11:10

这就是了:

http://en.cppreference.com/w/cpp/string/basic_string/find_first_of

size_t find_first_of( const basic_string& str, size_type pos = 0 )

搜索字符串中的任何字符...

代码语言:javascript
复制
#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s = "this is some, text";
  size_t position = -1;
  do {
      position = s.find_first_of(" ,", position+1);
  } while(position != std::string::npos);

}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36974817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档