Read as Binary

reading files in binary

Page 1 of 1

5 Replies - 1050 Views - Last Post: 22 May 2009 - 04:50 PM Rate Topic: -----

#1 DingleNutZ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 121
  • Joined: 02-May 09

Read as Binary

Posted 20 May 2009 - 07:16 PM

i have googled, and yahoo'd, and Ask'ed but to no avail have i found the answer to my problem.

is their anyway to read a file in binary, such as read an exe in binary, or a text file?

please help i cant think of anyway this could work.

thanx in advanced for any help.
Is This A Good Question/Topic? 0
  • +

Replies To: Read as Binary

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Read as Binary

Posted 20 May 2009 - 07:21 PM

Pass the right flag to fopen (in C) or the fstream constructor (in C++).
Was This Post Helpful? 0
  • +
  • -

#3 DingleNutZ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 121
  • Joined: 02-May 09

Re: Read as Binary

Posted 20 May 2009 - 07:33 PM

so would this
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int i;

  i = fstream filestr ("text.txt", fstream::in | fstream::out);

  cout << i << "\n";

  filestr.close();

  return 0;
}


out put the contents of text.txt in binary?

This post has been edited by DingleNutZ: 20 May 2009 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mowgef   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Read as Binary

Posted 20 May 2009 - 08:02 PM

http://www.cplusplus...tutorial/files/

This post has been edited by Mowgef: 20 May 2009 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 rs4   User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: Read as Binary

Posted 20 May 2009 - 11:21 PM

Yeah look at link above posted by Mowgef for more indepth.

But to put it simply
char in;
char out;
fstream filestr;//create stream for use called filestr
filestr.open("filepath",ios::in | ios::binary | ios::out);//Specify opening in binary mode
filestr.get(in);//Read a character and put it in 'in'
filestr.put(out);//Write the character in 'out', these are useful if using non keyboard char's
filestr.write(pointer to string, length);//faster to read/write long strings
filestr.read(pointer to string, lenght);//lenght of string you want to read/write not neccessaraly the whole string.
//<< and >> opertators can still be used.
filestr.close();//close when done



Hope this gives you a good place to start.

This post has been edited by rs4: 20 May 2009 - 11:25 PM

Was This Post Helpful? 1
  • +
  • -

#6 DingleNutZ   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 121
  • Joined: 02-May 09

Re: Read as Binary

Posted 22 May 2009 - 04:50 PM

thankyou, geez your like the second kiwi i have found here. not many of us in these forums
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1