use of &i in file.write

is following syntax right??? if yes then why my compiler is giving error

also if this is not right then please tell me, What you think my teacher wanted to tell me by this code he said run this and in file you will see characters not numbers from 0 to 255

1
2
3
4
5
for(int i = 0; i < 256; i++){
            
            filehandle.write(&i, sizeof(i));
            
            }//-------for 
if yes then why my compiler is giving error
What error is it giving?
36 D:\Devcpp Programming\temp\temp.cpp no matching function for call to `std::basic_fstream<char, std::char_traits<char> >::write(int*, unsigned int)'

and a note

note C:\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:360 candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]

in my program I'm using ff for filehandle
You need a cast here.

filehandle.write(reinterpret_cast<char*>(&i), sizeof(i))
try the following:

#include <fstream>

1
2
3
4
5
6
7
ofstream filehandle("TEST.DAT");
    for(int i = 0; i < 256; i++){
            
            filehandle<<(char )i;
            
            }//-------for
    filehandle.close();
yes it did work but this time it's putting 3 space characters after each character. Is it due to the sizeof(i) I used

and reinterpret_cast<char*>(&i) it converts integer to characters constant pointer right???

I wish I knew how it works.
the way DareNow told works fine too and in this method there are no spaced between characters

now another problem I tried to read same file back to console this way

ff >> c;
cout << c;

but there are no character or number just blank spaces
yes it did work but this time it's putting 3 space characters after each character. Is it due to the sizeof(i) I used

and reinterpret_cast<char*>(&i) it converts integer to characters constant pointer right???


Yes. sizeof(int) and sizeof(char) are usually different. It wouldn't be space characters, but characters with ascii value 0 (which your editor may show as spaces.)

In this case, the reinterpret_cast converts from pointer to int to pointer to char which just means we're going to interpret the bits that make up whatever is pointed to (i in this case) as the type referenced by the pointer type we converted to. (In other words, we interpret the bits of i as if it was an array of char.)
@danicpp to read a file you have to declare the variable of type ifstream (input file stream)

In the example I've show to you I've used a variable of type of ofstream (output file stream)
using this

file.open("useampi.txt", ios::out | ios::in);

and

for(int i = 0; i < 256; i++){

file >> c;
cout << c;

}//----------for


worked but not well, yes there was something on console this time but it were a few weird symbols those could be ASCII representations of first few 10,12 characters but after about 10 characters it is all down-arrow symbols

then I used (int)c {by the way atoi didn't work as it needed const char, and my c was a variable char} and result was 0-25 and afterward just 25 repeatedly and nothing else till loop reached EOF(I guess)
Last edited on
Topic archived. No new replies allowed.