Code:
char line[200], dummy;
double x, y;
if (fgets(line, sizeof line, stdin) == NULL) {
/* No more input: user pressed CTRL+D,
* or no more piped or redirected input.
* You should exit here, as there will
* not be any more input.
*/
} else
if (sscanf(line, " ( %lf , %lf ) %c", &x, &y, &dummy) == 2 ||
sscanf(line, " [ %lf , %lf ] %c", &x, &y, &dummy) == 2 ||
sscanf(line, " < %lf , %lf > %c", &x, &y, &dummy) == 2 ||
sscanf(line, " ( %lf %lf ) %c", &x, &y, &dummy) == 2 ||
sscanf(line, " [ %lf %lf ] %c", &x, &y, &dummy) == 2 ||
sscanf(line, " < %lf %lf > %c", &x, &y, &dummy) == 2 ||
sscanf(line, " %lf %*1[:;,/xX] %lf %c", &x, &y, &dummy) == 2) {
sscanf(line, " %lf %lf %c", &x, &y, &dummy) == 2) {
/* It was a 2D vector.
* The line did not contain anything except the vector
* (we verified that by scanning an additional "dummy" char).
* The components are in the x and y variables.
*/
} else {
/* Tell the user you couldn't understand the line, and exit.
* Or, tell the user about the expected formats, and retry.
*/
}
The