need Help! reading a binary file

This is a discussion on need Help! reading a binary file within the C++ Programming forums, part of the General Programming Boards category; unsigned char namelength;...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    unsigned char namelength;

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    again i like to thank everyone for there help with this.


    I'm wondering how do i get the HEXVALUE to display in Hexadecimal format.
    Code:
    
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	 char  signature[5];			//is logi
    	 unsigned int   version;		//greater than 5
    	 unsigned int   entries;		//number of entries
    
    	ifstream ifs("testfile.dat", ios::binary);		//OPEN FILE
    		
    	ifs.read((char *)&signature,4);					//Get Signature
    	signature[4] = '\0';
    	
    	ifs.read((char *)&version, 4);					//Get Version
    	
    	ifs.read((char *)&entries, 4);					//Get Num Entries
    
    	unsigned char  lenth;							//Get Namelenth
    	unsigned int nlenth;
    	ifs.read((char *)&lenth,1);
    	nlenth = (int)lenth;
    
    	unsigned char *name;							//Get NAME
    	name = new unsigned char [nlenth+1];
    	ifs.read((char *)&*name,nlenth);
    	name[nlenth]='\0';
    	
    
    	unsigned char hexinput[6];						//Get HEXVALUE
    	ifs.read((char *)&hexinput,6);
    
    	//output data to console.
    	cout << "File information\n";		
    	cout << "Signature: " << signature << endl;
    	cout << "FileVersion: " << version << endl;
    	cout << "Entries count: " << entries << endl;
    	cout << "Lenth of name: "  << nlenth << endl; 
    	cout << "The Name:  " << name <<endl;
    	cout << "The hexvalue is: " << hexinput << endl;  //How do i make this look like the Hexadecimal value ;
    	return 0;
    }
    Attached Images Attached Images  

  3. #18
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    1) You have to operate on a character at a time
    2) The *prinft and *scanf functions have a formatter of "&#37;x" that will aid you in your mission.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    alright i got it to print in hex but how do i make it set this value to a string.
    also how do i get the 64bit interger of FILETIME and show the date and time it is representing.


    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    /*
    class Header				
    {
    public:
    	 char  signature[5];			//is logi
    	 unsigned int   version;		//greater than 5
    	 unsigned int   entries;		//number of entries
    };*/
    int main()
    {
    	 char  signature[5];			//is logi
    	 unsigned int   version;		//greater than 5
    	 unsigned int   entries;		//number of entries
    
    	ifstream ifs("testfile.dat", ios::binary);		//OPEN FILE
    		
    	ifs.read((char *)&signature,4);					//Get Signature
    	signature[4] = '\0';
    	
    	ifs.read((char *)&version, 4);					//Get Version
    	
    	ifs.read((char *)&entries, 4);					//Get Num Entries
    
    	unsigned char  lenth;							//Get Namelenth
    	unsigned int nlenth;
    	ifs.read((char *)&lenth,1);
    	nlenth = (int)lenth;
    
    	unsigned char *name;							//Get NAME
    	name = new unsigned char [nlenth+1];
    	ifs.read((char *)&*name,nlenth);
    	name[nlenth]='\0';
    	
    	
    													//OUTPUT data to CONSOLE.
    	cout << "File information\n";		
    	cout << "Signature: " << signature << endl;
    	cout << "FileVersion: " << version << endl;
    	cout << "Entries count: " << entries << endl;
    	cout << "Lenth of name: "  << nlenth << endl; 
    	cout << "The Name:  " << name <<endl;
    	cout <<  "The hexvalue is: " ;
    	
    
    	unsigned char hexinput;							//Get HEXVALUE
    	unsigned int hexoutput;
    	for (int i=1;i<6;i++)
    	{
    	ifs.read((char *)&hexinput,1);
    	hexoutput = (int)hexinput;
    	cout.setf(ios_base::hex, ios_base::basefield);
    	if (hexoutput < 17) { cout << 0;}
    	cout << std::hex << hexoutput ;					//How do i make this look like the Hexdicimal value in a string ;
    	}
    	cout << endl;
    
    /*	unsigned long long filetime;   // DOESNT WORK;
    	ifs.read((char *)&filetime,8);
    	cout << "The date:  " << filetime;
    */
    
    	return 0;
    }
    Attached Images Attached Images  

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Instead of sending it to cout, you can send it to a StringStream. A very handy class.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    10
    Quote Originally Posted by Dino View Post
    Instead of sending it to cout, you can send it to a StringStream. A very handy class.
    hi dino thank you for the replay Stringstream works very well.

    now the only part I'm having problems with right now is the. FILETIME i need to know what is the right way to get this 64bit int out of the file and how i can convert this to unixtime so PHP can read it.








    -----------------------------------------------------------------------------------------------------------
    just something off subject here. last night i made the program. read all the logi entries i had in one directory right around 30,000,000 of them so I left the computer on to see how long it would take. and when i woke up in the morning i had an out of memory error. i thought well what can this program be doing that takes so much memory it shouldn't take more than about 2 megabytes. so i look at the code and didn't see any problems. i look at it again an there it was the for loop

    Code:
    for(int f=1;f<files;f++){
    for (int i=1;i<=entries;i++){
    }

    declaring the variable in the loop was taking all my memory

    so i added it to the head of the code and now the program takes only about 839k of memory.i know this might be old for some of you but who know maybe someone is here reading this right now that doesn't know where to look for a memory problem.

    Code:
    int i;
    int f;
    for(f=1;f<files;f++){
    for(i=1;i<entries;i++){}}

    anyways i need to get back to figuring out how to get the FILETIME from the file and read it. thank you all for your help on this program.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As to the FILETIME thing, do you know how the timestamp is output? (We've gone around and around and around with people who have a "perfectly standard timestamp" that no one knows how to interpret.) More importantly, that might give you some idea of how to read it in.

    Anyway, g++ might take long long, or int64_t if you include inttypes.h. (I don't believe C++ has a standard way of getting at "types of a specific size, provided they exist" like C does.)

  8. #23
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    FILETIME is the number of 100-nanosecond since January 1, of 1601.... or something like that... I would not recommend using FILETIME if you are wanting to make your program work on OSes that are not Windows.

    For the sake of tabstop's personal gratification (though I know him well enough to know he probably doesn't care terribly much). This example I wrote sort of unravels the mystery for you.

    (Oh and for anyone who gives a damn: 11644473600 is the number of milliseconds between Jan 1, 1601 and Jan 1, 1970. And that other magic number is the coversion rate between 100-nanosecond or whatever to milliseconds)
    Last edited by master5001; 10-17-2008 at 03:23 PM.

  9. #24
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    nano you ninny. (I think)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by the beginning of the thread
    8 FILETIME FILETIME 100-nanosecond increments since January 1, 1601 (UTC). 64-bit integer
    Yeah, ok. So you do know what that is, never mind. So it just comes down to getting that 64-bit variable (or, if push comes to shove, int filetime[2] and pretend its a 64-bit variable with some extra special math).

  11. #26
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Dino View Post
    nano you ninny. (I think)
    We will have to call it a push... its 100-nanosecond intervals... So technically we are both off by two powers of 10. Well played old bean. We shall meet again.

  12. #27
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by tabstop View Post
    Yeah, ok. So you do know what that is, never mind. So it just comes down to getting that 64-bit variable (or, if push comes to shove, int filetime[2] and pretend its a 64-bit variable with some extra special math).
    One need not assume I read the beginnings of threads. Because I have never conveyed any gestures that could ever mislead someone to that conclusion.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    @master: sorry I meant the OP. But you, too, can use an array of two ints to solve the problem of finding a 64-bit variable!

  14. #29
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No biggy. I used the M$ structures to serve the same function as an array of two ints. For no other reason than to guarantee correct alignment.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  3. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 06:51 PM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 01:00 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 09:52 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21