File in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

File in c++

This code is a in cpp Can someone explain it to me please? fstream fp("courses.dat" , ios::out|ios::in|ios::binary); if(!fp) { ofstream fp1("courses.dat" , ios::out|ios::binary); fp1.close(); } ifstream inj("j.txt" , ios::in); if(!inj) { ofstream outj("j.txt" , ios::out); int tempJ=0; outj<<tempJ; outj.close(); }

17th May 2020, 3:52 PM
Yeg
5 Answers
+ 1
Myabe this can help you: https://www.sololearn.com/learn/CPlusPlus/1920/ https://www.sololearn.com/learn/CPlusPlus/1921/ If not, tell us where you dont inderstand what is happening
17th May 2020, 4:12 PM
Alexander Thiem
Alexander Thiem - avatar
0
Alexander Thiem tnx Alexander Thiem those informations helped me a little But i still have questions First :can we write" if(inj.is_open())" instead of (!inj)? And if the answer is no can you explain me why? And the second question is why we use flag ios::in ? I really don't understand the usage of these optional parameters (flags) Sorry I'm new to cpp so I have so many questions
17th May 2020, 6:44 PM
Yeg
0
I think inj.is_open is similar to !inj, but you can probably test this out in code playground or an other code area. In this case the file is opened with ios::in , which means that you can only read it.... These flags define how the file is open: For example ios::trunc clears the file completely before opening it. Hope I could help you
18th May 2020, 6:18 AM
Alexander Thiem
Alexander Thiem - avatar
0
Alexander Thiem Tnx for your help
18th May 2020, 6:23 AM
Yeg
0
you are welcome
18th May 2020, 6:31 AM
Alexander Thiem
Alexander Thiem - avatar