Question: 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?

Answer: 
Use [`fseek()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fseek.html) (or [`lseek()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html) 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.)
Your program does

```
fp=fopen(argv[0], "r+b");

```

argv[0] contains the name of the running executable, you want `argv[1]`, the first command line parameter. The next problem you'll probably run into (when starting with ID3v2) is structure field alignment, depending on the datatypes used the compiler may leave "gaps" between the consecutive members of a structure. To avoid this problem the compiler should be instructed to align the structure on byte boundaries with (in most cases, check your compiler's documentation)

```
#pragma pack(1) 

```

This [Structure padding and packing](https://stackoverflow.com/questions/4306186/structure-padding-and-structure-packing) SO answer explains clearly what happens if you don't use the correct packing.

Also, see [the ID3V1 layout](http://en.wikipedia.org/wiki/Id3#Layout), there's not always a terminating 0 after the individual fields, so

```
printf("%s\n", fileTag.tagMark);

```

will print TAG followed by the song title (printf %s only stops when it encounters a \0).

IMHO, given the jungle that ID3 is you're probably better off delegating the raw ID3 manipulation to an existing library like [id3lib](http://id3lib.sourceforge.net) and focus on the functionality of your editor.