LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

String delimiter

Hi!

I have a log file, and now, I have to read and process it, line by line.

To the work become easier, I would like to separate the words of each line, so I can read all data and interpret it.

For example:

tab1    control   25   100   control_constant   control_type   control_color

As you could see, I used the \t as a delimiter of the data log.

I would like to get this information and put it in a vector.

As a result I would lie to get something like this:
data[0] = tab1
data[1] = control
data[2] = 25
data[3] = 100
data[4] = control_contant
...

Does someone has any idea how to do it? It will be great if someone post an example of how I can do it.

Regards

0 Kudos
Message 1 of 3
(3,102 Views)
I suggest you to use the Scan function from the Formatting and I/O Library, using an appropriate delimiter to separate individual items; your sample string could be read as follows:
 
Scan (line, "%s[t9]%s[t9]%d%d%s[t9]%s[t9]%s[t9]", string1, string2, integer1, integer2, string3, string4, string5);
 
The online help for the scan function is very detailed and several examples help clarify all the matter.
 
 
If you prefere to use standard C library instead, you could use strtok function to separate the items in the line; in this case you may want to keep an index of the item read to help decode the item in the appropriate way:
 
int     index;
char    line[256], *item;
 
// Read a line from the file
index = 0;
item = strtok (line, "\t");
while (item) {
    switch (index) {
        case 0:   // The first element
           // Copy in the array
       case xxx:
          ....   // Other elements read
    }
    // Decode the item
    item = strtok (NULL, "\t");   // When item == NULL the string is finished
}
 


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(3,094 Views)

A warning about using strtok. strtok treats multiple occurances of the seperator as a single occurance. For instance if the file contains "one,two,,four" and you use the "," as the seperator, you will only get 3 token:

0 = one

1 = two

3 = four

This may be what you want, but you would need to be aware of this behavioral difference between scan and strtok.

Message 3 of 3
(3,057 Views)