我想从 mp3 文件中读取 mp3 标签:D 并将其保存到 txt 文件中。但是我的代码不起作用:(我的意思是我在我的 mp3 文件中设置正确位置时遇到了一些问题,请看一下:(为什么它不想工作?)。我必须自己做,没有额外的库。
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
int getFileSize(const char *filename)
{
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1;
}
int main(int argc, char **argv)
{
char *infile = "in.mp3", *outfile = "out.txt";
int infd, bytes_read = 0, buffsize = 255;
char buffer[255];
infd = open(infile, O_RDONLY);
if (infd == -1)
return -1;
int outfd = open(outfile, O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
if (outfd == -1)
return -1;
if(lseek(infd, -128, SEEK_END) < 0)
return -1;
for(;;)
{
bytes_read = read(infd, buffer, buffsize);
if (bytes_read > 0)
{
write(outfd, buffer, bytes_read);
}
else
{
if (bytes_read == 0)
{
if (close(infd) < 0)
return -1;
break;
}
else if (bytes_read == -1)
{
break;
return -1;
}
}
}
return 0;
}