Advance understanding of software development (12) configuration file read and file operation

Source: Internet
Author: User
Tags error code ftp

In the field of communication software development projects, C language is the mainstream programming language, and file operation in which occupies a very important position. In addition, to reflect the flexibility of the product, you can add a configuration file to configure some important parameters on demand. This requires that the program be able to read exactly the values of each configuration item.

This paper takes a practical small software as an example, introduces the method of reading the configuration file in C language and the use method of the important file operation function, which provides a useful reference for the related software development project.

1. Foreword

In general software projects, it is often necessary to use the C language to operate the file. This is particularly true in procedures such as reconciliation. In C language, there are as many as dozens of functions of file operation, familiar with common functions (such as fopen, Fclose, Fgets, fread, fwrite, etc.) can improve the programming efficiency of the project, and has very realistic significance.

Flexibility is one of the characteristics of good software to attract users, in order to reflect the flexibility of the software, you can put the relevant parameters in a configuration file, users can be set according to the needs. Therefore, the configuration file reading function in C language is also worthy of attention.

In this paper, the author of the Software as an example, the C language of the configuration file reading and the use of common file operation functions are described in detail.

2. Introduction to the software, file manipulation functions and configuration files used in this article

2.1 The software used in this article

The software used in this article is based on MFC implementation and has a user-operated interface, as shown in Figure 1.

Figure 1 Software Operation interface

In this software, the three main buttons implement the following functions:

(1) "Add to Output Box" button: Read the relevant information from the configuration file and output it to the blank box above (the blank box has the property of the listbox).

(2) "Save to File" button: Saves the information read from the configuration file to a file in the specified named format.

(3) "Upload to FTP" button: The file generated in (2) is uploaded to the specified FTP directory.

2.2 File action functions used in this article

The file manipulation functions used in this article include fopen, fwrite, Fflush, fclose, and so on, and they are described in detail as follows:

(1) fopen function

Function: Open File

Table header file: #include <stdio.h>

Definition function: FILE *fopen (const char *path, const char *mode);

Function Description: The parameter path string contains the file path and filename you want to open, and the parameter mode string represents the flow pattern.

Mode has the following morphological strings:

R: Open a read-only file, the file must exist.

r+: Open a read-write file that must exist.

W: Open Write-only file, if the file exists, the file length is 0, that is, the file content will disappear. If the file does not exist, the file is created.

w+: Open a writable file, if the file exists, the file length is zero, that is, the file content will disappear. If the file does not exist, the file is created.

A: Open a write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained.

A +: Open readable and writable files in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained.

The above morphological strings can be added to a B-character, such as RB, W+b or ab+, add a B-character to tell the function library to open the file as a binary file, not a plain text file. However, in the POSIX system, including Linux ignores the character. The new file created by fopen () will have a s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth (0666) permission, this file permission will also refer to the Umask value.

Return value: After the file is successfully opened, the file pointer to the stream is returned. If the file fails to open, it returns null and the error code exists in errno.

Additional note: Generally speaking, open the file will make some files read or write the action, if the file failed to open, the next reading and writing can not be smoothly, so in fopen () after the error judgment and processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.