如何从char数组C ++中提取2个整数并将它们存储在2个变量中(初学者)

时间:2018-07-17 10:34:36

标签: c++ arrays char

我设法从.txt文档中提取一行并将其存储在char数组中

ifStream inData;
inData.open("test.txt');

char range1[40];
inData.getline(range1, 40);

我得到的输出是:

  

BaseIdRange = 0-8

我想将数字0和8存储在两种不同的数据类型中。 即int1 = 0和int2 = 8

非常感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

这是一个适用于2个无符号整数的示例:

public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if(view.getVisibility() == View.VISIBLE){
                handler.removeCallbacks(handlerRunnable);
                handler.postDelayed(handlerRunnable, initialInterval);
                touchedView = view;
                touchedView.setPressed(true);
                clickListener.onClick(view);
            }
            return true;    
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            if(view.getVisibility() == View.VISIBLE){
                handler.removeCallbacks(handlerRunnable);
                touchedView.setPressed(false);
                touchedView = null;
            }
            return true;
    }

    return false;
}
  • 可以使用#include <sstream> #include <string> int main() { char buffer[32]{ "BaseIdRange=0-8" }; // Input line // Clean all chars that are not one of 0-9): std::string chars = "0123456789"; // 'unsigned int' legitimate chars for (int i = 0; i < sizeof(buffer); i++) { if (chars.find(buffer[i]) == std::string::npos) // I.e not one of 0-9 buffer[i] = ' '; } std::stringstream ss(buffer); // Extract the 2 integers: unsigned int data[2]{ 0 }; for (int i = 0; i < 2; i++) { ss >> data[i]; } /* // Or (instead of the last for): unsigned int a = 0, b = 0; ss >> a; ss >> b; */ return 0; } 代替std::set<char> chars,并且可以将if行更改为std::string chars,但是我更喜欢简化它-if (chars.find(buffer[i]) == chars.end())的初始化是不太明显。