[置顶] C语言解析lrc文件

本程序将歌词文件的时间和歌词分开来,并把 时间转换为微秒的单位。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXLINE 256
#define MAXBUF 256

char *tmp;

int chartoint(char ch){
	return ch - '0';
}

int strtoint(char *str){//计算时间,微秒 
	if(isdigit(str[0]) && isdigit(str[1])
		&& isdigit(str[0]) && isdigit(str[0])
		&& isdigit(str[0]) && isdigit(str[0])){
			int mintue = chartoint(str[0]) * 10 + chartoint(str[1]);
			int second = chartoint(str[3]) * 10 + chartoint(str[4]);
			int microsecond = chartoint(str[6]) * 10 + chartoint(str[7]);
			return (mintue * 60 + second) * 1000 + microsecond * 10;
		}
	return -1;
}

char * praseLRC(char *str, int *time){
	
	if(strlen(str) == 0){//空的行 
		return NULL;
	}else{
		char *p, *temp;
		 
		p = strchr(str, '[');
		if(p != NULL) 
			if(isdigit(*(p + 1))){
				temp = p + 1;
				p = strchr(str, ']');
				temp[p - temp] = '\0';
				//printf("%s\n", temp);
				if((*time = strtoint(temp)) < 0){
					perror("error time");
					exit(1);
				}
			    tmp = p + 1;
    			while(*p != '\n'){
    				p++;
   				}
   				tmp[p - tmp] = '\0';
   				//printf("%s", lrc);
		    	return tmp;
			}
		return NULL;
	}
	return NULL;
} 

int main(){
	char buf[MAXLINE];
	FILE * fd;
	int time[MAXBUF];
	int line = 0;
	char *lrc[MAXBUF];
	tmp = (char *)malloc(sizeof(char) * MAXLINE);
	if(tmp == NULL){
		perror("malloc buf err");
		return 1;
	}
	//fd = fopen("李玖哲 - 爱不需要理由.lrc", "r");
	//fd = fopen("龙梅子 - 你把爱情给了谁.lrc", "r");
	fd = fopen("王凝露 - 眼泪的错觉.lrc", "r");
	if(fd == NULL){
		perror("open file");
		exit(1);
	}
	while(fgets(buf, sizeof(buf), fd) != NULL){
		if((lrc[line] = praseLRC(buf, &time[line])) != NULL){
			printf("%d\t%s\n", time[line], lrc[line]);
			line++;
		}
	} 
	return 0;
} 
由于水平限制,目前只能解析图一格式的文件,而图二格式的文件还不能。

[置顶] C语言解析lrc文件_第1张图片[置顶] C语言解析lrc文件_第2张图片

运行完本程序,时间和歌词分别存放在两个数组里面。运行结果如下:

[置顶] C语言解析lrc文件_第3张图片


你可能感兴趣的:(c,File,null,语言)