02-10-2006 02:03 AM - edited 02-10-2006 02:03 AM
Message Edited by Vector on 02-10-2006 02:07 AM
02-10-2006 04:09 AM - edited 02-10-2006 04:09 AM
To split a string with comma-separated values, you can use Scan function, provided that you use the correct formatting string to separate each field in the input string.
To scan comma-separated numbers you can use Scan (inputString, "%d[x]%d", &num1, &num2); this example scans two number discarding the separatos, which can be any non-numeric character (comma, semicolon, tab character...). To scan strings it is necessary to know the exact separator, since the function becomes Scan (inputString, "%s[dxt44]%s", subString1, subString2); in this case "44" stands for a colon, while "59" means semicolon: it is the ascii code associated with the separator character. The separator itself is discarded via the use of "d" operator. The online help for the formatting and I/O library is very detailed on this matter to it's a good option to study it carefully.
The matter becomes considerably more difficultin case (as I can seem to understand) your input string has not always the same structure (i.e. numbers and tests are mixed in non-constant way). In this case you'll have to use a switch with a bunch of cases for each line structure you expect. In case the line structure is perfectly random, I suggest you to use the same functions reading all inputs as strings and detecting the type by using strpbrk function on each field to search for numbers.
Now the output side of your problem: you could use indifferently a table or a list box. In case your columns have not always the same type as in your example, and provided that the operator is not intended to modify directly the content of individual cells, I have found more handy a listbox. You can look at listdelx example in <cvidir>\samples\userint directory: it shows how to display elements in columns and with different colors.
In case you do prefere to use a table, you'll need to add a new row for each line of data and customize individual cells based on cell content.
Message Edited by Roberto Bozzolo on 02-10-2006 11:11 AM
02-10-2006 12:25 PM
02-10-2006 03:33 PM
02-15-2006 02:41 PM
Hi Roberto / All
I am not able separate comma string by using above Scan function.
Can you/Somebody help me to separate following string by using scan function.(what I have to put inside scan function)
input string : 121,abcd,11,49.11
menas : integer,string,integer,float.
Thanks a lot
02-15-2006 03:48 PM - edited 02-15-2006 03:48 PM
Try
Scan (string, "%d[x]%s[dxt44]%d[x]%f", &integer1, string1, &integer2, &double);
Message Edited by mvr on 02-15-2006 03:49 PM
02-15-2006 07:07 PM
Hi
It is saying too many parameters.
I am not able to find what am i doing wrong. Can you please check.
Thanks for help!!
02-16-2006 12:02 AM
02-16-2006 02:20 AM
Hi Roberto,
Here is my code.
////////////////////////////////
int serialNo,unitNo;
double weight;
char *partNo;
partNo = calloc(1,15);
sprintf(data,"4,abcde,21,9") ;
Scan (data, "%d[x]%s[dxt44]%d[x]%f", &serialNo, partNo, &unitNo, &weight);
///////////////////////////
02-16-2006 02:58 AM
d | Discard data | When applied to a target specifier, the d modifier indicates that no target argument corresponds to the target specifier. The functions discard the data the functions would otherwise place in the target argument. |