file IO problem

This is a discussion on file IO problem within the C++ Programming forums, part of the General Programming Boards category; hello in my program, i want to be able to read, and write the binary values. i want to be ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    file IO problem

    hello
    in my program, i want to be able to read, and write the binary values. i want to be able to read: (as text) "hi", and getr (in hex) "68 69",
    then i want to be able to write (as hex) "FF FF" and stuff.
    in short i want to need to use a hex editor to view my values,

    here is what i got so far
    Code:
    unsigned encrypt2bytes(unsigned key){
    	
    	 ifstream qaz("data.txt",ios::in | ios::binary ); // this file is just opened to 
    	 if (! qaz){ cout << "error, check file existance";} // to let us read it
    	 unsigned tev; qaz>> tev;
    	 
    	 ofstream qaze("endata.txt", ios::binary | ios::out); //this is the one that actually is going to write to the new file
    	 if (! qaze){ cout << "error, check file existance";}
    	 qaze << encrypt(key, tev);
    
    qaz.close();
    qaze.close();
        return 0;
    }
    
     unsigned decrypt2bytes(unsigned key){
    	 ifstream qaz("endata.txt", ios::in | ios::binary); // this file is just opened to 
    	 if (! qaz){ cout << "error, check file existance";} // to let us read it
    	 unsigned tev; qaz  >> tev;
    	 
    	 ofstream qaze("data.txt", ios::binary | ios::out); //this is the one that actually is going to write to the new file
    	 if (! qaze){ cout << "error, check file existance";}
    	 qaze  << decrypt(key, tev);
    
    qaz.close();
    qaze.close();
        return 0;
    }
    thanks

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The data in the file is binary in any case, as is everything in the computer. How you actually see it is only a matter of the program you use to view it. If you open an exe file in notepad you'll see some strings embedded in it. If you open a text file in a hex editor you see the hex values. If you open a specially treated gif file in an image viewer you see a dinosaur. If you open it with winzip you can extract the DeCSS source code from it.

    It's all a matter of interpretation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    To read a binary file you could get some hex dump utility (download off the internet). Or you can simply open the file in VC++ 6.0, won't work for VC++ .NET (or probably you have to go though some steps that I don't know of)
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It works. You have to add it as a custom resource to some dummy project though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Problem reading file
    By coder_009 in forum C Programming
    Replies: 10
    Last Post: 01-15-2008, 01:22 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Totally puzzling IO problem
    By Vorok in forum C++ Programming
    Replies: 5
    Last Post: 01-06-2003, 07:10 PM

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