Introduction
Hello, my name is Duck.
This time we used the C programming language to read a file during software training.
Until a file is read, the program does not know what it contains.
What do you do when you come across an unknown file?
This article will talk about the length of the file.
In addition, this time we used the function ”fscanf” to read and display files that do not contain newlines or spaces.
Loading a short text file
The following source code was created to read a text file.
【 Figure 1 】 test1 Code
Operation details are as follows,
1) Open a text file with fopen
(2) Read strings in the text file with fscanf and display them on the console with printf for confirmation
(3) Close the text file with fclose
The following is an example of a text file.
As a test, we read the following 7 character file.
【 Figure 2 】 The input file ( read.txt )
【 Figure 3 】 test1 Output result
The code was correct.
However, this code had a fatal flaw.
The length of the string cannot be changed!
A longer 14-character file was loaded.
【 Figure 4 】 Input file ( read.txt )
Output file ( read.txt )
【 Figure 5 】 test1 Output Result
The loaded string is not displayed and an error message appears.
Why is this?
This time, the array used to read the file is str[10].
In other words, we only have enough memory for 10 characters.
If the number is exceeded, characters cannot be read.
【 Figure 6 】 Reading a String
When defining an array, the length of the array must be fixed at the beginning.
However, when reading a file, you do not know the length of its contents until you open it, so you do not know how large it needs to be.
This means that preparing a huge amount of memory is wasteful from a programmatic standpoint.
So what should we do?
A ray of hope: malloc.
Here, as introduced in the previous article, we use pointers to store strings instead of arrays.
【 Figure 7 】 test2 Code
The code is a bit long. The main additions are the following two points.
(1) fseek goes to the end of the file, assigns the length of the file to size, and returns to the top of the file.
(2) malloc allocates memory for the size.
In this way, after opening a file, the length of strings in the file is checked.
By allocating memory for that size, the file can be read without first specifying the array size.
Now, let's output the 14-character file【 Figure 4 】 shown in the previous .
【 Figure 8 】test2 Output Result
The output is correct.
I see that the length of an array whose length is unknown can be changed after the code is written by using malloc.
By the way
The memory area allocated by malloc must be released using the ”free” function when you are done using it.
If you continue to use the memory without freeing it, it will fill up and cause an unexpected error.
If you are allocating a small amount of memory, as in this case, there is no problem, but if you are allocating a large amount of memory or a large area, you must not forget to do so.
【 Figure 9 】 free Example of Use
New Engineer's Blush Blog Articles