Hello, I'm Duck.
This time, we read files using C language during software training.
The program doesn't know what's inside the file until it reads it.
What to do when encountering an unknown file?
This article will focus on the topic of file length.
Also, this time, we used the function fscanf to read and display files that do not contain newlines or spaces.
Read short text file
I made the following source code to read a text file.
test1 code
What it does is
1. Open a text file with fopen
2. Read the string in the text file with fscanf and display the string read with printf on the console for confirmation
3. Close the text file with fclose
becomes.
As a test, I read the following 7-character file.
入力ファイル (read.txt)
test1 output result
You did it right.
However, this code had a fatal flaw.
You can't change the length of a string! ?
I read in a longer 14 character file.
入力ファイル (read.txt)
output.
test1 output result
The read string is not displayed and an error message is displayed.
Why?
This time, the array for reading the file is str[10].
In other words, memory is prepared for only 10 characters.
If it exceeds that, the characters cannot be read.
reading a string
When defining an array, you must first fix the length of the array.
However, when reading a file, you don't know the length of the contents until you open it, so you don't know how much size you need.
Even so, preparing a huge amount of memory will be wasteful in the program.
Then what should I do.
light of hope malloc
Here, as I introduced last time, save the character string using a pointer instead of an array.
test2 code
It's been a little longer. The following two points were added.
1. Go to the end of the file with fseek, substitute the length up to that point in size, and return to the beginning.
2. Allocate memory for the size with malloc.
This way, after opening the file, check the length of the string in the file.
By allocating memory for that size, you can read the file without specifying the size of the array first.
では、先ほどの 図 4 の 14 文字のファイルを出力してみましょう。
test2 output result
output correctly.
I see, you can change the length of an array whose length you don't know by using malloc after you've finished writing the code.
as a side note
The memory area allocated by malloc must be freed using the free function when finished using it.
If you continue to use the memory without freeing it, the memory will fill up and an unexpected error will occur.
There is no problem when securing a small memory area like this time, but you should not forget when securing a large area or securing a large area.
free usage example
Duck C Language Related Articles
Easy-to-understand explanation of the basics of the C language, which is indispensable for embedded systems!
Initial value problem ver.C language
Forgetting initial values can cause trouble! ?
Actually the same person! ? arrays and pointers
Arrays and pointers, use these two that look similar but are different!
malloc ~challenge to the unknown~
Explains how to use memory, which can be said to be a feature of the C language!
Is it unreadable to humans? Binary file mystery
What is in the contents of that file that cannot be read as text! ?