background top icon
background center wave icon
background filled rhombus icon
background two lines icon
background stroke rhombus icon

Download "Программирование на Си урок 37: Работа с файлами в языке Си"

input logo icon
Video tags
|

Video tags

Видео курс
Программирование
Си
С
Программирование с нуля
Python
Assembler
Linux
Windows
Visual Studio Code
gcc
Ubuntu
Subtitles
|

Subtitles

subtitles menu arrow
  • ruRussian
Download
00:00:02
Hello everyone, Dmitry is here, and in
00:00:05
today’s lesson we will begin to
00:00:07
examine a new topic: creating files,
00:00:11
creating files, reading data from files
00:00:15
and writing data
00:00:17
files. So, this topic, I think, is
00:00:21
quite interesting and
00:00:23
relevant. You can, for example, read
00:00:25
some data from files into your
00:00:26
program or, on the contrary, output
00:00:28
some of your data
00:00:30
to a
00:00:31
file on your hard
00:00:34
drive. So, let's first create a
00:00:38
new
00:00:43
file and call
00:00:51
it
00:00:58
Create and
00:01:01
create a read record, roughly speaking,
00:01:05
so in the comments I will write what we
00:01:08
will have and working with
00:01:13
files files
00:01:15
A
00:01:19
using A data type,
00:01:23
file
00:01:25
A and the function Open and
00:01:32
F
00:01:33
Open and
00:01:38
F So now I
00:01:48
connect the standard header file
00:01:50
ST and then let me immediately create
00:01:58
a constant that will be useful to us
00:02:04
further Well, the
00:02:06
Main function So working with
00:02:18
files So A in the standard
00:02:21
header file std o we
00:02:26
have a data type called a
00:02:28
pointer to
00:02:30
a file, it’s
00:02:32
called a file, then it goes on. Actually,
00:02:35
the pointer itself And the name of the pointer is
00:02:41
arbitrary, for example, file
00:02:43
PT That is, I created a pointer with a new
00:02:47
data type file, this is exactly the
00:02:50
file and PT, let’s say this is my
00:02:53
pointer now take certain
00:02:56
values
00:02:58
that
00:03:00
In general, it will
00:03:05
contain a pointer to the file. Yes, roughly
00:03:08
speaking, I’ll express myself further, you
00:03:11
will understand.
00:03:13
So, in order for us to open
00:03:18
a file, for example, create it or
00:03:21
open or write something to it, we
00:03:24
need to first open it And as you
00:03:27
probably already guessed, opening we will
00:03:28
produce exactly a pointer with a
00:03:30
file data type, it is this that will work with
00:03:34
our file, check whether the file is open, not
00:03:36
opened, and the like. So I
00:03:40
write the file PT equals That is, the file PT I
00:03:45
will write the
00:03:50
result of opening or not opening
00:03:53
our file So to begin with, we
00:03:55
will need to open the file in order to
00:03:57
open the file in the file and there is a
00:04:01
function called F
00:04:04
Open this function takes two
00:04:07
arguments, the first argument is a string literal
00:04:11
in this case it’s just
00:04:14
the name of the file for example New
00:04:19
F and then I I indicate that its
00:04:22
extension will be txt and the second argument is
00:04:25
again a string literal. In this
00:04:27
case, I must indicate
00:04:32
the mode in which I will open
00:04:34
My file, for example,
00:04:37
W
00:04:39
abbreviation
00:04:41
record So Now I’ll tell you a little
00:04:44
about these
00:04:46
abbreviations So The first is R
00:04:52
Why do we need R this
00:04:57
shadow? If we put it here, then
00:05:02
we will have this file. We will open this file
00:05:06
for
00:05:09
reading. That is, if we have a
00:05:11
file on our hard drive with this
00:05:13
name and this extension, then when we
00:05:16
specify R I I’ll open it if it opens
00:05:20
for reading That is, I can read
00:05:23
further from the file W
00:05:28
means It means
00:05:30
writing, for
00:05:32
example, writing to an existing file or
00:05:38
creating a new file That is, if
00:05:41
we don’t have such a file in the current directory on the
00:05:44
terrible disk, then W also creates a file
00:05:50
with this name and with this
00:05:51
extension that we
00:05:55
indicated here and it can also open an
00:05:58
existing file if we have one and
00:06:01
overwrite it, that is, this option,
00:06:04
if there is a file, opens it and
00:06:07
overwrites its entire
00:06:08
contents Erase the previous contents of the
00:06:13
file I hope it’s clear next option
00:06:18
a a is adding text to our
00:06:22
existing
00:06:26
file
00:06:28
opki file if it has not
00:06:33
been created and I also want to explain that a
00:06:36
adds some values ​​to the file by
00:06:41
simply adding it and not like W
00:06:45
which is just
00:06:58
restarting for example
00:07:01
r+ What is r+ for? How you know R
00:07:05
opens for reading, yes, that is, it opens
00:07:09
by the way, these r+ modes only work in
00:07:13
text files.
00:07:15
That is, it opens the file for
00:07:19
reading Well, and for writing, that is, for
00:07:21
adding some text
00:07:24
w+ means that we open the file for
00:07:27
writing or for reading a text file Well,
00:07:32
ap means that we want to write
00:07:35
any information to the file before opening it
00:07:37
for reading or writing,
00:07:45
there are also such records as RB Or
00:07:49
let's say
00:07:50
wb this means that we will work
00:07:53
with a binary file means binary
00:07:56
Well, Actually, what you would need to
00:07:58
know is the argument of the function f Open; again,
00:08:02
the function F Open, in case of
00:08:06
good luck, for example, will return us a pointer to
00:08:09
our file. And in case of failure, F Open
00:08:12
returns data type N, that is,
00:08:15
nothing. So let's try it with you.
00:08:18
now create a new file So, as
00:08:21
you can see, I'm
00:08:23
trying to create W or write a new
00:08:28
file New F tex,
00:08:30
now I have to check what
00:08:31
the function F
00:08:33
Open returned from me if, for
00:08:36
example, ptr, but this is where
00:08:43
the value of the function f Open was returned If the returned
00:08:46
value is not equal
00:08:49
n
00:08:52
then I use the standard function and
00:08:55
write
00:08:57
what, for example, was
00:09:08
successfully
00:09:15
created. And if my file was not
00:09:21
created successfully, then
00:09:23
I have a standard error stream
00:09:26
called
00:09:27
stdr, I will send a message that it
00:09:34
was not possible to create or
00:09:37
open
00:09:39
the file and indicate which file, for example
00:09:43
this one
00:09:46
file And now I have to return the error code here, of
00:09:50
course, I
00:09:57
return it with wash
00:10:00
1 as you know Return 0 returns
00:10:04
that everything is fine and Return 1 returns
00:10:08
an error Well, in principle, our
00:10:11
program is written Let's try to
00:10:12
compile it So I use gcc -
00:10:18
O Create
00:10:20
Write so
00:10:25
X
00:10:28
so
00:10:31
missed
00:10:36
a letter So compile then
00:10:41
SSH X I launch As you can see, the
00:10:46
New File txt file has been successfully
00:10:49
created Well, let's check whether
00:10:52
he created it for us or not So I
00:10:55
'm in the New
00:10:56
File directory As you can see, here's the New file,
00:11:00
again I I can look at LS mi If
00:11:03
you want from the terminal Yes and how You see here
00:11:06
New file text I really have such a file
00:11:10
Let's look at it
00:11:14
So New F I'll try to open it As
00:11:17
you can see the file opens the file really
00:11:19
exists that is, we have successfully
00:11:21
created a new text file Excellent
00:11:25
now Let's
00:11:28
learn how to
00:11:30
read data from our file and then read data from
00:11:33
our
00:11:37
file Well, let's probably first
00:11:39
Let's learn how to read data from a
00:11:42
file So let's work with the
00:11:46
same file
00:11:47
New So Here in this branch
00:11:50
If if we have fa there is Lin Yam
00:11:58
here from this file So, to begin with,
00:12:01
of course, I have to write something into
00:12:02
this
00:12:03
file. So, for example, I’ll open a file and
00:12:07
let’s say I write something in it.
00:12:10
And
00:12:28
we
00:12:36
let it be. Here’s a simple line,
00:12:37
we’re programming in C language, we’re studying a
00:12:39
new topic on working with
00:12:41
files Well, just as an example
00:12:45
Yes, so now I need to read
00:12:48
data from a file So how can I do this?
00:12:51
I can read data from a file
00:12:53
using various functions, the simplest
00:12:55
option is to use the functions f and
00:12:59
Get C or FP S, or you could
00:13:05
use other functions but I
00:13:06
will use the FP S function. The
00:13:10
F
00:13:13
S function allows us oh fgts fgts allows
00:13:20
us to receive data from external
00:13:24
files or from any stream. In
00:13:26
this case, the stream we have is exactly this, this is
00:13:29
our index file PT if you don’t
00:13:32
know about the fgs function, then take a look my
00:13:34
previous videos there I described working with
00:13:37
this
00:13:38
function So fgs we receive three
00:13:41
arguments and the first argument is where
00:13:44
we want to
00:13:46
write our string in this case I have
00:13:50
nowhere to write yet so I’m writing
00:13:59
the contents of our text file As
00:14:02
you can see, I passed it a constant here n
00:14:07
as the dimension of this array
00:14:10
So I write what I want to
00:14:13
write a certain
00:14:17
number of
00:14:20
bytes in s Yes the length and I indicate where the file is from
00:14:27
T where I want to
00:14:31
write from So after I read
00:14:35
more precisely I received the data from the
00:14:44
file I need to output it to My
00:14:47
terminal for this I can use
00:14:49
ordinary functions Well, let me use
00:14:55
the function to
00:14:58
output what I want, I want to output
00:15:01
the contents of STR in this case, I better
00:15:03
use the percentage S And here I will write
00:15:10
STR Well, here I will put with a
00:15:15
line feed, that is, like this I I’ll just display
00:15:19
the contents of our array, that is, using
00:15:22
fgs I write a certain
00:15:26
number of characters in p, for example 100 Yes, from
00:15:30
the index of this file ptr file ptr
00:15:32
I have exactly this, this is mine This
00:15:35
file is open Well, let’s
00:15:39
see, let’s clean
00:15:41
the console,
00:15:46
compile fil New File Text has been successfully
00:15:52
created, so something happened here, but
00:15:56
we have an empty line. Uh-huh.
00:16:00
Let's then use
00:16:06
while, so for now
00:16:09
I have
00:16:11
the truth, I
00:16:27
read it, so the
00:16:31
Russians don’t want to read something, yes, but
00:16:34
my file is actually empty, why is it
00:16:38
empty, right? I had
00:16:43
a file opened before writing, so it
00:16:47
erased it from me,
00:16:48
so let’s replace W
00:16:52
with so for
00:16:57
reading, writing the file, let’s do
00:17:02
so with
00:17:30
the contents
00:17:33
So I started compiling and how
00:17:36
You see, I have a program C Language I
00:17:45
read everything perfectly,
00:17:49
let’s try to read the Russian text
00:17:54
Let’s write it
00:17:57
here
00:18:08
So I’m
00:18:11
closing Let’s
00:18:15
restart everything successfully, it works for us, it’s
00:18:18
readable in both English and
00:18:21
Russian, and in this case
00:18:26
sln could, in principle, not be
00:18:28
specified
00:18:33
So the New F text file has been successfully created
00:18:38
Everything worked out perfectly further We are
00:18:41
reading from the
00:18:43
file here are two lines which I read
00:18:46
from
00:18:47
the file here Of course, you can correct
00:18:50
something,
00:18:53
let’s say
00:19:09
we read the data from the
00:19:15
file and after closing.
00:19:20
Well, more precisely, when we finish
00:19:23
reading, we will also write a message
00:19:26
that the reading is completed.
00:19:43
So Let’s try again
00:19:57
recombination
00:20:00
so recombination
00:20:08
could be put here / N
00:20:19
Yes, for example, put it here like this
00:20:30
to make it more clear Yes, it’s clear
00:20:32
So
00:20:38
compiled from a file So I’m reading the
00:20:41
contents of the file PR programmed
00:20:45
working with files reading data from the
00:20:47
file
00:20:49
is completed Great, we just
00:20:51
learned how to read data from the
00:20:57
file, it’s formatted, that is, this solution
00:21:00
will work both on Linux and on
00:21:01
Windows Well, I think that it will also work on osx,
00:21:05
but well now Let's
00:21:09
try to write the data to a file instead of this is
00:21:12
how we read the
00:21:14
data Yes now Let's try to
00:21:16
write the data to our
00:21:20
file So let's
00:21:23
write to for example, after reading
00:21:26
data from a file I want to write something here and here
00:21:31
Well, let's Try to write
00:21:34
the data Yes, so I use another
00:21:38
Char array
00:21:42
Well, let it be
00:21:45
name, for example, I will pass here this
00:21:48
certain
00:21:57
length
00:22:21
So I have a string I I want to
00:22:27
write this line, for example, to our
00:22:29
file. So, what can I do? For
00:22:34
example, I can
00:22:39
use the function a
00:22:44
STR cpi Yes,
00:22:47
but I can just write
00:22:51
the file forcibly before.
00:22:54
So, after my reading is
00:22:56
completed, I want to simply
00:22:59
and Add this file How can I
00:23:01
do this I can use a regular
00:23:03
function called
00:23:09
F if you don’t know how to work with this
00:23:11
function then watch my previous
00:23:14
videos So the function takes two
00:23:17
arguments the first argument is what I want to
00:23:20
write for example
00:23:27
I’m a function
00:23:30
A string yes And where I want to write
00:23:34
write I want file
00:23:36
T, that is, in our open
00:23:40
file Well,
00:23:47
let me put
00:23:50
the line feed here so that
00:23:52
I have it so Let's
00:23:54
try to reforge
00:23:58
everything so
00:24:01
it worked for us I got the data read and
00:24:03
here I made a record Now let's
00:24:06
see have I written something to
00:24:08
my file So New File I open the file and
00:24:11
As you can see I have the line
00:24:15
byy From the lammer to the
00:24:17
programm here this is the name of my channel in this
00:24:21
simple way you can
00:24:24
write any data to a file or
00:24:26
write create a file completely Well,
00:24:29
this is a rather simple method,
00:24:32
we could of course
00:24:36
use, for example, other
00:24:38
possibilities Yes, other functions But with
00:24:42
this simple example, I
00:24:44
showed you How you can read data
00:24:48
from a file and how You see here you can it
00:24:54
was possible to write data into our file, including in Russian, write it in
00:25:02
English again,
00:25:04
let's see, yes
00:25:08
in Russian Add it
00:25:15
Yes, for example, Dmitry Yes, my
00:25:18
name
00:25:21
So the
00:25:26
compiled one should have been written down
00:25:29
So let's try to open it from here
00:25:33
As you can see,
00:25:36
I have a line written in
00:25:39
English and the line was written in
00:25:43
Russian That is, it’s
00:25:46
quite easy to
00:25:48
create your own files. First of all, you can
00:25:51
write
00:25:53
data to a file and you can read data
00:25:56
from a file, that is, write a file and
00:26:00
read data from a
00:26:02
file. Well, you can
00:26:05
modernize this program as you
00:26:08
want can be allowed Here,
00:26:13
directly in the console, specify the file
00:26:15
that you want to create or open
00:26:18
using command line arguments Yes,
00:26:21
well, you can already do this yourself Well,
00:26:24
Dmitry was with you, like,
00:26:26
subscribe to my channel if you like
00:26:29
like or and if you are not subscribed
00:26:31
Subscribe if you like the video Like the video
00:26:33
If you don’t like the video
00:26:35
put a dislike Well, in general, at your
00:26:38
discretion Well, I say goodbye to you
00:26:41
Good luck to everyone and
00:26:43
bye

Description:

Исходники из 37 урока: http://my-files.ru/2bjun2 Создание файла, чтение из файла, запись в файл на языке программирования Си. Данное решение является кросплатформенным и работает как на Linux так и Windows. Но есть и другие способы как все сделать вышеописанное на более низком уровне но реализация будет только под Linux ) Поддержать канал: If You want to support the channel: Донат / Donate RUB R939373054553 USD Z657301568665

Preparing download options

popular icon
Popular
hd icon
HD video
audio icon
Only sound
total icon
All
* — If the video is playing in a new tab, go to it, then right-click on the video and select "Save video as..."
** — Link intended for online playback in specialized players

Questions about downloading video

mobile menu iconHow can I download "Программирование на Си урок 37: Работа с файлами в языке Си" video?mobile menu icon

  • http://unidownloader.com/ website is the best way to download a video or a separate audio track if you want to do without installing programs and extensions.

  • The UDL Helper extension is a convenient button that is seamlessly integrated into YouTube, Instagram and OK.ru sites for fast content download.

  • UDL Client program (for Windows) is the most powerful solution that supports more than 900 websites, social networks and video hosting sites, as well as any video quality that is available in the source.

  • UDL Lite is a really convenient way to access a website from your mobile device. With its help, you can easily download videos directly to your smartphone.

mobile menu iconWhich format of "Программирование на Си урок 37: Работа с файлами в языке Си" video should I choose?mobile menu icon

  • The best quality formats are FullHD (1080p), 2K (1440p), 4K (2160p) and 8K (4320p). The higher the resolution of your screen, the higher the video quality should be. However, there are other factors to consider: download speed, amount of free space, and device performance during playback.

mobile menu iconWhy does my computer freeze when loading a "Программирование на Си урок 37: Работа с файлами в языке Си" video?mobile menu icon

  • The browser/computer should not freeze completely! If this happens, please report it with a link to the video. Sometimes videos cannot be downloaded directly in a suitable format, so we have added the ability to convert the file to the desired format. In some cases, this process may actively use computer resources.

mobile menu iconHow can I download "Программирование на Си урок 37: Работа с файлами в языке Си" video to my phone?mobile menu icon

  • You can download a video to your smartphone using the website or the PWA application UDL Lite. It is also possible to send a download link via QR code using the UDL Helper extension.

mobile menu iconHow can I download an audio track (music) to MP3 "Программирование на Си урок 37: Работа с файлами в языке Си"?mobile menu icon

  • The most convenient way is to use the UDL Client program, which supports converting video to MP3 format. In some cases, MP3 can also be downloaded through the UDL Helper extension.

mobile menu iconHow can I save a frame from a video "Программирование на Си урок 37: Работа с файлами в языке Си"?mobile menu icon

  • This feature is available in the UDL Helper extension. Make sure that "Show the video snapshot button" is checked in the settings. A camera icon should appear in the lower right corner of the player to the left of the "Settings" icon. When you click on it, the current frame from the video will be saved to your computer in JPEG format.

mobile menu iconWhat's the price of all this stuff?mobile menu icon

  • It costs nothing. Our services are absolutely free for all users. There are no PRO subscriptions, no restrictions on the number or maximum length of downloaded videos.