- Ask the user for the name of an mp3 file to open or enter exit to exit program
ID: 3551088 • Letter: #
Question
- Ask the user for the name of an mp3 file to open or enter exit to exit program
- Open mp3 file and read ID3 tag information
- Display current tag information to user
- ask user if they want to change any tag information
- If they want to change something, ask use to enter only the information they wish to change
- Write new ID3 tag info to mp3
- ID3v1.1 tag info is the last 128 bytes of an mp3 file. each field of the tag is a fixed length.
Tag = 3 characters
Title = 30 characters
Artist = 30 characters
Album = 30 characters
Year = 4 characters
Comment = 28 characters
Zero = 1 byte
Track = 1 byte
Genre = 1 byte
Sample mp3 file : http://www20.zippyshare.com/v/99564579/file.html
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MP3_FILENAME "01-Cheeseburger in Paradise.mp3"
typedef struct{
char tag[3];
char title[30];
char artist[30];
char album[30];
char year[4];
char comment[30];
char genre;
} mp3Tag;
int main()
{
int in, input,i=0;;
char name[30];
FILE *fp = fopen(MP3_FILENAME, "rb");
if (!fp)
{
perror("File open failed");
return EXIT_FAILURE;
}
mp3Tag tag;
// Seek to 128 bytes before the end of the file
if (fseek(fp, -1 * sizeof(mp3Tag), SEEK_END) == -1)
{
perror("fseek failed");
return EXIT_FAILURE;
}
// Read the tag
if (fread(&tag, sizeof(mp3Tag), 1, fp) != 1)
{
fprintf(stderr, "Failed reading tag ");
return EXIT_FAILURE;
}
// Make sure we've got what we expect.
if (memcmp(tag.tag, "TAG", 3) == 0)
{
// Found the tag where we expected
printf("Title: %.30s ", tag.title);
printf("Artist: %.30s ", tag.artist);
printf("Album: %.30s ", tag.album);
printf("Year: %.4s ", tag.year);
if (tag.comment[28] == '')
{
printf("Comment: %.28s ", tag.comment);
printf("Track: %d ", tag.comment[29]);
}
else
{
printf("Comment: %.30s ", tag.comment);
}
printf("Genre: %d ", tag.genre);
}
else
{
fprintf(stderr, "Failed to find TAG ");
return EXIT_FAILURE;
}
printf("do you want to change the Tags, for yes enter 1 else 0:");
scanf("%d",&in);
if(in==1)
{
printf("which tag info you want to change: ");
printf("enter 1 for title and then enter title ");
printf("enter 2 for artist and then enter artist ");
printf("enter 3 for album and then enter album ");
printf("enter 4 for year and then enter year ");
printf("enter 5 for comment and then enter comment ");
printf("enter 6 for genre and then enter genre ");
scanf("%d ",&input);
gets(name);
if(input==1)
strcpy(tag.title,name);
else if(input==2)
strcpy(tag.artist,name);
else if(input==3)
strcpy(tag.album,name);
else if(input==4)
strcpy(tag.year,name);
else if(input==5)
strcpy(tag.comment,name);
else if(input==6)
{
printf("enter the new genre value:");
scanf("%d",input);
tag.genre=input;
}
if (memcmp(tag.tag, "TAG", 3) == 0)
{
// Found the tag where we expected
printf("Title: %.30s ", tag.title);
printf("Artist: %.30s ", tag.artist);
printf("Album: %.30s ", tag.album);
printf("Year: %.4s ", tag.year);
if (tag.comment[28] == '')
{
printf("Comment: %.28s ", tag.comment);
printf("Track: %d ", tag.comment[29]);
}
else
{
printf("Comment: %.30s ", tag.comment);
}
printf("Genre: %d ", tag.genre);
}
}
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.