Home Common Problem What are the file types that C language can handle?

What are the file types that C language can handle?

Sep 19, 2022 pm 03:53 PM
c language

The file types that the c language can handle are: text files and binary files. The files that C language can process are divided into text files and binary files according to the storage form: 1. Text files store an ASCII code, and the content of the file can be directly input and output; 2. Binary files directly store characters, and binary files cannot be stored. The contents of the file are output directly to the screen.

What are the file types that C language can handle?

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

In computers, input and output are in the form of data streams. According to the access mode, files are divided into sequential access files and random access files. According to the storage form, it is divided into binary files and text files. The text file stores an ASCII code, and the content of the file can be directly input and output. Binary files store characters directly, and the contents of binary files cannot be output directly to the screen. Therefore, the files that C language can process are divided into text files and binary files according to the storage form.

The difference between text files and binary files

Text files are usually used to save characters visible to the naked eye, such as .txt files, .c files, and .dat files, etc., open these files with a text editor, and we can successfully understand the contents of the files.

Binary files are usually used to save unreadable content such as videos, pictures, programs, etc. If you open these files with a text editor, you will see a bunch of garbled characters that you cannot understand at all.

But physically speaking, there is no difference between binary files and character files. They are both data stored on the disk in binary form.

The reason why we can understand the content of text files is because the text files use character encodings such as ASCII, UTF-8, GBK, etc. The text editor can recognize these encoding formats and convert the encoding values ​​​​to Convert to characters and display them.

The binary files use special encoding formats such as mp4, gif, exe, etc. The text editor does not recognize these encoding formats and can only parse them randomly according to the character encoding format, so it becomes a bunch of messy characters. , some have never even seen it.

If we create a new mp4 file, write a string of characters to it, and then open it with a text editor, you can still understand it. Interested readers can try it themselves.

To sum up, different types of files have different encoding formats, and corresponding programs (software) must be used to parse them correctly, otherwise they will be a bunch of garbled characters or cannot be used.

For programmers, text files and binary files are a statement, indicating how you should open the file (text mode/binary), what function you should use to read and write this file (read and write functions), how The judgment reads to the end of this file.

Specifically:

1. How to open a file?

ANSI C specifies the standard input and output function library, and uses the fopen() function to open files. The calling method of the fopen() function is generally:

FILE *fp;
fp=fopen(文件名,使用文件方式);
Copy after login

See the table below for using the file method:

When the same file is read from the disk to the memory (program data area or cache area), the contents in the memory are generally different in the two methods. This is the substantial difference between the two opening methods.

There is a background here, that is, under Windows, it will do a process, that is, when writing a file, the newline character will be converted into a carriage return. The newline character is stored in the disk file, and when reading the file, the newline character will be converted into a carriage return. file, it will perform reverse processing, that is, convert consecutive carriage returns and line feeds in the file into line feeds.

Therefore, when reading a disk file, the file content read in text mode is likely to be shorter than the binary file, because reading in text mode requires two characters of carriage return and line feed to be converted into one character. , which is equivalent to truncating the file. But why is it just possible? Because there may not be two consecutive bytes 45 and 42 in the text (45 is the ASCII code of CR carriage return, 42 is the ASCII code of line feed CL), there is no "truncation" operation, so reading The content is the same.

Specifically, file files (written in text mode) are best read in text mode. Binary files (written in binary mode) are best read in binary mode. Otherwise it may be incorrect. The above has been analyzed.

2. What function is used to read and write files?

How data is written on the disk is not determined by the file opening method, but by the writing function. How data is read from the disk is not determined by the file opening method, but by the read function.

How to write data mentioned above refers to how to store a type of variable? For example, int 12, you can directly store the binary code of 12 (4 bytes), or you can store character 1 and character 2.

How to read data means that if I want to read an int variable, I can read it directly. sizeof (int) bytes, or read character by character until the character read is not a numeric character.

There are two sets of file reading and writing functions in C that support the above two methods of reading and writing:

  • 1.fread(buffer,size,count,fp), fwrite(buffer,size,count,fp). Used to read and write a data block. It corresponds to the first storage method. Directly specify the number of bytes to read and write according to the byte length of the type.

  • 2fprintf function and fscanf function. It corresponds to the second reading and writing method. That is, reading and writing in character format. (The fprintf function and the fscanf function have similar functions to the printf function and the scanf function. They are all formatted reading and writing functions. The reading and writing objects of the fprintf and fscanf functions are disk files, while the reading and writing objects of the printf and scanf functions are the terminal.)

Their general calling format is:

fprintf (file pointer, format string, output list);

fscanf (file pointer, format string, input list);

3 How to determine the end of the file?

In the C language, or more precisely the C standard function library, there is a special character EOF (this definition in stdio.h #define EOF (-1) ), which means :end of file. In the while loop, EOF is used as the end-of-file mark. The file with EOF as the end-of-file mark must be a text file. In text files, data is stored in the form of character ASCII code values. We know that the range of ASCII code values ​​is 0~255, and -1 is impossible, so EOF can be used as the end of file mark.

However, in C language, when data is stored in a file in binary form, a -1 value will appear. At this time, EOF cannot be used as the end mark of the binary file. To solve this problem, ANSI C provides a feof function to determine whether the file has ended. If the end of the file is encountered, the value of the function feof (fp) is 1, otherwise it is 0.

The feof function can be used to determine whether the binary file ends or the text file ends. However, it should be noted that when feof is used to determine the end of a text file, if the code is not written properly, the end-of-file character EOF in the text may also be read; see http://baike.baidu.com/view/656648 for details. htm

4. Knowing whether a file is a text file or a binary file will "remind" us more about which read and write function we should choose.

As mentioned in 2, how the data is stored is not determined by the file opening method, but by the read and write functions.

For example, if we open a file as a binary file (actually only specifying the conversion of newline characters), it represents more of an idea (virtual): I "hope" The data in this file is like this, int type occupies 4 bytes and char occupies 1 byte. In this mode, I use fread(buffer,size0f(int),1,fp) to read an int into an int variable.

We remember

Before we operate on a file, first of all, we must know whether the file is a text file or a binary file. Files are opened in text mode, binary files are opened in binary mode

If we want to operate a binary file, then we open it in binary mode (theoretically it can also be opened in file mode, but if the binary data is written When there is 45 in it, it will be converted into 45,42 and stored, see 1. This is very likely to happen). When reading and writing at the same time, use the two functions fread and fwrite.

If I want to operate a text file, then we open it in text mode (theoretically it can also be opened in binary mode, but it is not safe). When reading and writing at the same time, use those functions fprintf, fscanf, fgetc, fputc, putw, getw, fgetc, fputs

Related recommendations: "C Video Tutorial"

Using the file method

Meaning

"r" (read-only)

Opens a text for input File

"w" (write only)

Open a text file for output

"a" (append)

Open a text file for append

"rb" (read only)

Open a binary file for input

"wb" (write only)

Open a binary file for output

"ab" (append)

Open a binary file for append

"r "(read and write)

Open a text file for reading/writing

"w "(reading and writing)

for reading/writing Create a text file

"a " (read and write)

Open a text file for reading/writing

"rb "(read and write)

Open a binary file for reading/writing

"wb "(read and write)

Create a binary file for reading/writing

##"ab "(read and write)

Open a binary file for reading/writing

The above is the detailed content of What are the file types that C language can handle?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C language data structure: data representation and operation of trees and graphs C language data structure: data representation and operation of trees and graphs Apr 04, 2025 am 11:18 AM

C language data structure: The data representation of the tree and graph is a hierarchical data structure consisting of nodes. Each node contains a data element and a pointer to its child nodes. The binary tree is a special type of tree. Each node has at most two child nodes. The data represents structTreeNode{intdata;structTreeNode*left;structTreeNode*right;}; Operation creates a tree traversal tree (predecision, in-order, and later order) search tree insertion node deletes node graph is a collection of data structures, where elements are vertices, and they can be connected together through edges with right or unrighted data representing neighbors.

The truth behind the C language file operation problem The truth behind the C language file operation problem Apr 04, 2025 am 11:24 AM

The truth about file operation problems: file opening failed: insufficient permissions, wrong paths, and file occupied. Data writing failed: the buffer is full, the file is not writable, and the disk space is insufficient. Other FAQs: slow file traversal, incorrect text file encoding, and binary file reading errors.

C language multithreaded programming: a beginner's guide and troubleshooting C language multithreaded programming: a beginner's guide and troubleshooting Apr 04, 2025 am 10:15 AM

C language multithreading programming guide: Creating threads: Use the pthread_create() function to specify thread ID, properties, and thread functions. Thread synchronization: Prevent data competition through mutexes, semaphores, and conditional variables. Practical case: Use multi-threading to calculate the Fibonacci number, assign tasks to multiple threads and synchronize the results. Troubleshooting: Solve problems such as program crashes, thread stop responses, and performance bottlenecks.

How to output a countdown in C language How to output a countdown in C language Apr 04, 2025 am 08:54 AM

How to output a countdown in C? Answer: Use loop statements. Steps: 1. Define the variable n and store the countdown number to output; 2. Use the while loop to continuously print n until n is less than 1; 3. In the loop body, print out the value of n; 4. At the end of the loop, subtract n by 1 to output the next smaller reciprocal.

CS-Week 3 CS-Week 3 Apr 04, 2025 am 06:06 AM

Algorithms are the set of instructions to solve problems, and their execution speed and memory usage vary. In programming, many algorithms are based on data search and sorting. This article will introduce several data retrieval and sorting algorithms. Linear search assumes that there is an array [20,500,10,5,100,1,50] and needs to find the number 50. The linear search algorithm checks each element in the array one by one until the target value is found or the complete array is traversed. The algorithm flowchart is as follows: The pseudo-code for linear search is as follows: Check each element: If the target value is found: Return true Return false C language implementation: #include#includeintmain(void){i

C language data structure: the key role of data structures in artificial intelligence C language data structure: the key role of data structures in artificial intelligence Apr 04, 2025 am 10:45 AM

C Language Data Structure: Overview of the Key Role of Data Structure in Artificial Intelligence In the field of artificial intelligence, data structures are crucial to processing large amounts of data. Data structures provide an effective way to organize and manage data, optimize algorithms and improve program efficiency. Common data structures Commonly used data structures in C language include: arrays: a set of consecutively stored data items with the same type. Structure: A data type that organizes different types of data together and gives them a name. Linked List: A linear data structure in which data items are connected together by pointers. Stack: Data structure that follows the last-in first-out (LIFO) principle. Queue: Data structure that follows the first-in first-out (FIFO) principle. Practical case: Adjacent table in graph theory is artificial intelligence

The concept of c language functions and their definition format The concept of c language functions and their definition format Apr 03, 2025 pm 11:33 PM

C language functions are reusable code blocks, receive parameters for processing, and return results. It is similar to the Swiss Army Knife, powerful and requires careful use. Functions include elements such as defining formats, parameters, return values, and function bodies. Advanced usage includes function pointers, recursive functions, and callback functions. Common errors are type mismatch and forgetting to declare prototypes. Debugging skills include printing variables and using a debugger. Performance optimization uses inline functions. Function design should follow the principle of single responsibility. Proficiency in C language functions can significantly improve programming efficiency and code quality.

C language conditional compilation: a detailed guide for beginners to practical applications C language conditional compilation: a detailed guide for beginners to practical applications Apr 04, 2025 am 10:48 AM

C language conditional compilation is a mechanism for selectively compiling code blocks based on compile-time conditions. The introductory methods include: using #if and #else directives to select code blocks based on conditions. Commonly used conditional expressions include STDC, _WIN32 and linux. Practical case: Print different messages according to the operating system. Use different data types according to the number of digits of the system. Different header files are supported according to the compiler. Conditional compilation enhances the portability and flexibility of the code, making it adaptable to compiler, operating system, and CPU architecture changes.