0
我目前正在尝试从C中的ID3V1.1标签读取信息。对于此项目,我们不允许使用任何外部库。除轨道字段以外的所有字段均可正确读取。最后两行是给我麻烦的。每当我运行程序时,当它尝试获取曲目编号时,我会遇到seg故障。我试图使用gdb进行调试,并且它表示问题发生在fseek所在的第34行。它适用于其他领域,所以我想知道为什么它出错了。我应该将偏移量更改为-128以外的其他值吗?但整个标签只有128个字符,所以我不确定发生了什么问题。错误从ID3V1标签读取跟踪
#include <stdio.h>
#include <string.h>
struct Tag
{
char tag[3];
char song_title[30];
char artist[30];
char album[30];
char year[4];
char comment[28];
char seperator;
char track;
char genre;
};
int main (int argc, char *argv[])
{
struct Tag file_tag;
FILE *fp;
char title[31];
char artist[31];
char album[31];
char year[5];
char comment[29];
char track[2];
int track_number;
fp = fopen(argv[1],"r+b");
if (!fp)
{
printf("File does not exist");
}
fseek(fp, -128, SEEK_END);
fread(&file_tag,sizeof(file_tag),1,fp);
fclose(fp);
if(strncmp(file_tag.tag,"TAG",3)!=0)
{
printf("ID3 tag is not present\n");
}
else
{
strncpy(title, file_tag.song_title,30);
title[31]='\0';
printf("Title: %s\n",title);
strncpy(artist, file_tag.artist,30);
artist[31]='\0';
printf("Artist: %s\n",artist);
strncpy(album, file_tag.album,30);
album[31]='\0';
printf("Album: %s\n",album);
strncpy(year, file_tag.year,4);
year[4]='\0';
printf("Year: %s\n",year);
//printf("Year: %.4s\n",file_tag.year);
strncpy(comment, file_tag.comment,28);
comment[29]='\0';
printf("Comment: %s\n",comment);
//these lines cause the seg fault
track_number = atoi(file_tag.track);
printf("Track: %d\n",track_number);
}
return 0;
}
这里是段错误的完整信息:
Program received signal SIGSEGV, Segmentation fault.
0x00000034ff66edf1 in fseek() from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6_5.3.x86_64
(gdb) back
#0 0x00000034ff66edf1 in fseek() from /lib64/libc.so.6
#1 0x000000000040076f in main (argc=1, argv=0x7fffffffdf58) at id3tagEd.c:34
你可以尝试使用SEEK_SET和SEEK_END而无偏移量来查找你的文件是否比预期的缩短? – 2015-02-08 03:29:29
是的,确认标签存在于文件的最后128个字符中。所以赛道应该在那里,但我不确定发生了什么。该文件是我期望的大小 – ComicStix 2015-02-08 05:48:04
看起来'file_tag.track'在传递给atoi之前应该是空终止的。 – nwellnhof 2015-02-08 11:30:40