Parsing lrc files in C Language

Source: Internet
Author: User

This program separates the Time of the lyrics file from the lyrics file, and converts the time to the unit of microseconds.

# 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) {// computing time, microsecond 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) {// empty row 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;} 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 ("Li xiaozhe-love does not need a reason. LRC "," R "); // FD = fopen (" longmeizi-who did you give love. LRC "," R "); FD = fopen (" Wang ninglu-the illusion of tears. 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 ;}

Due to horizontal restrictions, only files in Figure 1 format can be parsed, while files in Figure 2 format cannot.

After running this program, the time and lyrics are stored in two arrays respectively. The running result is as follows:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.