brazc0re
brazc0re

Reputation: 353

Compiling char array with null terminators in between

I am trying to make a small C utility that reads in a MP3 tag from a file and displays it. I'm trying to make it as efficient as possible, so I'm making all of the char arrays that store the name, title, etc of the song dynamic, so they're sized at run time. I find the size of each 'frame' (a frame is: name, title, etc) through the MP3 ID3 header.

I am starting with the title of the song first. The MP3 file buffer that I loaded with the fopen operation and put into a char buffer looks like this for the title:

T|0|I|0|T|0|I|0|l|0|E, 

all in hex of course. As you can see, there are 0's in between the letters of the title, thus in C, meaning a string terminator when trying to store in a char array.

I am trying the follwing:

memcpy( bBuffFirstFrame, &bBuffTag[0x0A+10], iFrameSize );

Where bBuffTag is where all of the ID3 title information is located.

Reading the documentation, it says that memcpy should store everything from the original buffer, including null terminators. However, the char array I am creating (bBuffFirstFrame) only stores the first byte and gets terminated because of the null terminator. So instead of storing the song name "N|0|A|0|M|0|E" (0 is a null), it stores only N.

bBuffFirstFrame is being defined as:

bBuffFirstFrame = ( unsigned char* ) malloc( iFrameSize );

So my first question is, is this the best way even to approach this problem? Should I use malloc to create dynamic arrays or should I just create an array thats really big for each frame? For example:

bBuffFirstFrame[10000]? 

As you probably know, the song title changes with each MP3. Another reason I don't want to create a pre-defined size array is that it might cause a buffer overflow for a real big song name.

Final question, is there a way to store null terminators inside of a char array without terminating the string?

Sorry if this sounds confusing. Thanks!

Upvotes: 1

Views: 225

Answers (2)

Matt Lacey
Matt Lacey

Reputation: 8255

The null terminators will only be respected by the string functions, so if you know the size of the strings you're reading etc. you should be fine — are you sure you're using memcpy and not strcpy? I know it sounds silly to ask given what you've posted, but memcpy shouldn't stop at a null terminator.

I would suggest that iFrameSize is actually not holding the value you expect and that is where the problem is coming from


Upvotes: 2

ThomasMcLeod
ThomasMcLeod

Reputation: 7779

You can store data you like in a C array. The problem is that many C runtime function that operate iteratively on strings recognize the first '\0' in the array as the termination character.

Upvotes: 1

Related Questions