10

I am unclear about how reading long integers work. If I say

long int a[1]={666666}
ofstream o("ex",ios::binary);
o.write((char*)a,sizeof(a));

to store values to a file and want to read them back as it is

long int stor[1];
ifstream i("ex",ios::binary);
i.read((char*)stor,sizeof(stor));

how will I be able to display the same number as stored using the information stored in multiple bytes of character array?

3
  • 1
    Can you be more specific? Commented Feb 22, 2013 at 6:18
  • I would like to learn how read an integer value[4 bytes] stored in a binary file. Commented Feb 22, 2013 at 6:20
  • 5
    Isn't your code doing that already? Commented Feb 22, 2013 at 6:22

2 Answers 2

12

o.write does not write character, it writes bytes (if flagged with ios::binary). The char-pointer is used because a char has length 1 Byte.

o.write((char*)a,sizeof(a)); 

(char*) a is the adress of what o.write should write. Then it writes sizeof(a) bytes to a file. There are no characters stored, just bytes.

If you open the file in a Hex-Editor you would see something like this if a is int i = 10: 0A 00 00 00 (4 Byte, on x64).

Reading is analogue.

Here is a working example:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main (int argc, char* argv[]){
    const char* FILENAM = "a.txt";
    int toStore = 10;
    ofstream o(FILENAM,ios::binary);

    o.write((char*)&toStore,sizeof(toStore));
    o.close();

    int toRestore=0;
    ifstream i(FILENAM,ios::binary);
    i.read((char*)&toRestore,sizeof(toRestore));

    cout << toRestore << endl;


    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, I'm having problem with this. What happens if you don't flag the file object with binary?
I think this breaks when reading a such a file on a big endian machine when it was generated on a little endian system or vice-versa.
-4

Sorry I took so long to see your question.

I think the difference between binary is the binary will read and write the file as is. But the non-binary (i.e. text) mode will fix up the end-of-line '\n' with carriage-return '\r'. The fix-up will change back and forth between '\n' and '\r', or "\n\r" or "\r\n" or leave it as '\n'. What it does depends on whether the target operating system is Mac, Windows, Unix, etc.

I think if you are reading and writing an integer, it will read and write your integer fine and it will look correct. But if some byte(s) of the integer look like '\r' and '\n', then the integer will not read back correctly from the file.

Binary assures that reading back an int will always be correct. But you want text mode to format a file to be read in a text editor such as Windows's Notepad.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.