In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to organize and build a multi-file C language program". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn how to organize and build a multi-file C language program.
Include project header file
The source code of the / * 01 project inclusion file * / located in part 2 is as follows:
/ * main.c-Meow Stream Codec * /... / * 01 Project contains file * / # include "main.h" # include "mmecode.h" # include "mmdecode.h"
# include is a C language preprocessing command that copies the contents of the file name to the current file. If the programmer uses double quotes (") around the header file name, the compiler will look for the file in the current directory. If the file is surrounded by angle brackets (), the compiler looks for the file in a set of predefined directories.
The main.h file contains the definitions and type definitions used in the main.c file. I like to put as many declarations in the header file as possible so that I can use these definitions elsewhere in my program.
The header files mmencode.h and mmdecode.h are almost the same, so I'll take mmencode.h as an example.
/ * mmencode.h-Meow stream codec * / # ifndef _ MMENCODE_H#define _ MMENCODE_H# include int mm_encode (FILE * src, FILE * dst); # endif / * _ MMENCODE_H * /
If the compiler finds more than one definition / prototype / declaration in a file, it will generate a warning. Therefore, these protective measures are necessary.
Inside these guards, there are only two things: the # include instruction and the function prototype declaration. I include the stdio.h header file here so that I can use the FILE definition in the function prototype. The function prototype can also be included in other C files to make it easier to create in the file's namespace. You can think of each file as a separate namespace in which variables and functions cannot be used by functions or variables in another file.
Don't forget to use protection.
The final realization of Meow coding
The function of the program is to encode and decode MeowMeow strings in bytes, which is in fact the simplest part of the project. What I've done so far is to support allowing this function to be called in the right place: parsing the command line, determining the action to use, and opening the file to be operated on. The following loop is the coding process:
/ * mmencode.c-Meow stream codec * /. While (! feof (src)) {if (! fgets (buf, sizeof (buf), src) break; for (iTun0; I > 4; fputs (tbl [hi], dst); fputs (tbl [lo], dst);}}
To put it simply, when there are blocks in the file (feof (3)), the loop reads a block in the file (feof (3)). Each byte of the read is then divided into two half-byte nibble of hi and lo. A half byte is half a byte, or 4 bits. The secret here is that 16 values can be encoded with 4 bits. I use hi and lo as indexes on 16 string lookup tables, tbl, that contain MeowMeow strings encoded in half bytes. These strings are written to the target FILE stream using the fputs (3) function, and then we move on to the next byte of the cache.
The table is initialized with macro definitions in table.h, and I like to use macros for initialization when there is no special reason (for example, to show a local header file containing another project). I will further explore the reasons in future articles.
The realization of Meow Decoding
I admit it took some time before I started work. The decoding loop is similar to encoding: reading the MeowMeow string into the buffer and converting the encoding from the string to bytes
/ * mmdecode.c-Meow stream codec * /. Int mm_decode (FILE * src, FILE * dst) {if (! src | |! dst) {errno = EINVAL; return-1;} return stupid_decode (src, dst);}
Doesn't that meet your expectations?
Here, I expose the details of the stupid_decode () function through the externally exposed mm_decode () function. When I say "external" above, I mean outside this file. Because the stupid_decode () function is not in the header file, it cannot be called in another file.
We sometimes do this when we want to publish a reliable public interface, but we haven't fully used functions to solve the problem. In this example, I wrote an I _ peg O-intensive function that reads eight bytes from the source at a time, then decodes it to get one byte and writes it to the target stream. A better implementation is to process buffers that are more than 8 bytes at a time. A better implementation can also output bytes through a buffer, thereby reducing the number of writes to a single byte in the target stream.
/ * mmdecode.c-Meow streaming Codec * /... int stupid_decode (FILE * src, FILE * dst) {char buf [9]; decoded_byte_t byte; int i; while (! feof (src)) {if (! fgets (buf, sizeof (buf), src)) break; byte.field.f0 = isupper (buf [0]) Byte.field.f1 = isupper (buf [1]); byte.field.f2 = isupper (buf [2]); byte.field.f3 = isupper (buf [3]); byte.field.f4 = isupper (buf [4]); byte.field.f5 = isupper (buf [5]); byte.field.f6 = isupper (buf [6]); byte.field.f7 = isupper (buf [7]); fputc (byte.value, dst);} return 0 }
Instead of using the displacement method used in the encoder, I created a custom data structure called decoded_byte_t.
/ * mmdecode.c-Meow stream codec * /. Typedef struct {unsigned char f7 unsigned char 1; unsigned char f5 unsigned char 1; unsigned char f4 unsigned char 1; unsigned char f3 unsigned char 1; unsigned char f2 decoded_byte_t 1; unsigned char f1 1; unsigned char f0 decoded_byte_t 1;} fields_t; typedef union {fields_t field; unsigned char value;} 1
You may feel a little complicated when you see the code for the first time, but don't give up. Decoded_byte_t is defined as a combination of fields_t and unsigned char. You can think of named members in a federation as aliases for the same memory region. In this case, value and field point to the same 8-bit memory area. Setting field.f0 to 1 will also set the least significant bit in the value.
While unsigned char is not mysterious, it may seem strange to fields_t 's type definition (typedef). Modern C compilers allow programmers to specify the value of a single bit field in a structure. The type of the field is an unsigned integer type, followed by a colon and an integer that specifies the length of the bit field.
This data structure makes it easy to access each bit in the byte by field name, and the combined value can be accessed through the value field in the union. We rely on the compiler to generate the correct shift instructions to access the fields, which can save you a lot of time when debugging.
Finally, because the stupid_decode () function reads only eight bytes at a time from the source FILE stream, it is not efficient. Usually we try to minimize the number of reads and writes to improve performance and reduce the overhead of calling system calls. Remember: a small number of read / write large blocks is much better than a large number of read / write small blocks.
Thank you for your reading. the above is the content of "how to organize and build a multi-file C language program". After the study of this article, I believe you have a deeper understanding of how to organize and build a multi-file C language program. Specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.