I want to know the differences in between two?
also is this code equivalent
char ch;
FILE *fin;
ch = fgetc (fin);
char ch;
ifstream fin;
fin>>ch;
The results are the same -- a character is read -- but comparing the two is a little hard since your comparing two different programing paradigms...I guess if you strip away the syntax and look at the low level results of the code...then yes the code is equivalent since both accomplish the same thing.
>I want to know the differences in between two?
That's a tough comparison. Can you be a little more specific?
>also is this code equivalent
No, the C code has two bugs (an uninitialized FILE pointer, and using char instead of int for the return value of fgetc). The C++ code is correct due to ifstream's constructor, but it won't do anything because the stream isn't attached to a file.
Ok . Consider that the files are attached.
Actually i wanted to ask whether this correct
void encode_file (ifstream fin, ofstream fout)
{
fxn body
}
and then i can pass fin and fout with which i associate some files in main().
I know that this is correct:
void encode_file (FILE *infile, FILE *outfile)
Also
fgetc and fputc should be equivalent to fin>>ch and fout<<ch if i consider ch to be char type.
ie they read and write a single byte at a time only (if ch is char type)
Please clarify.
I don't think it'll work because ifstream won't work with File pointer i.e ifstream parameter won't accept a File Pointer.
>Consider that the files are attached.
I'd still ask you to be specific, and the char bug for fgetc is still present.
>Actually i wanted to ask whether this correct
That's quite a bit different than "tell me all of the differences between FILE* and ifstream". :icon_rolleyes: And no, it's not correct. For reasons that I won't go into, stream objects aren't copyable. You need to pass by reference:
void encode_file (ifstream& fin, ofstream& fout)
Then it should work.
>ie they read and write a single byte at a time only (if ch is char type)
That's correct, but fgetc can also return EOF. It's for this reason that fgetc returns int rather than char.
Hey Narue, if we just change the paramenter passing to "pass by reference", the function would accept a File pointer?
thanks a lot. For detecting eof i can go like this
fin>>chl
while(!fin)
{
.........
fin>>ch
}
or i think there is alo a function called eof().
>Hey Narue, if we just change the paramenter passing to "pass by reference", the function would accept a File pointer?
Of course not. You simply didn't understand the question.
The difference? Aren't files and filestreams different objects/types?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.