TFileStream Help

Hey every one...
Hey Mr bilderbikkel...

1- How can I open a file in binary mode using TFileStream ?
2- How to read the file quickly ?

I browsed the help, but I found no file manager for binary mode.

I need to read a file in binary mode then display the contents of it in a memo in terms of 0s and 1s ..... some thing like HexEditors but in binary mode.
i'm afraid if i use text mode, some translation will occure on the characters in the file.
i did this program in console and it works fine, but still one problem it is desplaying just a 300 KB file may take a minute to complete translation into 0s,1s and displaying them on the screen...
On the other hand HexEditors can open a 600MB in just a second ...How ?
I can post the code if you wish to see it.
it is just 20 line.
Mohammad Nasim

Comments

  • : Hey Mr bilderbikkel...
    Hey Luckyboy!

    : I can post the code if you wish to see it.
    : it is just 20 line.
    I'd love to see the code. Could you post it? Perhaps there are some nifty hi-tech technique I know of to improve the speed.

    See ya,

    bilderbikkel

  • [b][red]This message was edited by luckyboy at 2005-12-5 11:54:50[/red][/b][hr]
    [b][red]This message was edited by luckyboy at 2005-12-5 11:53:42[/red][/b][hr]
    [b][red]This message was edited by luckyboy at 2005-12-5 11:49:17[/red][/b][hr]
    : I'd love to see the code. Could you post it? Perhaps there are some nifty hi-tech technique I know of to improve the speed.
    :
    //binary.cpp
    #include
    #include
    #include "utilities.h"
    using namespace std;
    int main(int argc, char *argv[])
    {
    char ch;
    ftream file;
    file.open(argv[1], ios::in|ios::binary);
    if(!file)
    {
    cout<<"Error openning the file...";
    return 1;
    }
    while(file) // it will be NULL when eof reached
    {
    file.get(ch);
    DisplaBits(ch);
    }
    return 0;
    }
    //______________________________________

    //utilities.cpp
    #include <iostream>
    #include "utilities.h"
    using namespace std;
    void DisplayBits(char ch)
    {
    const int shift = sizeof(ch); // for portability reasons
    char mask = 1; // it is now 0000 0001
    mask <<= shift-1; // it will be 1000 0000
    for(const int i=1; i<=shift; i++)
    {
    cout<<(mask & ch)? '1' : '0';
    ch <<= 1;
    }
    cout<<" "; // two spaces to fit the screen which support 80
    // characters per line.
    }
    //____________________________________

    //utilities.h
    void DisplayBits(char);
    //________________________________________

    20 Lines without #includes and others.... :).
    you don't answer the first question
    Does TFileStream support binary mode ?!






  • Hi M-Nasim,

    1) Use code tags: start code with [leftbr]code], end with [leftbr]/code]!
    2) Your code does not compile:

    [code]
    : //binary.cpp
    : #include
    : #include
    : #include "utilities.h"
    : using namespace std;
    : int main(int argc, char *argv[])
    : {
    : char ch;
    [red]: ftream file;
    //Undefined object ftream, should be fstream[/red]
    : file.open(argv[1], ios::in|ios::binary);
    : if(!file)
    : {
    : cout<<"Error openning the file...";
    : return 1;
    : }
    : while(file) // it will be NULL when eof reached
    : {
    : file.get(ch);
    [red]: DisplaBits(ch);
    //ERROR: call to undefined function 'DisplaBits', should be DisplayBits[/red]
    : }
    : return 0;
    : }
    : //______________________________________
    :
    : //utilities.cpp
    : #include <iostream>
    : #include "utilities.h"
    : using namespace std;
    : void DisplayBits(char ch)
    : {
    : const int shift = sizeof(ch); // for portability reasons
    : char mask = 1; // it is now 0000 0001
    : mask <<= shift-1; // it will be 1000 0000
    [red]: for(const int i=1; i<=shift; i++)
    //ERROR: Cannot modify a const object[/red]
    : {
    : cout<<(mask & ch)? '1' : '0';
    : ch <<= 1;
    : }
    : cout<<" "; // two spaces to fit the screen which support 80
    : // characters per line.
    : }
    : //____________________________________
    :
    : //utilities.h
    : void DisplayBits(char);
    : //________________________________________
    [/code]

    Fix it,
    bilderbikkel

  • [b][red]This message was edited by luckyboy at 2005-12-5 17:55:45[/red][/b][hr]
    : Fix it,
    :
    it just because i was writing quickly, but i compiled it and it worked nicely regardless that it was some slow.

    please if you have a way to make it fast post it.
    also tell me how to open a file in binary mode using TFileStream.

    I need to make this prpogram 'visual' like Hex-Editors

    By the way I found in your profile that you are game developer, so check this link
    http://www.flipcode.com/articles/article_vfs01.shtml
    I don't know any thing in game development field, but i found it may help you.
    see ya
    M-Nasim
  • : it just because i was writing quickly, but i compiled it and it worked nicely regardless that it was some slow.
    I AM NOT GOING TO CORRECT YOUR CODE WHEN I TRY TO IMPROVE IT ON MY OWN COMPUTER.

    : please if you have a way to make it fast post it.
    Yes. I won't.

    : also tell me how to open a file in binary mode using TFileStream.
    Don't know, didn't try

    : I need to make this prpogram 'visual' like Hex-Editors
    Easy

    : I don't know any thing in game development field, but i found it may help you.
    It didn't
  • [b][red]This message was edited by luckyboy at 2005-12-6 10:53:51[/red][/b][hr]
    [code]
    //main.cpp
    #include
    #include
    #include "utilities.h"
    using namespace std;

    int main(int argc, char* argv[])
    {
    ifstream *file;
    char ch;
    if(argc != 2)
    {
    cout<<"Usage: binary [path]filename";
    return 1;
    }
    file->open(argv[1], ios::in|ios::binary);
    if(! file )
    {
    cout<<"Can't open file ...";
    return 1;
    }
    while(file)
    {
    file->get(ch);
    DisplayBits(ch);
    }
    delete file;
    return 0;
    }
    //__________________________________________

    //utilities.cpp
    #include
    #include "utilities.h"
    using namespace std;

    /* Convert to Binary form */
    void DisplayBits(char ch)
    {
    char shift = 8 * sizeof ch;
    char mask = 1;
    mask <<= (shift - 1); // to remain last bit

    // to prevent <= we use shift+1
    for(int i=1; i<shift+1; i++)
    {
    cout << ((ch & mask)? '1':'0');
    ch <<= 1;
    if(i%8 == 0)
    cout<<" ";
    }
    }
    //________________________________________

    //utilities.h
    void DisplayBits(char ch);
    //___________________________
    [/code]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories