LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2019, 11:47 AM   #1
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
Unhappy outputting gibberish


I have been working on an id3 tag editor and am about to wrap up the reading of the tag. I thought my code would work, but it turns out I am wrong When I run my code I get gibberish and I can't figure out why... Oh, this is for ID3V2 in case that makes a difference...

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

	/* variables */
	char TAG[3];
	char title[30];

	char artist[30];
	char album[30];

	char comment[30];
	short date;

	short track_number;
	char genre[30];

	char album_artist[30];
	char lyrics[30];

	char cover[30];

	typedef struct {
		char TAG[3];
		char title[30];

		char artist[30];
		char album[30];

		char comment[30];
		short date;

		short track_number;
		char genre[30];

		char album_artist[30];
		char lyrics[30];

		char cover[30];
	}id3;

	id3 *tag = calloc(1, sizeof(id3));

	FILE *song;
	song = fopen(argv[1], "r+b");

	/* set postion of mp3 file and read id3 tag */
	fseek(song, -128, SEEK_END);
	fread(&tag->TAG, 1, sizeof(&tag->TAG), song);

	fread(&tag->title, 1, sizeof(&tag->title), song);
	fread(&tag->artist, 1, sizeof(&tag->artist), song);

	fread(&tag->album, 1, sizeof(&tag->album), song);
	fread(&tag->comment, 1, sizeof(&tag->comment), song);

	fread(&tag->date, 1, sizeof(&tag->date), song);
	fread(&tag->track_number, 1, sizeof(&tag->track_number), song);

	fread(&tag->genre, 1, sizeof(&tag->genre), song);
	fread(&tag->album_artist, 1, sizeof(&tag->album_artist), song);

	fread(&tag->lyrics, 1, sizeof(&tag->lyrics), song);
	fread(&tag->cover, 1, sizeof(&tag->cover), song);

	/* close file */
	fclose(song);

	/* print out tag */
	printf("TAG: %s\n", tag->TAG);
	printf("title: %s\n", tag->title);

	printf("artist: %s\n", tag->artist);
	printf("album: %s\n", tag->album);

	printf("comment: %s\n", tag->comment);
	printf("date: %d\n", tag->date);

	printf("track number: %d\n", tag->track_number);
	printf("genre: %s\n", tag->genre);

	printf("album artist: %s\n", tag->album_artist);
	printf("lyrics: %s\n", tag->lyrics);

	printf("cover: %s\n", tag->cover);

	return 1;

}
When I run this on a file called "d_b.mp3" I get really weird output:

Code:
TAG: Zbn
title: n
artist: \ Ȋ3
album: 4Z
comment: 3cRBR
date: s<U|UV
track number: bHg
genre: i4
album artist: !!Ԃ3
lyrics: ke

cover: 6

0xh
I hate to post here again so soon, but I am confused. Shouldn't this print the actual tag? For example title should "Beautiful Disaster".

Last edited by FOSSilized_Daemon; 06-20-2019 at 12:23 PM.
 
Old 06-20-2019, 12:29 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,156
Blog Entries: 11

Rep: Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077
Quote:
Originally Posted by FOSSilized_Daemon View Post
I hate to post here again so soon, but I am confused.
This is clearly a continuation of your previous tag related code project, please do not do that. Also a more descriptive title will help you receive more specific replies.

The other thread has been marked SOLVED so this one will be left open for now, but in future please post related questions to a single thread to make it easier for others to follow a continuing discussion, be warned that similar threads will be merged going forward.

Saying that it produces weird output without providing any example of what the input data structure actually is is not very helpful to others.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!

In future, please try to work your problem down to a minimal, complete example which demonstrates the problem you are experiencing. This will help others to more quickly understand the problem and very often you will understand and solve the problem in the process before asking the question! This is a great way to build your own troubleshooting skills!

Last edited by astrogeek; 06-20-2019 at 12:32 PM.
 
Old 06-20-2019, 12:32 PM   #3
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Sorry about that, the forum I used before I started using LQ didn't allow using a single thread like that. But I will change that in the future, just habit. I did try to research the issue and tested some others code, but couldn't find anything that helped break the issue down. I thiiiink it might have to do with pulling data from the wrong section of the tag, but I am unsure how that could be.
 
Old 06-20-2019, 12:48 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,156
Blog Entries: 11

Rep: Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077
Quote:
Originally Posted by FOSSilized_Daemon View Post
I thiiiink it might have to do with pulling data from the wrong section of the tag, but I am unsure how that could be.
Well, have you verified the tag structure of the file?

Begin by comparing the specification of that tag structure with what is actually in the file. Is is correct?

If so, pick one tag and write a minimal code block to read that one tag... does it work as expected? If so extend that to the others incrementally and verify each one. If not, answer the single question, "Why?".

Learn to build and troubleshoot your code incrementally, not in large blocks.
 
2 members found this post helpful.
Old 06-20-2019, 01:05 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,963

Rep: Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849Reputation: 3849
You know, you should really be using id3lib for this.
 
1 members found this post helpful.
Old 06-20-2019, 01:11 PM   #6
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by astrogeek View Post
Well, have you verified the tag structure of the file?

Begin by comparing the specification of that tag structure with what is actually in the file. Is is correct?

If so, pick one tag and write a minimal code block to read that one tag... does it work as expected? If so extend that to the others incrementally and verify each one. If not, answer the single question, "Why?".

Learn to build and troubleshoot your code incrementally, not in large blocks.
Everything I have read says that ID3v2 doesn't have a tag structure, unless I am misunderstanding the documentation (see here: http://id3.org/id3v2.4.0-structure) my code should work. I have encountered other peoples code who have tried to do similar things, but I can't find why my code is pulling what it is. Nothing points to me accessing the wrong data this makes no sense. I can't figure out what here is causing this to go wrong. I have checked stack overflow, a few git projects I am seriously confused about why this isn't printing the actual tag. I have verified what the tag should look like with kid3 and even tried other files I just don't understand this should be working...

I am just retarded and should probably just off myself I can't even understand this and my life is worthless if I can't do anything like this, i don't even care anymore forgot all of this I am too stupid to do BASIC DAY ONE code. I am 17 and in 12th grade I should know this by now everyone knows so much more than me and all I can do is stare at code and get confused! Anyone else here would have written this 100 times over by now. what is wrong with me? I just want to learn and be a good programmer and all I can do is be stupid.

Last edited by FOSSilized_Daemon; 06-20-2019 at 01:43 PM.
 
Old 06-20-2019, 01:12 PM   #7
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by dugan View Post
You know, you should really be using id3lib for this.
I don't like using libs, I know they make coding easier as they usually do the low pain in butt stuff; but I find that they bloat things up and then I never end up using 90% of what they do or I want to add something and then I have to add to the lib or have it look weird...
 
Old 06-20-2019, 01:56 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,799
Blog Entries: 13

Rep: Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285Reputation: 3285
Ok.

So what dugan said?

id3lib source is available, so while you may not wish to use it, as well as do this task for your own learning experience? There's reference code where you can see how it's done.
Quote:
Originally Posted by FOSSilized_Daemon View Post
I am just retarded and should probably just off myself I can't even understand this and my life is worthless if I can't do anything like this, i don't even care anymore forgot all of this I am too stupid to do BASIC DAY ONE code. I am 17 and in 12th grade I should know this by now everyone knows so much more than me and all I can do is stare at code and get confused! Anyone else here would have written this 100 times over by now. what is wrong with me? I just want to learn and be a good programmer and all I can do is be stupid.
Harsh lesson here? Save it! I get it that it should be easy but you're having difficulties right now. Ok, so find a way to work smarter and not harder. My personal thought processes do say, "This isn't rocket science. Other people manage to do this, so I can learn it."

But I say these things to myself, and I pull up my bootstraps and I work at it.

I see/hear someone spout out all that you did there, I first say to not get discouraged and to please not attack yourself in this manner.

Life experience shows me that many times people always say this, they continue to say it. So it gets old and personally I then ignore them.

That's just friendly advice. I fully get that the task is frustrating you.
 
2 members found this post helpful.
Old 06-20-2019, 02:35 PM   #9
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
I just feel so helpless sometimes. I understand that a lot of the people I see who code a lot of big projects have been doing this for years and have college level experince (Theo, Linus, Lunduke, Luke Smith etc), but sometimes I feel like I should just know this stuff. On the last forum I used everyone was kind of the Arch archetype they made everyone who wasn't using their stuff feel bad for doing so, and that's why I came here. Everyone here is so nice, when I started coding I started with Python; I liked it until I hit classes and such. I am not one of those "Die OOP!" people, I like Python; but I didn't like classes and such as I felt like they were overly complex. So someone told me about C as they knew I have a life goal of doing Operating System Development. So I started C and love it, structs are simple and I could understand it even pointers (I mean bugs happen, but the concepts made sense). I just feel like sometimes if I hit bugs and errors then I am a bad programmer as so many others out there make it seem that way (not on LQ, but just in the world). I know it seems silly when I think about it, but every now and then...
 
Old 06-20-2019, 03:29 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,156
Blog Entries: 11

Rep: Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077
Quote:
Originally Posted by FOSSilized_Daemon View Post
Everything I have read says that ID3v2 doesn't have a tag structure, unless I am misunderstanding the documentation (see here: http://id3.org/id3v2.4.0-structure) my code should work.
Why should it work? If you cannot demonstrate that you have correctly synched to the tag data and identified a clearly defined tag, there is no basis on which to say it should work.

I don't know much about such media tags other than what I have read related to other people's questions. But clearly that document specifies how to detect and synchronize to the tag data. You have set your goal too high trying to do it all in one go! Write a simplest code example which unambiguously synchs to the tag data, or produces a message if it fails to do so. Explore how that works on different example files and understand it and write yourself an explanation of how you understand it to work in terms of the specification and actual data!

There simply is no substitute for understanding - do that first and all other problems melt away.

Then add a smallest block which reliably extracts a single field of tag data from those same example files. Explore it and understand it, and add to your own notes.

Keep your example files organized and refer to them often, and build on them and on your own understanding.

Programming is not about trying stuff until something works then rushing on to the next random thought. Programming is about understanding each concept in isolation, then assembling the various well understood bits to perform some useful task. And it takes time and dedicated effort.

Quote:
Originally Posted by FOSSilized_Daemon View Post
I am just retarded and should probably just off myself I can't even understand this and my life is worthless if I can't do anything like this, i don't even care anymore forgot all of this I am too stupid to do BASIC DAY ONE code. I am 17 and in 12th grade I should know this by now everyone knows so much more than me and all I can do is stare at code and get confused! Anyone else here would have written this 100 times over by now. what is wrong with me? I just want to learn and be a good programmer and all I can do is be stupid.
I too get the frustration, but please quit with self deprecating whine, it is not appropriate here, or just about any where else. Why do you think you should know this by now? Did you absorb it through the skin or dream it one night? Perhaps you have held a book about it in your hands and thought that was enough? It doesn't work like that.

You should only know it if you have put in the diligent, methodical long term effort to learn it... clearly you have not yet done that, you have only taken the first few small steps. It is a long path with many steep climbs along the way. Walk, don't run, and learn to test your footing each step of the way.

Quote:
Originally Posted by FOSSilized_Daemon View Post
I don't like using libs, I know they make coding easier as they usually do the low pain in butt stuff; but I find that they bloat things up and then I never end up using 90% of what they do or I want to add something and then I have to add to the lib or have it look weird...
So you think it should be faster for you to figure it all out and invent a new solution from scratch than to avail yourself of the work of others who have already solved it?

Is it possible that learning your way around the libs looks like more effort than you want to put into it, so you imagine that you can find a shortcut straight to the desired ends without taking time to understand the landscape? We all do it at one time or another, and we usually end up bruised and frustrated in the end... and further behind than when we started. Don't do that.
 
Old 06-20-2019, 04:46 PM   #11
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by astrogeek View Post
Why should it work? If you cannot demonstrate that you have correctly synched to the tag data and identified a clearly defined tag, there is no basis on which to say it should work.

I don't know much about such media tags other than what I have read related to other people's questions. But clearly that document specifies how to detect and synchronize to the tag data. You have set your goal too high trying to do it all in one go! Write a simplest code example which unambiguously synchs to the tag data, or produces a message if it fails to do so. Explore how that works on different example files and understand it and write yourself an explanation of how you understand it to work in terms of the specification and actual data!

There simply is no substitute for understanding - do that first and all other problems melt away.

Then add a smallest block which reliably extracts a single field of tag data from those same example files. Explore it and understand it, and add to your own notes.

Keep your example files organized and refer to them often, and build on them and on your own understanding.

Programming is not about trying stuff until something works then rushing on to the next random thought. Programming is about understanding each concept in isolation, then assembling the various well understood bits to perform some useful task. And it takes time and dedicated effort.



I too get the frustration, but please quit with self deprecating whine, it is not appropriate here, or just about any where else. Why do you think you should know this by now? Did you absorb it through the skin or dream it one night? Perhaps you have held a book about it in your hands and thought that was enough? It doesn't work like that.

You should only know it if you have put in the diligent, methodical long term effort to learn it... clearly you have not yet done that, you have only taken the first few small steps. It is a long path with many steep climbs along the way. Walk, don't run, and learn to test your footing each step of the way.



So you think it should be faster for you to figure it all out and invent a new solution from scratch than to avail yourself of the work of others who have already solved it?

Is it possible that learning your way around the libs looks like more effort than you want to put into it, so you imagine that you can find a shortcut straight to the desired ends without taking time to understand the landscape? We all do it at one time or another, and we usually end up bruised and frustrated in the end... and further behind than when we started. Don't do that.
I just struggle with my self-esteem sometimes, everyone is doing amazing things and I feel like I am far behind... and some people make fun of me for reviewing others code and it makes me worry that I am a bad student
 
Old 06-20-2019, 05:09 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,156
Blog Entries: 11

Rep: Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077Reputation: 3077
For starters, don't compare yourself to everyone else - that is irrelevant.

You are learning to program computing machines, which is itself an amazing thing, and something still new in the overall scheme of human experience! How many of your fellow students aren't doing that? Again, irrelevant...

The best fix for uncertain self-esteem is confidence.

Confidence in any endeavor comes from one source - understanding what you are doing! So put ALL your effort into understanding the essentials of any task to the point that when you know something, you know it with certainty, and when you don't know something, you have a very good idea where to begin to get the missing understanding, and quickly! Put NO effort into competing with others on appearances! Arm yourself with knowledge and understanding and you will have little competition in the amazing feats arena!

Decide what you actually want to accomplish, then become the best at it... you can do it. Anyone can do it.

You say you are 17 years old, so hopefully you will be an adult some day! (humor) Don't think so much about what you aren't, think about what you want to be, then steer that direction - with growing confidence!

Good luck!

Last edited by astrogeek; 06-20-2019 at 05:13 PM.
 
2 members found this post helpful.
Old 06-20-2019, 05:23 PM   #13
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,272

Rep: Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106Reputation: 1106
In addition to the excellent advice astrogeek has given you, FOSSilized_Daemon, I feel a need to point out that most of what you've been asking about of late is way outside my realm of experience...and I've been programming computers since about 1977 (!)

Yes, I know how to do lots of stuff, but more importantly, I know there's lots of stuff I don't even have a clue about.

It's not possible to learn it all, ever. As astrogeek has said, take it one small step at a time.

Hang in there. You are making great progress!
 
2 members found this post helpful.
Old 06-20-2019, 05:24 PM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,728

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
Quote:
Originally Posted by FOSSilized_Daemon View Post
I just struggle with my self-esteem sometimes, everyone is doing amazing things and I feel like I am far behind...
Everybody wants to be a star.
The Olympic Gold Medal winner.
The Oscar winning actor.
The Nobel Prize winning scientist.
The social media billionaire.

There's nothing wrong with aspiring to be solidly competent in any field you choose. A doctor who makes sick people well. A brickmason who builds a durable wall. A carpenter who builds a sturdy shed. A programmer who produces (almost) bug-free code.

You won't be the next Bill Gates or Steve Jobs or Linus Torvalds or Mark Zuckerberg. Neither will I. You don't have to be a star. You can be a contributor.

The fundamental goal of every human being: make this world a bit better when you leave it than it was when you entered it. Be honest. Be decent. Follow the Golden Rule. Do all that, and have some fun along the way.

Daniel B. Martin

.
 
4 members found this post helpful.
Old 06-20-2019, 05:29 PM   #15
FOSSilized_Daemon
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by astrogeek View Post
For starters, don't compare yourself to everyone else - that is irrelevant.

You are learning to program computing machines, which is itself an amazing thing, and something still new in the overall scheme of human experience! How many of your fellow students aren't doing that? Again, irrelevant...

The best fix for uncertain self-esteem is confidence.

Confidence in any endeavor comes from one source - understanding what you are doing! So put ALL your effort into understanding the essentials of any task to the point that when you know something, you know it with certainty, and when you don't know something, you have a very good idea where to begin to get the missing understanding, and quickly! Put NO effort into competing with others on appearances! Arm yourself with knowledge and understanding and you will have little competition in the amazing feats arena!

Decide what you actually want to accomplish, then become the best at it... you can do it. Anyone can do it.

You say you are 17 years old, so hopefully you will be an adult some day! (humor) Don't think so much about what you aren't, think about what you want to be, then steer that direction - with growing confidence!

Good luck!
No one else my age (in my area like school and stuff) doesn't do anything computers at all. I do coding outside of school (regular school + coding) so everyone I look at is late college or a professional....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OCRAD returns gibberish EVERY time - is there a good HowTo? KimVette Linux - Software 18 08-29-2005 02:24 AM
xchm 0.9.6-1 displaying gibberish? nagromo Linux - Software 0 12-19-2004 04:43 PM
Apollo P-2200, apsfilter, and a bit of gibberish goofyheadedpunk Slackware 0 10-15-2004 05:15 PM
Console gibberish linmonkey Slackware 2 07-12-2004 09:14 AM
Canon BJC 210 SP prints gibberish!! blaze_wk Linux - Hardware 1 02-25-2004 02:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration