解析时忽略文件中的字符

时间:2010-04-19 15:05:44

标签: c string parsing gcc

我需要解析文本文件并处理数据。有效数据通常用带有TS后跟10个数字的时间戳(TS1040501134)或带有alpabet后跟9个数字的值(A098098098)表示......所以它就像TS1040501134A111111111B222222222...........TS1020304050A000000000........

但是,有些情况下,当没有数据时会有填充0。所以,这种情况可能是

00000000000000000000TS1040501134A111111111B2222222220000000000TS1020304050A000000000........`

现在我们可以看到我需要忽略这些零。我怎么能这样做?我正在使用gnu C。

2 个答案:

答案 0 :(得分:0)

您应该能够将文件读入字符串,然后使用strnstr()在其中找到“TS”子字符串。字符串strnstr()返回将是时间戳的开头。

要查找下一个时间戳,请在刚刚找到的字符串后面的指针处的同一缓冲区上启动strnstr。如果处理多个字符串,则必须处理单个时间戳分割为多个字符串的情况。

答案 1 :(得分:0)

我第一次尝试'C'之类的东西 大约20年......所以接下来就是伪代码!

阅读一行文字,然后......

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 */
   } 

如果行不必包含完整的字符串(例如,一行以TS10405结尾 下一行以01134开头,您必须编写额外的代码来管理正确刷新text缓冲区。