C++ file handling for beginners! The easiest way to read/write into text files!

preview_player
Показать описание
📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

I use it to enhance the performance, features, and support for C, C#, and C++ development in Visual Studio.
It is a powerful, secure text editor designed specifically for programmers.

Files are used to store data permanently.
In this video, you'll learn how to read and write into text files using C++.
In order to work with files, first, include the fstream library.
Inside this library there are three data types:
ofstream - used for writing into files
ifstream - used for reading from files
fstream - used for both read and write
In this video, I'm demonstrating the use of fstream object in file handling.

In order to open a file we use open() function.
This function receives two parameters: file name and file open mode.
The modes in which file can be opened are below:
ios::in - opens the file to read(default for ifstream)
ios::out - opens the file to write(default for ofstream)
ios::binary - opens the file in binary mode
ios::app- opens the file to append new info at the end
ios::ate - opens and moves the control to the end of the file
ios::trunc - removes the data in the existing file
ios::nocreate - opens the file only if it already exists
ios::noreplace - opens the file only if it doesn't already exist

We can also combine different modes using symbol |
For example:

However, please don't feel obligated to do so. I appreciate every one of you, and I will continue to share valuable content with you regardless of whether you choose to support me in this way. Thank you for being part of the Code Beauty community! ❤️😇

Contents:
00:00 - Intro
01:02 - Write into a text file using C++
07:46 - Append to a text file using C++
10:35 - Read from a text file using C++
15:56 - Tasks to test your C++ knowledge!

Add me on:
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

CodeBeauty
Автор

You described how to do all this in 20 minutes, what my school book took a million pages to describe. (Give or take a few hundred thousand pages.) Thank you!

tigergumby
Автор

Hey I commented in your last video and I said I was going to have a exam in c++ . I managed to get a 10/10 max score, thank you very much for helping me personally it was such a great journey through these series.

klevismema
Автор

Prefect timing, we are dealing with File I/O this week in class

meekosalas
Автор

I’ve been having the doubt of how read and write from a file for weeks because my programming professor did not explained as good as you. You were the light that lit the way for me because I needed it for a project. Thank you Saldina!!

luxis_
Автор

explained my 3 lectures from college in 15 mins!! this is so sick because Ive actually understood you better

sinnyx
Автор

My solution for the 1st challenge, converting a story to cipher and deciphering back the code in the console:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
fstream Myfile;


// Taking the input string from the user

string story;
cout << "Enter a story: \n";
getline(cin, story);

// Converting the string to character array

char story_char[story.length()];
for (int i = 0; i < story.length(); i++)
{
story_char[i] = story[i];
}

// Ciphering the story and writing it into the

Myfile.open("story.txt", ios::out);
if (Myfile.is_open())
{
for (int i = 0; i < story.length(); i++)
{
Myfile << int(story_char[i]);
}
}
Myfile.close();

// De-ciphering the numbers into
cout << "The deciphered numbers form the story:\n ";
Myfile.open("story.txt", ios::in);
if (Myfile.is_open())
{
string line;
while(getline(Myfile, line)){
int num = 0;
for(int i = 0; i < line.length(); i++){
num = num * 10 + (line[i] - '0');
if(num>=32 && num<=122){
char ch = (char)num;
cout << ch;
num = 0;
}
}
}
}
Myfile.close();

return 0;
}
Input:
Hello, my name is Priyanshu Saxena, and I am from India, and I love watching videos on CodeBeauty channel.
Ciphered Text:
Deciphered in the console:
Hello, my name is Priyanshu Saxena, and I am from India, and I love watching videos on CodeBeauty channel.

priyanshusaxena
Автор

my course constructors ignored this chapter even in exams last semester, now that i want to make my own project i realized how important this topic is

sehrishzarin
Автор

Saldina thank you for making this concept easier to understand. You are definitely an inspiration and I hope to be as great at programming just like you someday.

potatoitis
Автор

Big Bro
3 minutes ago
I just love how you make coding so simple, Saldina. You have a great gift. Keep up the good work👏🏽👏🏽👏🏽

bigbro_songz
Автор

New video from codeBeauty notification, me on the road running as fast as I can to watch it. Thanks Saldina.

ghislain
Автор

Saldina is back🎊🎊🎊🎊👌👌👌👌Namaste You are on time, Wednesday... I respect your thankyou didi 👌

himanshurajpal
Автор

IT HAS BEEN DAYS OF HARDWORK THAT I PASSED THROUGH IN ORDER TO GET MY FIRST APPLICATION, WHICH IS CONCERNED TO BE THE END OF THE YEAR FINAL PROJECT
BY YOUR ENLIGHTENMENT; YOU MAKE ME SO HAPPY BECAUSE I COULD SOLVE MANY PROBLEMS THAT I FACED WITH FILES

omarbenamer
Автор

I have an exam on Object oriented programming in less than 2 hours and these videos are a life saver :- )))

nopecharon
Автор

I recently started to work on a project which is in C++. I literally knows nothing about C++ and came to you channel. You literally saved my work... Please continue doing this! Many thanks!

cnjrhdf
Автор

3:20​ @CodeBeauty Yes the ".txt" extension is just to make Windows' shell use whatever default program to open these text files. But otherwise, any extension is good as well.
The important point however is that, used as such, the fstream::open() function opens files in **text mode** by default, unless one ORs the second parameter (the mode) with ios::binary. This is important, because the default text mode is "translated" in the sense that depending on the platform you run the code, the end-of-line character being issued in the file will be different (\n on *nix, \r on mac, \r\n on Windows), even if you just use "\n" (without "\r") in the code.

hbm
Автор

Thanks Saldina,
I was waiting for ages for this video.
Actually I have a request to make, that if you find the time to do so, make an advance level video about file handling like about reading mixed data (numbers, alphabets, symbols, spaces, without new line) and taking out the specific type of data according to our desire and storing it in arrays, and also more info about functions like getline and putting some conditions in their parameters to ignore specific characters.
Me and my friends will be waiting desperately. 🤞🏻🤞🏻

glitch
Автор

I cannot believe that I finally understand this, I'm gonna cry😭. Anyway let me get started with the homework you gave us

con_el_maestro
Автор

Thanks Sister, for Muslims تقبل الله صيامكم

abdelfetahissellal
Автор

Hi Saldina! Thanks for your great videos. Going to watch them all🙏

Joinwithmeonmyjourney