I am making an id3 tag editor in C. I am having trouble figuring out how to pull the last 128 bytes off the end of the binary file in order to manipulate/printout the area where the id3 tag sits. Heres some code:
struct Tag{
char tagMark[3];
char trackName[30];
char artistName[30];
char albumName[30];
char year[4];
char comment[30];
char genre;
};
int main(int argc, char *argv[]){
struct Tag fileTag;
FILE *fp;
fp=fopen(argv[0], "r+b");
if(!fp){
printf("ERROR: File does not exist.");
}
int bufLength=129;
fseek(fp, 0, SEEK_END);
long fileLength=ftell(fp);
fseek(fp, fileLength-bufLength+1, SEEK_SET);
fread(&fileTag, sizeof(fileTag), 1, fp);
printf("%s\n", fileTag.tagMark);
return 0;
}
I am using a file to test this with that contains a properly formatted id3 tag. In an id3 tag, the first three bytes contain 'T', 'A', and 'G' respectively in order to identify that the tag exists. Does someone know why when I run this program, "_main" is the only thing that prints out?
Use fseek() (or lseek() if you're using file descriptors instead of file streams) with a negative offset (-128) bytes from the end of the file. Then read the 128 bytes of information.
Hence:
fseek(fp, -128L, SEEK_END);
lseek(fd, -128L, SEEK_END);
(Interestingly, the SEEK_END, SEEK_SET and SEEK_CUR macros are defined in both <stdio.h> for standard C and in <unistd.h> for POSIX.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With