Ignoring characters in a file when parsing
I need to parse a text file and process the data. valid data is usually denoted by either timestamp with TS followed by 10 numbers (TS1040501134) or values with alpatet followed by nine numbers (A098098098) ... so it would be likeTS1040501134A111111111B222222222...........TS1020304050A000000000........
However, there are times when there will be no fillers when no data is available. So such a case could be
00000000000000000000TS1040501134A111111111B2222222220000000000TS1020304050A000000000........`
Now, as we can see, I need to ignore those zeros. How can i do this? I am using gnu C.
a source to share
My first attempt at something "C" like in about 20 years ... So what's next, at best, pseudocode!
Read in a line of text, then ...
char timestamp[11]; timestamp[10] = '\0';
char number[10]; number[9] = '\0';
for (i = 0 ; i < strlen(text); ) {
if isAlpha(text[i]) {
if text[i] == 'T' & text[i+1] == 'S' {
memcpy(timestamp, text[i+2], 10)
/* do whatever you do with a timestamp */
i += 12 /* Skip over timestamp */
} else {
memcpy(number, text[i+1], 9)
/* do whatever you do with a number */
i += 10 /* Skip over number */
}
} else {
if text[i] != '0' {
/* handle the error - should not get here */
}
i++ /* move to next character */
}
If the lines do not need to contain complete lines (for example, one line ends with TS10405
and the next line starts with 01134
), you will need to write additional code to update the buffer correctly text
.
a source to share
You should be able to read the file as a string, and then use strnstr () to find the substring "TS" in it. The string strnstr () returns the start of your timestamp.
To find the next timestamp, run strnstr in the same buffer with a pointer after the line you just found. If you are dealing with multiple lines, you will have to deal with a situation where one timestamp is split across multiple lines.
a source to share