2012-01-27 56 views

回答

5

使用sscanf

int a, b, c; 
if (sscanf(inData, "%d,%d,%d", &a, &b, &c) == 3) 
{ 
    // Got all three numbers converted, go ahead and use them. 
} 
+0

工作。謝謝。 – user1165574 2012-01-27 14:35:41

+1

由於這顯然是一個C++問題,因此使用C++方法而不是C庫函數可能會更好。 – 2012-01-27 14:37:06

2
#include <iostream> 
#include <sstream> 

using namespace std; 

int main() 
{ 
    char inData[] = "1,13,1"; 
    istringstream inDataStream(inData); 

    int a, b, c; 
    char x; 
    inDataStream >> a >> x >> b >> x >> c; 

    cout << a << ", " << b << ", " << c << endl; 

    return 0; 
}