C++ Binary Files | Read, Write Methods | CPP Programming File Management Video Tutorial

  Рет қаралды 108,631

LearningLad

LearningLad

Күн бұрын

Пікірлер: 82
@davigomides161
@davigomides161 Жыл бұрын
Your File Handling course saved me, thank you brother! I'm in the 2nd year in Computer Science and haven't understood file handling yet. After your course i don't have any doubts. Thanks for this amazing tutorials, keep the good work !
@LearningLad
@LearningLad Жыл бұрын
glad my video helped you. keep learning :)
@forrealkye
@forrealkye 2 жыл бұрын
Dude you just helped me with an assignment I couldn't find how to use .read() to assign values to an instance anywhere else, thank you so much!!!
@LearningLad
@LearningLad 2 жыл бұрын
Pleasure
@samhackett4181
@samhackett4181 8 жыл бұрын
Thank you! This is the clearest explanation of this topic I've found.
@truthworld3d
@truthworld3d 7 жыл бұрын
Learning Lad Rocks!! Anil Shetty is awesome!! Thank you so much... you saved my life.
@LearningLad
@LearningLad 7 жыл бұрын
:)
@vikaskamble3431
@vikaskamble3431 3 жыл бұрын
Thank you so much brother for this series... this series helped me a lot in my project..
@LearningLad
@LearningLad 3 жыл бұрын
Pleasure
@MsRedCatastrophe
@MsRedCatastrophe 9 жыл бұрын
Thank you so much! This cleared up so many problems I was having. I was able to finish my cs homework in ten minutes thanks to you. Looking forward to watching more of your videos.
@LearningLad
@LearningLad 9 жыл бұрын
Caitlin A great! i'm glad that my videos helped you :)
@rajanibhatt5415
@rajanibhatt5415 7 жыл бұрын
just too helpful in the exam time please continue these types of vedioes
@LearningLad
@LearningLad 7 жыл бұрын
sure :)
@CreamCity
@CreamCity 7 жыл бұрын
in this program how would you read from the middle of the file? how would you change file.seekg(0) to file.seek(1) and still have a functional program?
@emcemimotionandcontrol5554
@emcemimotionandcontrol5554 3 жыл бұрын
You can't. It's a serial of objects stored in a file.
@richarddanladi9802
@richarddanladi9802 8 жыл бұрын
Great tutorial sir. I finally got it.
@LearningLad
@LearningLad 8 жыл бұрын
Awesome. Thank You :)
@jackiechan8459
@jackiechan8459 9 ай бұрын
amazing tutorial!
@LearningLad
@LearningLad 9 ай бұрын
Thank you!
@zakariachahboun
@zakariachahboun 8 жыл бұрын
amazing tutoriel ! simple and short thank you sir
@LearningLad
@LearningLad 8 жыл бұрын
my pleasure :)
@digimikeh
@digimikeh 3 жыл бұрын
Thanks Anil..
@LearningLad
@LearningLad 3 жыл бұрын
Pleasure
@ishan7824
@ishan7824 7 жыл бұрын
Why there is a need of type casting object name?
@jamunabaskar7537
@jamunabaskar7537 7 жыл бұрын
thanks a lot, your videos are are incredibly helpful for me to understand the concept thank you
@LearningLad
@LearningLad 7 жыл бұрын
my pleasure. keep Learning :)
@VijaydBazz
@VijaydBazz 8 жыл бұрын
You are the best bro..very nice and detailed explanation...other tutorial sucks!!!
@LearningLad
@LearningLad 8 жыл бұрын
Thank you :)
@VijaydBazz
@VijaydBazz 8 жыл бұрын
Welcome...:)
@AbhishekSingh-yh7ce
@AbhishekSingh-yh7ce 4 жыл бұрын
Awesome videos on C++ to learn as a noob programmer. Enjoying my time with these great videos during this great lockdown. #ThanksLearningLad❤️
@gomesluis40
@gomesluis40 10 жыл бұрын
thank you very much.....this clears so many doubts :)
@LearningLad
@LearningLad 10 жыл бұрын
luis gomes glad that my video helped you :)
@4Y0P
@4Y0P 8 жыл бұрын
Thank you very much! (: Helped me out
@LearningLad
@LearningLad 8 жыл бұрын
+Whims glad to hear that . happy Learning :)
@anmolbehl8572
@anmolbehl8572 7 жыл бұрын
Sir it's not working for me in code blocks
@alg0rithm1
@alg0rithm1 9 жыл бұрын
How portable is this? Can I treat the binary file as a library and expect to work on a different computer? Thanks
@bonbonpony
@bonbonpony 5 жыл бұрын
No, you can't. The technique presented here is not portable, mostly due to endianness. Different platforms use different order of bytes in a multi-byte number, so just reading them into a struct happy go lucky may easily break on a computer with a different endianness. Also, compilers can use padding between the fields of data structures to make memory accesses faster, leaving some spaces between them. In that case, reading data from file into the memory of the object and interpreting them as the object won't work either, because everything will get corrupted if the actual layout in memory is different than in the file. You can kinda avoid that by packing your data structures in memory (using `#pragma pack`), but then it may damage the memory access speed because the fields won't be properly aligned in memory and the CPU will have to make two memory accesses (or more) instead of one to get the entire piece of data. Data in memory should be aligned, and data in binary files should be packed. Simple as that. So you need to come up with a procedure of converting the data back and fort from/to packed representation during reading/writing. And make sure you read the bytes of a number in correct order, matching the endianness of your platform. Then the code will be fully portable.
@carlosgarciavigoa7937
@carlosgarciavigoa7937 5 жыл бұрын
@@bonbonpony Excuse Bon Bon, but every time than I find a video about reading and writhe file or binaries, you always make a complaint about is not correct or is wrong. So now I don't know is this correct or not. I know C++ and I'm in the process to make thing correctly. Can you explain how can we do it the right way? PLS!
@bonbonpony
@bonbonpony 5 жыл бұрын
@@carlosgarciavigoa7937 And?
@carlosgarciavigoa7937
@carlosgarciavigoa7937 5 жыл бұрын
@@bonbonpony Can you do a video on how to do it?
@bonbonpony
@bonbonpony 5 жыл бұрын
@@carlosgarciavigoa7937 I don't have an equipment, but I don't think a video is necessary. I can explain it in short here. First the padding of structures: As I said, they should remain aligned in memory, because that's how the CPU can access their fields faster, but remain packed in binary files, because that way they take less disk space. So you have to come up with a way of converting one representation into another (which is a good thing anyway, because it may isolate your code from the actual binary representation, which may then change without affecting the code). If you read one field at a time, you have this problem solved. Except for the endianness, so let's get into that now. One way do do that is by reading one byte at a time, because endianness don't matter for bytes, only for multi-byte sequences (i.e. numbers). So if you have to read some multi-byte sequences, you can read them one byte at a time and then put together in the correct order by bit shifting and masking. (Or you can use a bit field and let the compiler do the bit fiddling for you, but this is a bit less reliable due to sloppy standards and their implementations.) It's good to test the endianness of the platform before reading any multi-byte sequences. You can do that by writing some byte pattern in a known order into memory, and then reading it back as a multi-byte number, then compare which of the possible numbers you got. (Many file formats use "magic signatures" in the beginning of the file for that very purpose.) For example, if you write 0xCA into the lower address and 0xFE into the higher address, and after reading it back as 16-bit number you got 0xCAFE, then your platform is big-endian. If you got 0xFECA instead, your platform is little-endian. You can even make it a compile-time test if you don't want to branch at runtime, and then let the compiler generate one of two versions of your code depending on the endianness of the platform. There are also some library functions for byte swapping and detecting the endiannes, so you can use that as well.
@khurshidernazarov809
@khurshidernazarov809 5 жыл бұрын
There was no need to create classes, you could save everyone's time. But anyways thanks.
@LearningLad
@LearningLad 5 жыл бұрын
thanks for the suggestion. will be remaking this series soon. i will keep this mind.
@gauravmangal5191
@gauravmangal5191 5 ай бұрын
Hello, I just liked all of your videos from this playlist on File management. they are good, other then you trying to throw English accent which was annoying🤣🤣, Everything was great and very helpful. Thank you for your time.🙂😊
@LearningLad
@LearningLad 5 ай бұрын
Stupid me 😀
@p9malino267
@p9malino267 7 жыл бұрын
Your tutorials are very good but goodness me, improve that code layout!
@hafidzrazman5365
@hafidzrazman5365 6 жыл бұрын
can you please explain why we need to use person(char *name,int age) ?? why we need to use pointer there? enlighten me please
@sarathelayadath2565
@sarathelayadath2565 4 жыл бұрын
will it work if there is a variable size memeber in the class ?
@nutcrackzengine1232
@nutcrackzengine1232 8 жыл бұрын
Hi LearningLad, how can i use this awesome method to read an existing file without writing to it first? thanks for this awesome tutorial :)
@EBKCS_SamarthGoyal
@EBKCS_SamarthGoyal 2 жыл бұрын
Sir instead of storing name as a char array if I want to take name as string class object then how to store name in a file ?
@sahelahmed0093
@sahelahmed0093 8 ай бұрын
Damn this saved me
@LearningLad
@LearningLad 8 ай бұрын
glad my video helped you :)
@ДимаКимсыщик
@ДимаКимсыщик 8 жыл бұрын
i got it finally ))). it is read functions specficity
@wasiimo
@wasiimo 8 жыл бұрын
(char*)&anil just means we're going to have a char pointer pointing to the byte data which make up anil.
@baczek40
@baczek40 2 жыл бұрын
thanks bro!
@MrHmm-cv6gs
@MrHmm-cv6gs 5 жыл бұрын
you just made the tutorial difficult using constructer, though there was no need for that
@dmitryalexandersamoilov
@dmitryalexandersamoilov 4 жыл бұрын
but i learned more about constructors!
@MrHmm-cv6gs
@MrHmm-cv6gs 4 жыл бұрын
@@dmitryalexandersamoilov great👍
@shahrukhtramboo6781
@shahrukhtramboo6781 7 жыл бұрын
sir i am getting an ISO file as output. and it shows INVALID IMAGE FORMAT. i cant open it. why is that . please help
@ajw17
@ajw17 8 жыл бұрын
Can you please explain how the (char*)&anil works? What exactly is happening here? Is this a cast?
@konstantinosdimitropoulos3533
@konstantinosdimitropoulos3533 7 жыл бұрын
Dude i did not understand that either
@jakethewoz
@jakethewoz 4 жыл бұрын
The read() and write() methods both read/write one byte at a time. In order to write something bigger than a byte, you have to cast it as a number of bytes. The code is basically saying write('anjali in the form of bytes', 'the size of anjali in bytes').
@Kromush1995
@Kromush1995 7 жыл бұрын
Can someone please explain me why it crashes when it reaches the exit bracket of else branch? Here's the code, in which i create a binary file, write something in it, extract the content into a different string and then displays the output to console. void main() { fstream file("BinaryTest.bin", ios::binary | ios::in | ios::out | ios::trunc); if (!file.is_open()) { cout
@jacksquirrel2001
@jacksquirrel2001 6 жыл бұрын
{ string content = "First line in binary file."; // 1) You can't take the address of a string and // use it to save its contents - you need to use // the data() member function, which returns the // address of the first character in the string. // Note that for string, data() returns a char*, // so there's no need for the (char*) cast: file.write(content.data(), content.size()); //file.write((char*)&content, content.size()); file.seekg(0); // 2) The outputContent string is empty, so // there's no place to read the contents in // to. You need to allocate space so that // all of the expected data can be read. This // can be done using the string constructor. // The following statement initializes a string // with content.size() characters all set to 0. string outputContent(content.size(), 0); //string outputContent; // 3) You need to make the same change // as 1) and use data() instead of &. file.read(outputContent.data(), content.size()); //file.read((char*)&outputContent, content.size()); cout
@maulinshah5275
@maulinshah5275 8 жыл бұрын
hello sir, while doing this program I have an error that [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] plz give me possible solution..
@astudent9206
@astudent9206 5 жыл бұрын
I guess you are using Linux...I think there's a flag for suppressing warnings...I don't quite remember the syntax...You can google it :)
@rahulroyjannapurashivakuma7767
@rahulroyjannapurashivakuma7767 5 жыл бұрын
Awesome tutorial. If we declare Person class data member 'name' as string, then anil.change() function is also changing the name of sunil to "xxxx" but not the age. Please clarify. TIA.
@ДимаКимсыщик
@ДимаКимсыщик 8 жыл бұрын
I don't get it. WHY? it saves an adress of object, then it copies the adress to another object, bu they are different?
@stovegamesgames6917
@stovegamesgames6917 4 жыл бұрын
Where is the documentation for the libraries used?
@GurpreetKaur-pn7vk
@GurpreetKaur-pn7vk 6 жыл бұрын
tell me the significance of cin.get(ch)?
@DickheadEnterprises
@DickheadEnterprises 6 жыл бұрын
What happens if we exclude ios::in and ios::out? Isn't an fstream already implied in and out
@bonbonpony
@bonbonpony 5 жыл бұрын
`ifstream` implies `ios::in`, while `ofstream` implies `ios::out`.
@lavanyalavan8057
@lavanyalavan8057 6 жыл бұрын
The computer screen was not clear.
@LearningLad
@LearningLad 6 жыл бұрын
really sorry about that.try to watch it in 1080p. in some video's we have this issue and we have solved this in our latest video's :)
@hemanthmusic1933
@hemanthmusic1933 6 жыл бұрын
Hi, I have a doubt can you please help me
@muhammadusmanghani1648
@muhammadusmanghani1648 7 жыл бұрын
Why its displaying data twice?
@gosnooky
@gosnooky 7 жыл бұрын
Good tutorial, your code is a mess, though.
@ruchisolanki4755
@ruchisolanki4755 7 жыл бұрын
Voice clearity is less
@LearningLad
@LearningLad 7 жыл бұрын
yes. i can understand. we have solved this problem in latest video's :)
@bonbonpony
@bonbonpony 5 жыл бұрын
This approach is very bad, because it ignores the endianness of the platform (the order in which bytes are stored in a multi-byte number) and data layout in memory (packed vs. unpacked), and therefore it is higly unportable and prone to data corruption. Don't follow these advices in real production code!
@bonbonpony
@bonbonpony 5 жыл бұрын
@Peterolen Standard integer size will solve only the packing problem (and even that only partially, because there still could be some padding between them that will break the compatibility of the binary structures), but not the endianness problem. Bytes of a number can still come in the wrong order.
Stringstream in C++ | CPP Programming Video Tutorial
5:35
LearningLad
Рет қаралды 169 М.
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Exception handling in C++ (How to handle errors in your program?)
24:46
C++ CLASSES & OBJECTS explained easy 🧍
10:54
Bro Code
Рет қаралды 43 М.
Stack vs Heap Memory in C++
19:31
The Cherno
Рет қаралды 587 М.
How to store records in binary files in C++
7:18
Professor Hank Stalica
Рет қаралды 4,4 М.
C++ Reading / Write Binary File
17:30
Tommy Ngo (SW CODING CHANNEL)
Рет қаралды 44 М.
File Handling in C++ Programming
16:06
Simple Snippets
Рет қаралды 375 М.
Reading and Writing from Binary Files in C!
20:26
Astrocode
Рет қаралды 14 М.