fstream help

hey guys,

I am developing a program that searches through a .mp3 file and extracts the IDE information and some other stuff after that but that is not my problem, I have managed to search and locate the information and whilst inputting the information into a struct the program is in fact reading the 3 bytes but when I am outputting it is showing 12. had my brother look at it too and he is lost.. any help would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
using namespace std;

struct idestruct{
	char header[3];
	char title[30];
	char artist[30];
	char album[30];
	char year[4];
	char comment[30];
	char genre;
};
	
int main()
{

	char file[128];
	idestruct ide;
	int ide_size = sizeof(ide);

	
	

	cout << "Enter your MP3 filename: ";
	cin >> file;

	ifstream ins;

	ins.open(file, ios::binary | ios::in);
	ins.seekg(-(ide_size), ios::end);
	
	cout << ins.tellg() << endl;

	ins.read((char *) &(ide), (sizeof(ide.header)));
	cout << ins.gcount() << endl;

	cout << ide.header << endl; //outputting more than 3 read bytes?

	ins.close();

	return 0;
}
Don't forget the terminator. Your header string should hold room for 4 bytes: 3 for information and the last for the null terminator ('\0').

Edit:
http://www.cplusplus.com/reference/istream/istream/read/?kw=istream%3A%3Aread
This function simply copies a block of data, without checking its contents nor appending a null character at the end.
Last edited on
its in binary and it is written so that there is no terminator between them the information. :(
I mean you have to manually put in a terminator in the string. When you pass a char* to most C-string operations, they will only stop iterating until they find '\0'. By convention, '\0' marks the end of a C-string.
Last edited on
ah ok i see what you mean, what is the best way of doing that?
1
2
3
4
5
6
7
8
9
10
struct idestruct{
    char header[4];
//...
};

//...

ins.read(ide.header, 3);
ide.header[3] = '\0';
ah ok.. tried that but now it is not outputting anything?? :\
Did you try iterating through the string yourself to see if you did read anything?
1
2
for(unsigned I(0); I < 3; ++I)
    cout << static_cast<int>(ins.header[I]) << ' ';
it is reading and I can display it through iteration.. however when I use cout << ide.header << endl; it is displaying a blank.. could it be a problem in the cout function?
You might want to reference this table: http://www.asciitable.com/

Maybe the data you are reading happen to be whitespace characters.
Topic archived. No new replies allowed.