相顾无言,唯有贴代码,请上眼:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define STRLEN 256
//只是去掉时间标记
char *parse_lrc_line(char *line)
{
char *p = NULL;
p = strrchr(line,']');
if(p == NULL)
{
goto END;
}
p = p + 1;
END:
return p;
}
//在其中解析包含时间标签的LRC文件的字符串,并输出到stdout,略微修改即具有实用价值(编码还是要改的)
void parse_lrc_line2(char *line)
{
char *p = NULL;
char *right = NULL;
char *left = NULL;
char buff[STRLEN] = {0};
char *p2 = NULL;
int n;
p = line;
//left为左边的[所在的位置
left = strchr(p,'[');
if (left == NULL)
{
return ;
}
//right为右边的]所在的位置
right = strchr(p,']');
while(right != NULL && left != NULL)
{
n = right - left - 1;
strncpy(buff,left + 1,n);
puts(buff);
p2 = strrchr(p,']');
if (p2 != NULL)
{
p2++;
puts(p2);
}
//可以在这里保存歌词和时间标签信息,此时时间标签为buff,歌词内容为p2
//这里把buff清空为\0。。反正总归要清空的,这样就不用计算字符串长度再补\0了
memset(buff,'\0',n);
p = right;
//strchr函数只向后查找,把这段字符串划分成两个
*p = '\0';
p = p + 1;
left = strchr(p,'[');
if (left == NULL)
{
continue;
}
right = strchr(p,']');
}
}
int main()
{
FILE *f = NULL;
char line[STRLEN] = {0};
f = fopen("qclg.lrc","r");
assert(f);
while(fgets(line,STRLEN,f))
{
//parse_lrc_line是去标签的
printf(parse_lrc_line(line));
}
rewind(f);
//读取每一行,并在其中解析
while(fgets(line,STRLEN,f))
{
//每行解析一次
parse_lrc_line2(line);
}
getchar();
fclose(f);
}