Thread: File I/O Binary Question...

  1. #1
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178

    File I/O Binary Question...

    im here again to influence you with another problem

    im having problems im making a game mod and im havin troubles tryin to decode the file it looks as if o be in a binary format how do i extract it and output it into a normal text file.

    i learnt this on another tutorial site but i always like to come here

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    char ch;
    
    ifstream source_file ( "C:\AGP.u", ios::binary ); //open that dreadful file
    
    ofstream output_file ( "C:\opened.txt" ); /* file to put the un-encoded version */
    
    if (! source_file.is_open()) {
    cout << "File Failed to Open, Press Enter" << endl;
    cin.get();
    return -1;
    }
    else
    {
    while ( source_file.get(ch) ) {
    output_file.put(ch);
    }
    cout << "file should be outputted" << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    not working and i have no clue how to change it back into Binary please help
    Last edited by C+noob; 07-10-2005 at 01:54 AM.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    With that code you are taking 1 byte out of AGP.u, converting it into binary (0s and 1s) (I believe) and then putting that stream into opened.txt, 1 byte at a time. That would leave you with AGP.u unchanged, and opened.txt (possibly in the wrong format) as a copy of AGP.u.

    I'm assuming this isnt a mod for your own game.

    If you can create a mod for a game it usually comes with some SDK, like an application. You dont use like just open it in binary so you can run it, or add C++ code to it when you I/O with it. That mod file should be like an output of whatever the application of the game comes out with in a strange and unique format (using that games functions and such). It wont come out in code in anyway, because you cant tell where binary starts a space, a letter, or anything, because its all just 0's and 1's. Then theres hex-editing which is a long process to learn, specifically reverse engineering (like learning 3 different programming languages), and you're basicly 'cracking' the file, turning it into assembly language, which you can barely figure out how to turn into C++, and that would give you a very spagetti version of the code used in that mod.. which you could recompile if you were very good. Anyway, this all just a bunch of ramble...

    There should be an application to edit/create mods.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I'm not entirely sure that'll work, but if it did, your code isn't working because you're not reading from it like it's a binary file:
    Code:
    #include<fstream>
    
    int main()
    {
            char ch;
    
            std::fstream in("test.in",std::ios::binary|std::ios::in);
            std::fstream out("test.out",std::ios::out|std::ios::trunc);
    
            while(in.read(reinterpret_cast<char*>(&ch),sizeof(ch)))
            {
                    out<<ch;
            }
    
            in.close();
            out.close();
            return 0;
    }
    like I said, that probably wont' do what you expect - it reads a character's worth of bytes from the file, and outputs it as a character into test.out.
    Last edited by major_small; 07-10-2005 at 03:05 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    umm i dont understand all of that but one thing is how the heck fstream isnt ia type i think it has to be ofstream and ifstream i dont have access to my compiler at the minute so....

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    fstream is a combination of ifstream and ofstream: http://www.cppreference.com/cppio/all.html

    it's useful if you want to use the same stream for input and output.

    btw, just for reference: I compile most of the code I post on the forums with -Wall before it's posted - I try to not post misinformation.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    ok and why may i ask you use std::? instead of using namespace std; is there an advantage?
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah - it's like the difference between taking a few marbles out of a bag and tearing the bag open and letting them fall all over the floor.

    I suppose it really doesn't matter in the end, but without doing it my way, you could never do cool things like this:
    Code:
    #include<iostream>
    
    namespace data
    {
            int cout;
    }
    
    int main()
    {
            data::cout=5;
            std::cout<<data::cout<<std::endl;
            return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include<iostream>
    
    using namespace std;
    
    int cout;
    
    int main()
    {
        ::cout=5;
    
        // std:: required to distinguish between global cout even with "using namespace std" statement
        // std:: not needed with endl however
        std::cout << ::cout << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    FOX
    Join Date
    May 2005
    Posts
    188
    What exactly is a binary file in Windows? I know POSIX systems don't distinguish between a binary file and a text file.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    a text file is (kinda) human-readable. a binary file is meant to be read by a program.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    All files are really binary, the only real difference between the two is in the interpretation of the newline character (I think). When writing a single newline character ('\n') in text mode you actually end up writing two characters, a carriage-return/linefeed combo. Likewise when reading from a file in text mode, reading in the particular 2 character carriage-return/linefeed combo gets converted into a single newline character ('\n'). Writing/reading in binary mode disables this conversion so that what you write is exactly what you wrote (byte-for-byte) and what you read is exactly what is in the file (byte-for-byte).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it's more than that... for example, run this code and look at the two files:
    Code:
    #include<fstream>
    #include<iostream>
    #include<ctime>
    
    int main()
    {
            int i=time(0);
            std::fstream text("test.in",std::ios::out|std::ios::trunc);
            std::fstream bin("test.out",std::ios::binary|std::ios::out|std::ios::trunc);
    
            text<<i;
            bin.write(reinterpret_cast<char*>(&i),sizeof(int));
    
            text.close();
            text.clear();
            bin.close();
            bin.clear();
    
            text.open("test.in",std::ios::in);
            bin.open("test.out",std::ios::binary|std::ios::in);
    
            i=0;
            text>>i;
            std::cout<<"Text: "<<i;
            i=0;
            bin.read(reinterpret_cast<char*>(&i),sizeof(int));
            std::cout<<"\nBinary: "<<i<<std::endl;
    
            text.close();
            bin.close();
    
            return 0;
    }
    in case you don't want to try it, here are the two files:

    test.in (text)
    Code:
    1121089981
    test.out (binary)
    Code:
    ½yÒB
    Last edited by major_small; 07-11-2005 at 07:59 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. simple File I/O question
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 06:53 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. I have a file I/O question as well
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:11 PM