difference between write and using insertion operator

If you want to write to a file you do this
1
2
3
//method 1
  ofstream offile("DATA.txt");
offile<<"This is the first method\n";

1
2
3
4
5
//method 2
ofstream offile("DATA.txt");
char s[]="This is the first method\n";
offile.write(reinterpret_cast<char *>(s),25*sizeof(int));
offile.close();

What are the basic differences between the two methods and why can't we use string in method 2 there is still some 5 char junk value stored in the end of file in method 2 which i don't know how to remove though
have you tried it with 25*sizeof(char)?

that could work
Last edited on
No that doesn't help either
Instead of making a new thread i am going to write another question over here it does write but can't read what's wrong with this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	ofstream os("edata.txt",ios::binary);
	const int max=100;
   int buff[max];
   for(int i=0 ; i<max; ++i)
   {
	   buff[i]=i;
   }
	os.write(reinterpret_cast<char *>(buff),max*sizeof(int));
	os.close();
	for(int i=0 ; i<max; ++i)
	{
		buff[i]=0;
	}
	ifstream is("edata.txt",ios::binary);
	is.read(reinterpret_cast<char *>(buff),max*sizeof(int));
	/*is.close();*/

}
here it does write but can't read

That code seems to work when I tried it. Of course there is no output on the screen in order to verify the result. How did you determine that it was not reading?
The difference is this:

Insertion/extraction operators perform text conversion on the output/input streams.

write()/read() do not: they simply copy data byte by byte.

Hopes this helps.
Duaoas are there any limitations for using strings for files
How did you determine that it was not reading?
it doesn't show the expected data in edata.txt file
it doesn't show the expected data in edata.txt file

It's possible that the data was perfectly ok, but it just wasn't what you expected.

Note that in your code above, "edata.txt" is not a text file, because the file is opened in binary mode and the os.write() will output binary data to the file. Thus it cannot be read in an ordinary text editor.

What can i do to have the file readable for me are there any function for it and it should be .txt file or there isn't cause what if i need that data I don't understand honestly why would they do such a thing becuase later reading that data for a normal person is important and is it in binary form computer stores it in binary form
Did you try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>

    using namespace std;

int main()
{
    ofstream os("edata.txt",ios::binary);
    const int max=100;
    int buff[max];
    for (int i=0 ; i<max; ++i)
    {
        buff[i] = i;
    }
    os.write(reinterpret_cast<char *>(buff),max*sizeof(int));
    os.close();
    
    for(int i=0 ; i<max; ++i)
    {
        buff[i] = 0;
    }
    
    ifstream is("edata.txt",ios::binary);
    is.read(reinterpret_cast<char *>(buff),max*sizeof(int));
    
    for (int i=0 ; i<max; ++i)
    {
        cout << buff[i] << '\t';
    }    

}

Above, lines 26 to 29 simply output the contents of the buffer after reading from the file. It should display the numbers from 0 to 99 - as expected.

If you want to view the contents of a binary file, then use a hex editor such as hxd or hexplorer.
http://mh-nexus.de/en/hxd/
http://sourceforge.net/projects/hexplorer/

Those tools are perfectly fine for a programmer to work with binary files of any type. However, if you want the file contents to be readable by an ordinary person who has no need to delve into the inner workings of the data, then a binary file is the wrong choice. Either don't use binary mode at all in that case, or use binary for the program's own purposes, and create a separate report file to give to the ordinary person.

One small point, it's probably a good idea to give the file a different name, such as "edata.bin" of "edata.dat" for the binary file, and reserve the .txt extension for true text files.
Last edited on
Topic archived. No new replies allowed.