Q

Emertxe-News & Blog

Home » Blog » Split command : Organize your C files better
Split command : Organize your C files better

Often when we walk into car garage, one can observe that the mechanic having different type of spanners, screw drivers for fixing different set of problems. These are nothing but tools that makes life of the mechanic easier. In the similar lines knowing different tools (ex: commands) and using it for their advantage can make life of a developer much easier. In this post let us have a look into split command.

 

Let us take example of the given file (Fig1, split_file.c), which has the obvious main function and two other functions i.e. function1 and function2. As the size of the file grows, file becomes bigger in size and the development may want to move some of the function implementation into other files and eventually compile them using make utility.

Fig1: split_file.c

One way to split function1 and function2 into two different file is by using the split command. Here is a simple way to understand the split command:

                                                split –b <file> <targetfile>
                                                split –l  <file> <targetfile>
where
                                                file: Input file which you need to get it splitted
                                               targetfile: Instance of output where it needs to be spitted

After executing the split command the command itself will split the output into targetfileaa, targetfileab etc, depending on the type of split. The “-b” or “-l” option talks about if the split to be based on bytes of lines of the file.

 

In our case since the main, function1 and function2 are having 10 lines each, we can apply a split based on the split as follows (Fig2: Applying split command)

 
Fig2: Applying split command

After the command you can do ls –al and see there are split_outputaa, split_outputab, split_outputac etc,which will have main, function1 and function2 split into it. Let us see the file contents by executing the cat (Fig3: Cat command for viewing contents) command as follows.

Fig3: Output of split command

In this way larger files can be split into smaller set of files, which come in handy during development. Such tools also help a developer to keep source files compact and split across, thereby helping in maintaining and enhancing them without much problem. For example if you have 5000+ lines of code in a single file, its high time to start dividing them into multiple times. Its time to start splitting! 

YOU MAY ALSO LIKE

Our Training Programs for Freshers

Our Training Programs for Freshers

Introduction: Emertxe is the leading training institute in Bangalore for Embedded Systems and IoT domains. It is a pioneer in training freshers since 2003 by providing excellent placement opportunities for freshers. Over the years 70000+ students have made their...

Difference between typedef and macro in C

Difference between typedef and macro in C

Introduction In any programming language, there are a set of keywords available, which can’t be used as variable name. It is because keywords are reserved and they have a special meaning. Keywords which are also called as reserved names can be anything like a command...

Emertxe and ESSCI launch Virtual Internships for Engineering students

Emertxe and ESSCI launch Virtual Internships for Engineering students

Emertxe, India’s premier embedded systems engineering finishing school, has provided more than 650+ students placements in 2019-20 alone. During 2020, amidst challenging pandemic times it continues to support students with placement opportunities and new programs via...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *