Hi, im trying to write some information into a mp3 file. The information i'm trying to introduce into the file must begin 128 bytes from the end of the file, and this block of information is identified by the string "TAG" at that position (128 bytes from the end).

After those 3 bytes, we have a 30 byte space for each one of these strings: title,artist name, album, a 4 byte slot for the year, and comment.

The sum of all these slots is 127, so that the information to be written can fit the file.

Thing is, it isnt writing anything into the file...not even those crucial 3 bytes. Here's the code:

void TagCmd::create_tag(Mp3& mp3){

	const char * name = mp3.Getname();
	fstream file(name, ios::out | ios::in | ios::binary);

	char * tag = "TAG";
	char * tit = "<Song Title>";
	char * art = "<Artist Name>";
	char * alb = "<Album Name>";
	char * jahre = 0;
	char * comment = "<Comment>";
	char * test = new char[4];
	//char c;
	

	file.seekp(-128,ios::end);

	file.write("TAG",3);

	file.write(tit,30);
	file.write(art,30);
	file.write(alb,30);
	file.write(jahre,4);
	file.write(comment,30);

	file.seekg(-128,ios::end);
	file.read(test,4);

	printf("coiz: %s\n",test);

	file.close();

}

It would be great if u guys could give me a hint here. Thanks again :)

> file.write(tit,30);
If you're going to write 30 bytes, then you need char tit[30] = "Title"; otherwise you're reading uninitialised memory and storing up who knows what kind of trouble.

> file.read(test,4);
This won't have a \0 at the end (unless the file happens to have a \0, which seems unlikely).
Read 3 bytes, and then set test[3] = '\0'; Also, between WRITING and READING, you need to flush()

You're right, thanks. I'm now facing a new problem. If we want to edit the information of those 128 bytes, you'll have to write in that zone of memory.

Now when i do that, it erases the whole file. I'm left with my mp3 file with nothing in it :S

Here's what i did:

void Mp3::editTag(const char * field, const char * value){

			const char * nome = this->Getname();
			fstream file(nome, ios::out | ios::binary);

			char * newValue;

			if(strcmp(field,"title") == 0){

				newValue = new char[30];
				strcpy(newValue,value);

				file.seekp(-125,ios::end);
				file.write(newValue,30);
				file.flush();
				file.close();
				printf("Editing of field %s successful.\n",field);

			}
			else
				if(strcmp(field,"artist") == 0){

				newValue = new char[30];
				strcpy(newValue,value);

				file.seekp(-95,ios::end);
				file.write(newValue,30);
				file.flush();
				file.close();
				printf("Editing of field %s successful.\n",field);

			}
			
			else
					if(strcmp(field,"album") == 0){

				newValue = new char[30];
				strcpy(newValue,value);

				file.seekp(-65,ios::end);
				file.write(newValue,30);
				file.flush();
				file.close();
				printf("Editing of field %s successful.\n",field);

			}
			
			else
				if(strcmp(field,"year") == 0){

				newValue = new char[4];
				strcpy(newValue,value);

				file.seekp(-35,ios::end);
				file.write(newValue,4);
				file.flush();
				file.close();
				printf("Editing of field %s successful.\n",field);

			}
			
			else
				if(strcmp(field,"comment") == 0){

				newValue = new char[30];
				strcpy(newValue,value);

				file.seekp(-31,ios::end);
				file.write(newValue,30);
				file.flush();
				file.close();
				printf("Editing of field %s successful.\n",field);

			}
			
			else{
				printf("ERROR: Wrong field specified.\n");
				return;

			}

		}

What's wrong in this? Is it the same mistake i did in the other method? I'm declaring the variable newValue inside the if clause because the size of the infomation to write varies between 30 bytes and 4 bytes. Maybe this isnt the right approach...

Theres actually nothing wrong with that...When i was testing it i was trying to write a multiple-worded string into the artist slot...but since im getting the string to write and the field from the command line, i'm just getting one word at the time, and when i try to write more than one work, it goes crazy.

I'm trying now to get every word (within the limit of 30 bytes of course) that the user specifies to write on the selected slot of those 128 bytes.

class EdtVisitor : public FrameVisitor {

	char * field;
	char * value;

EdtVisitor(int argc, const char * const * args){ 

		field = new char[strlen(args[0])];
			strcpy(field,args[0]);

		int i = 1;
	if(argc>=2){ while(i<argc) strncat(value,args[i],sizeof(args[i++])); }


		/*field = new char[strlen(f)+1];
		strcpy(field,f);

		value = new char[strlen(val)+1];
		strcpy(value,val);
		*/
	}

I'm appending those strings in the instance variable value, and i'm doing that in the class constructor. Is this the right way to do it? It would be really lame if we only could write one word when we want to edit the information of the file :S

I've cracked it...From these posts it seems like i'm brainstorming with myself lol...Maybe i should stop opening threads.

Thanks for your help anyway Salem :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.