Saturday 30 January 2016

Remove comments from file

0

Remove comments from a file. Store the new file without comments. Here I am assuming the file-name which has comments that is the source file is present in c drive with name src.cpp. While the file with the resultant content is named as res.cpp and will be created in c drive. 
This question needs basic knowledge of file handling.
C language has two types of comments.

 

1. Single line comment: 

This type of  comment starts with double slash (//) and it ends with enter character.

// This is a comment

2. Multi-line comment: 

This type of comment is used to multiple lines. It uses pair of slash and star to start and end a block of comment. 

/* This
is multi-line
comment */

Here goes the code:


#include <stdio.h>
#define INM 1       /* Reading multi-line comment */
#define INS 2       /* Reading single-line comment */
#define OUT 0       /* Not in comment */

int main() {
   int mode, i, l, j, k;
   FILE *src, *res; // File pointers

   /* File handling stuff: fopen is used to open a file */
   src = fopen("C:\\src.cpp", "rt");
   /* rt defines that opened file in reading mode */
   res = fopen("C:\\res.cpp", "wt");
   /* wt defines that opened file is in writing mode */
   
   mode = OUT;
   for (i = 1;;i++){
     
      if ((l = getc(src)) == EOF) {
         printf("File ended");
         break;
      }
      /* In comment section */     
      if (mode == OUT && l == '/') {
         if ((j = getc(src)) == EOF) {
            printf("File ended");
         }
         else if (j == '*') {
            mode = INM;
            /* entering into multi-line comment */
         }
         else if(j == '/'){
             mode = INS;
             /* entering into single-line comment */
         }
      }
      /* code to be written into new file */
      else if (mode == OUT) {
         putc(l, res);
      }
 
      /* Leaving comment */
      if (mode == INM && l == '*') {
         if ((j = getc(src)) == '/') {
            mode = OUT;
            /*end of multi-line comment*/
         }
      }
      if( mode == INS && l == '\n'){
          mode = OUT;
          /*end of single line comment*/
      }
   }
   
   /*close the file pointers*/
   fclose(src);
   fclose(res);
   return 0;
}


Results:

Contents of src.cpp

Don't remove this
// Single line comment : Remove this
/* Multi-line comment:
Remove this*/
Don't remove this!!


Contents of res.cpp

Don't remove this

Don't remove this!!


Thanks for reading!!

Tuesday 26 January 2016

Cloning and migration of virtual machine

1


Here we are talking about the cloning of the virtual machine it can be either ubuntu or windows. Suppose you have worked on it, but now you need a exact copy of the machine on a different system. In that case, you need to first clone the machine and then migrate it to other machine.

Some definitions:
1. Cloning: Cloning means making an exact similar copy.
2. Migration: Moving virtual machine from one computer to other.

Requirements:
1. Virtual box installed
2. Pen drive ( >4 GB) or any hard drive to transfer file
3. Virtual machine which you want to migrate

Steps:
1. Turn off the virtual machine. You cannot put it to pause. Power it off.

2. Cloning:
A. Select the machine from the left pane which you want to clone. Click snapshots which is present near upper right side.(see below)
B. Click on the sheep button.
C. A wizard will pop up.
It asks for name of clone machine. By default, Virtual box appends word clone to name of original machine which is being cloned. You can give any other name. but give different name ffrom original machine. Click next.

D. Type of clone:
You can make two types of clones:
1. Full clone: In this mode you can work with machine without any need of source Virtual machine. As we are migrating it from one system to other, we will make a full clone.
2. Linked clone:  In this mode new differencing disk images are created where the parent disk images are the source disk images. If you selected the current state of the source Virtual Machine as clone point, a new snapshot will be created implicitly.
Select full clone and then click clone.

E. Cloning will start. It took around 2-3 minutes. Then a machine is created. You can see it in the left pane.

To check whether your machine is working or not. Run it. ( It's to ensure that clone is working properly. It is an optional step)
3. Locate the .vdi file of the clone machine. you can find it in VirtualBox Vm folder if you are on windows.
Copy this file in pen drive and then onto new host system.
4.  Create new virtual machine as if you are creating a normal machine, until you see this step to choose the disk.
In this step, choose the option to use an existing virtual hard disk. Select the .vdi file. Rest steps are same as if you creating a standard virtual machine. A new machine is present in the left pane now. Run it.Migrated machine is working.



Thanks for reading!!

 

Saturday 23 January 2016

Virtual Box and Ubuntu

1



What is virtualization?

As the name suggests, it is the way to create a virtual instance of any object. In computer science, virtualization can be describe as to make a virtual version of server, storage or operating system. Virtualization of the operating system can be achieved by using virtual box or virtual machine softwares which are freely available on Internet. In this article we will see how to install Oracle Virtual Box on Windows 8 host machine. And after that we will install Ubuntu 14 in virtual box.
So lets get started:

Software required:
1. To download Oracle virtual box:
Choose according to your system.
2. To download Ubuntu:
Ubuntu is freely available. Choose according to your system.
I downloaded 32 bit despite the fact that I have the 64 bit system. 64 bit system can run both 32 or 64 bit version. No problem in either case.
Steps:

A. INSTALLING VIRTUAL BOX:

1. Open the downloaded exe file of virtual box which is of around 115 MB. Click on it. Click yes.
2. Click on install.
Then it will start installing.
Then you see this :
Allow it. Click install. It will start installing again.
 3. And it’s done!


B. UBUNTU INSTALLATION IN VIRTUAL BOX

1. Open virtual box:
 It looks like this. Click on NEW blue button on the upper left side.
2. Virtual machine name:

Give name to your virtual machine. I gave Ubuntu 14.04.3. Virtual box will automatically fill the other two options. If you add names like myVirtual or any customized name then you need to specify the other fields. Click on next.
3. Memory size:
How much memory do you want to give this machine?
It is RAM not secondary memory. I added 768 MB which was default. You can set according to your system. If your system has 2 GB or less then reduce it 512 MB or if you have more than 2 like 4,6 or 8 GB, then leave it as 768 MB.
 4.  Hard Disk: Choose Create a virtual hard disk now (which is default setting). It creates a virtual hard disk on your machine.
5. Hard disk file type: Choose default setting of Virtual disk image. Click next.

6. Storage on Physical hard disk: As you work on the virtual machine, you may create files and use storage. So what you need is to access space to store them. This space could be allocated once or you may say that it should do automatically when required. When memory assignment is done dynamically it can grow its size whenever machine needs. It’s good to opt for dynamically allocated option.

7. File location and size: It’s the size you want to give your machine. (I gave 15 GB)
 Then you see a machine named Ubuntu or whatever you gave in left pane. Select this machine. Click on the settings.

8.  Select system tab. Uncheck floppy. Set hard disk first and optical disk second by using arrows.
9. Select processor tab: If you 4 cores then set it to 1 or more than 4 then set it to 2.Change execution cap to 90%.
10. Switch to display tab. Change the video memory to half of the available or you can set it to 64 MB.
 
11.  Click on 3d acceleration check box.
 
12.  Switch to storage tab. Click on empty. Now select the cd icon which is present on the right side. Browse the location where you placed iso image of Ubuntu.

13. Click in OK. All settings are done. Click on the start button to start your newly built virtual machine. Virtual Machine will start booting process. It takes time. If you encounter error like ACPI PCC probe failed, do not worry. It is harmless error.
 
14. Now you see the ubuntu os asking you to install or try it. Click on try if you want to try it otherwise install button.
15.  Click on erase disk.
16. Select your time zone.

17. Select keyboard layout language:
 
18. Installation will take time (half an hour or so).
  19. Done!! 
 
Thanks for reading. I hope you get, how to do it.