+ 6

How you can use binary file ? How you can read and write in file ?

https://code.sololearn.com/cs5314LVlqEF/?ref=app

10th Jun 2017, 9:22 PM
Mohamed Abdalkader
Mohamed Abdalkader - avatar
8 odpowiedzi
+ 9
In C++, file handling is done using fstream, the standard file stream class... Now, to open a binary file, we use fstream objects but specify binary mode during opening... This is given in the second argument of the open method function, used to specify the conditions of the file we want during opening... fstream file; //Instead of a file pointer, we have this in C++ file.open("binary.bin", ios::binary | ios::out); //ios::binary means that we open file in binary mode, and ios::out means that we will be writing to it... // We use | or bitwise OR to seperate the openmodes, as they are read by computer as numbers and thus, ORing then results in combination of the numbers... //Now we create a character array and write it to the file using the write function... char* data=new char[13]; data="Hello World!"; if(file.is_open()) { file.seekp(0,ios::beg); //Optional - Just to move the cursor to the start of the file... file.write(data,13); file.close(); } Now, reading a binary file is also done in a similar way, using the read method... fstream file2; streampos size; //To get the number of characters in the file... char* buffer; file2.open("binary.bin",ios::binary | ios::in); if(file2.is_open()) { file.seekg(0,ios::end); //Move to end to get number of characters present... size=file.tellg(); //Assigning it to the file... file.seekg(0,ios::beg); //Now compulsory, as the cursor was at the last written character... buffer=new char[size]; //Assign the size of the buffer as the size we got fron the tellg(); file2.read(buffer,size); //Read size characters... file2.close(); } This is how one reads and writes to a binary file in C++... Hope this helps...
12th Jun 2017, 3:25 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 8
So you wish to have a C++ equivalent of the code above?
12th Jun 2017, 3:02 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 8
And instead of (ios::binary | ios::in), you may use rb, provided you do this after the headers... #define rb ios::binary|ios::in #define wb ios::binary|ios::out Now, use them as : file.open("binary.bin",wb); file2.open("binary.bin",rb);
12th Jun 2017, 3:29 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
Well, though many variants of printf like fprintf, cprintf etc exist in C to write data in different places, in C++, the operator << is valid for anything... Thus while you have to do this in C: --C Code-- FILE* file; //creating a file object. file = fopen("a file.txt","w"); //assigning file address to pointer of file... fprintf(file, " 33) Laptop - 33333.32 " ); //writing to the file, just like printf, but with an extra argument called file pointer... fclose(file); //closing the file, as we are done here. --end-- The C++ way would be: --C++ Code-- fstream file; //Creating a file object for out file maintainance. file.open("a.txt",ios::out); //Opened the file, in write only mode... file << 33 << ") Laptop - " << 33333.32 << endl; //Writing to the file. //Note how I used the same operators and in the same way as we used to with cout... // Just had to replace cout with file, thats it. // Same is for cin. file.close(); //Closed the file, as we are done here. --end-- Thus, all functions and operators that were valid for use with cout and cin are usable with any fstream object, just that an object opened with ios::out mode cannot return us data... Eg : fstream file; char data[20]; file.open("a.txt",ios::out); //Now can be used only for writing... file>>data; //Wrong, as the file was opened in write mode! But using these are valid : cin.getline(data,20); file<<setw(3)<<setprecision(6)<<33.33<<endl; file.write(data,20); //See, just like cout and cin.
13th Jul 2017, 8:46 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
And, to read from a file, you will have to create a reading object. Thus, fstream fout, fin; // fout - File out. - Will help write to file. // fin - File in - Will help read from file. // Other names can be used too, like f1, file, etc but these are closest to cin and cout, thus I use these. fout.open ( "Output.txt" , ios::out ); fin.open ( "Input.txt" , ios::in ); //Note that you cant open a same file for input and output at the same time even in C++.. int a; fout<<"Reading from Input.txt..."<<endl; fin>>a; fout<<"Number - "<<a<<endl; fin.close(); fout.close();
13th Jul 2017, 8:52 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
That is the Pure C++ way of doing things... I use classes and objects to handle files, and you use pointers to a file type (somewhat similar, except the pointer part) to handle files... Yours is a method which is from C and can be used in C++... Well, using fstream over FILE * is better, as fstream gives you more functions, is more exception safe, and is ready for cross platforms... But in the end, its just a personal preference...
21st Jun 2017, 2:12 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
very thanks.. for me...this is the first time that i see your code and your way to read & write from/in binary file we teached defferent way...in the college i have one question to you, what's the different between your code and my code? and what's the best code between them ...?
21st Jun 2017, 1:45 PM
Mohamed Abdalkader
Mohamed Abdalkader - avatar
+ 4
thanks ....my friend My own experience when using files makes some mistakes when using fscanf function with some parameter ...As i recall fscanf and fprintf are used to read or write in txt file... what will use instead of fscanf and fprintf functions ?
21st Jun 2017, 3:33 PM
Mohamed Abdalkader
Mohamed Abdalkader - avatar