CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2009
    Posts
    17

    [RESOLVED] Reading binary file

    Hey everybody,

    Firstly, thanks for reading this thread. I'm c++ n00b so don't be too harsh on me . Anyway I'm struggling to solve my first cpp project. Before I worked for a few month with .NET. So I reckon I have some basics, hopefully . Now to my problem.

    So, I have a file from gps receiver. I can't specify exactly what type it is, but I suppose it's binary file. Definitely it's not a text and there many unreadable characters. Some of them are actually readable e.g. pair of identifiers for certain messages (µb=0xB5 0x62). After these identifiers there is a body with the message that has to be converted to whatever(int,long,double....).
    I want to read sequentially the whole file from the start to the end (say it's around 150MB). Find these pair identifiers. Read the body of each identifier (with defined length ) to unique struct. I don't worry about the output for now. I would be glad if you can help me with the main loop. I have this for now:
    Code:
    void FileManager::openFile(){
    	//variables are located in FileManager.h
    	ifstream fp;
    	unsigned long len;
    	char *buf;
    	streambuf * pbuf;
    
    	char bufferA;
    	char bufferB;
    	fp.open("raw.ubx", ifstream::binary | ios::in);
    	fp.seekg(0, ios::end);
    	len = fp.tellg();
    	fp.seekg(0, ios::beg);
    	pbuf = fp.rdbuf();
    	while (pbuf->sgetc()!=EOF){
    		bufferB = pbuf->sbumpc();
    		if ((bufferB == 'µ') && (bufferA == 'b')){
    			/*function call of this identifier*/
    		}
    		bufferA = bufferB;
    	}
    }
    In my file on the first position there is µ character. But I get "-75" instead. All advices and tips from you are more than appreciated.
    Also I would be glad if you know where to get offline ISO c++ documentation.

    Thank you in advance

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Reading binary file

    1. You are checking the content of bufferA before you ever assign anything to it.

    2. For non-Ascii characters, use the numeric value that the character has according to the encoding your file is in.

    3. The logic in your code will find bµ and not µb

    4. Why do you access the streambuf. Why not just
    Code:
    char bufferB;
    while (fp>>bufferB) {
      // ...
    }
    Last edited by treuss; July 1st, 2009 at 11:02 AM.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jul 2009
    Posts
    17

    Re: Reading binary file

    Thank you for quick response, mate .
    1) I reckon I should assign at least 0 at first, right ? Will do.
    2)should I use atoi to get the numeric value? I don't think it's possible to do something like this int (bufferB). Can you give me an example, pls ?
    3)Oooo yeaahh, you're right. silly me
    4)Not sure, I suppose I found some examples and tried to use them in my way
    Last edited by redback; July 1st, 2009 at 11:53 AM.

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Reading binary file

    Quote Originally Posted by redback View Post
    2)should I use atoi to get the numeric value?
    No, just compare char to number:
    Code:
    if (bufferA == 0xB5)
    If that gives a warning, cast bufferA to unsigned char.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  5. #5
    Join Date
    Jul 2009
    Posts
    17

    Re: Reading binary file

    it works, great. Thanks a lot. I need to study much more

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured