/*
将时间和方括号去掉, 其余内容全部打印
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct lrc
{
int time;
char song[200];
struct lrc *next;
} LRC;
/**
* @brief 将歌词文件插入到链表
* @param ppNodeLRC: 歌词
* @param midtime: 时间
* @retval None
*/
void HeadInsertLRCNode(LRC **ppNodeLRC, int midtime)
{
LRC *pMidLRC = (LRC *)malloc(sizeof(LRC));
pMidLRC->time = midtime;
memset(pMidLRC->song, 0, sizeof(pMidLRC->song));
if (*ppNodeLRC == NULL)
{
pMidLRC->next = NULL;
*ppNodeLRC = pMidLRC;
}
else
{
pMidLRC->next = *ppNodeLRC;
*ppNodeLRC = pMidLRC;
}
}
/**
* @brief 将歌词按时间排序
* @param pNode: 保存歌词的链表
* @retval None
*/
void SortTimeNodeLink(LRC *pNode)
{
int cnt = 0;
int midtime = 0;
char midsong[200] = {0};
LRC *pMidNode = pNode;
if (pNode != NULL)
{
if (pNode->next != NULL)
{
while (pNode != NULL)
{
cnt++;
pNode = pNode->next;
}
for (int i = 0; i < (cnt - 1); i++)
{
pNode = pMidNode;
for (int j = 0; j < (cnt - i - 1); j++)
{
if (pNode->time > pNode->next->time)
{
midtime = pNode->time;
strcpy(midsong, pNode->song);
pNode->time = pNode->next->time;
strcpy(pNode->song, pNode->next->song);
pNode->next->time = midtime;
strcpy(pNode->next->song, midsong);
}
pNode = pNode->next;
}
}
}
}
}
/**
* @brief 打印歌词
* @param pNode: 保存歌词的链表
* @retval None
*/
void Print_Link(LRC *pNode)
{
while (pNode != NULL)
{
printf("%s\n", (*pNode).song);
pNode = pNode->next;
}
printf("LRC is already printed\n");
}
/**
* @brief 释放链表
* @param pNode: 被释放的链表
* @retval None
*/
void Free_Link(LRC *pNode)
{
while (pNode != NULL)
{
free(pNode);
pNode = pNode->next;
}
}
int main()
{
char al[100] = {0}; // 专辑
char ar[100] = {0}; // 歌手
char by[100] = {0}; // 作词
char ti[100] = {0}; // 歌曲名
LRC *pHeadLRC = NULL; // 保存歌词文件
LRC *pCopyLRC = NULL;
char LrcStr[200] = {0}; // 歌词
char *PLrcStr = LrcStr;
FILE *lrc_fp = fopen("./test.lrc", "r+");
if (NULL == lrc_fp)
{
perror("main_fopen01");
return -1;
}
else
{
/*
从 fp 指向的文件中读取一行不超过 200 字节的数据,
直到读取到文件末尾
*/
while (NULL != fgets(LrcStr, 200, lrc_fp))
{
int min = 0; // 分
int sec = 0; // 秒
if (sscanf(PLrcStr, "[ti:%s", ti)) // 拿取文件内容, 存入到 ti 数组
{
int i = 0;
while (*(ti + i) != ']')
{
i++;
}
*(ti + i) = 0;
}
if (sscanf(PLrcStr, "[ar:%s", ar))
{
int i = 0;
while (*(ar + i) != ']')
{
i++;
}
*(ar + i) = '\0';
}
if (sscanf(PLrcStr, "[al:%s", al))
{
int i = 0;
while (*(al + i) != ']')
{
i++;
}
*(al + i) = '\0';
}
if (sscanf(PLrcStr, "[by:%s", by))
{
int i = 0;
while (*(by + i) != ']')
{
i++;
}
*(by + i) = '\0';
}
while (2 == sscanf(PLrcStr, "[%d:%d", &min, &sec)) // 获得歌词时间
{
HeadInsertLRCNode(&pHeadLRC, min * 60 + sec);
PLrcStr = PLrcStr + 10;
}
if (pCopyLRC != pHeadLRC)
{
pCopyLRC = pHeadLRC;
while (pCopyLRC != NULL)
{
if (pCopyLRC->song[0] == 0)
{
strcpy(pCopyLRC->song, PLrcStr);
}
else
{
break;
}
pCopyLRC = pCopyLRC->next;
}
}
pCopyLRC = pHeadLRC;
PLrcStr = LrcStr;
memset(LrcStr, 0, sizeof(LrcStr));
}
fclose(lrc_fp);
SortTimeNodeLink(pHeadLRC);
printf("Song: %s\n", ti);
printf("Singer: %s\n", ar);
printf("album: %s\n", al);
printf("lyricist: %s\n", by);
LRC *p = pHeadLRC;
Print_Link(pHeadLRC);
Free_Link(pHeadLRC);
pHeadLRC = NULL;
}
return 0;
}
本文为原创文章,转载请注明出处!
admin:系统自动奖励,+10,