Archive for November, 2008
How to redirect standard output and standard error to same file
Nov 16th
Sometimes while compiling C source code, the screen scrolls down if lot of warnings and errors are generated. This is uncomfortable as you can’t see all errors and warnings and you can’t attend to all warnings and warning at once and keep on recompiling. Instead you can use redirection facility supported by Bash shell to capture both messages printed on standard output and error to a same file. This method is more useful when you have huge build process to compile sources of your project.
Basically redirection of standard output is done as usual but the standard error is appended to More >
How to access files of a .iso file in Linux
Nov 16th
A file with .iso extension is typically a raw image that can be used to burn a CD or DVD. A file with .iso can be created using mkisofs command or by doing raw copy of a CD or DVD. In this article we will attempt to access the files present in an ISO file. First of all, we need to mount the ISO file like a partition in the hard drive. The Linux supports a cool feature of loop back mounting using which a file can be treated as partition on the hard drive and mounted. Such file can contain More >
Why macro can’t be used instead of typedef in C
Nov 14th
The C preprocessor blindly expands the macro with whatever they are defined with. Let us examine the following example.
#define CHARPTR char* CHARPTR p1,p2;
The C preprocessor expands the above code to the following.
char *p1,p2;
But we would like p2 to be a character pointer but not a character variable.
So macro are not generally used for variable type definitions as they are not suitable for all sorts of usages. Instead typedef is used as shown below.
typedef char* CHARPTR; CHARPTR p1,p2;
With the above code, the variables p1 and p2 will effectively become character pointers.
Why my netstat command is slow in Linux
Nov 14th
Basically default usage of netstat command makes netstat command to resolve all IP addresses to display symbolic host names. If your connection to DNS server is slow or not working, netstat command spends some time in waiting for response from the DNS server. However DNS resolution of each entry in the route table itself makes it slower.
To avoid this, you can use “-n” option which disables resolution of symbolic host names from numerical IP addresses and gives prompt response with numerical IP addresses.
[neo@techpulp ~]# netstat -nr
Similar problem is seen with “arp” command as well. Just use “-n” option in this case as well More >
Why my route command is slow in Linux
Nov 14th
Basically default usage of route command makes route command to resolve all IP addresses to display symbolic host names. If your connection to DNS server is slow or not working, route command spends some time in waiting for response from the DNS server.
To avoid this, you can use “-n” option which disables resolution of symbolic host names from numerical IP addresses and gives prompt response with numerical IP addresses.
[neo@techpulp ~]# route -n
How do I resolve compiler error with my multi-lined macro in C
Nov 14th
Let us look at the following plain multi-lined and multi-statement macro.
#define SAY_HELLO() \
printf("Hi, There!\n"); \
printf("Nice to meet you\n");
If you use the above macro as shown below, you will encounter compilation errors.
if(manager)
SAY_HELLO();
else
printf("You are not my manager..Why should I care?\n");
The above code expands to the following and results in compilation error as “else” part of the code is detached from the actual “if” condition.
if(manager)
printf("Hi, There!\n");
printf("Nice to meet you\n");
else
printf("You are not my manager..Why should I care?\n");
If you attempt to wrap all the statements of macro in {…}, general usage of the macro will throw More >
How to swap two integers without using third variable
Nov 13th
The safest way to swap two variables without using third variable is to use XOR operation as shown below.
This example C program swaps integer values of two variables i and j.
[neo@techpulp ~]# cat iswap.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 10, j = 20;
printf("Before swap: i = %d, j = %d\n", i, j);
i = i^j;
j = i^j;
i = i^j;
printf("After swap: i = %d, j = %d\n", i, j);
return 0;
}
[neo@techpulp ~]# gcc iswap.c -o iswap
[neo@techpulp ~]# ./iswap
Before swap: i = 10, j = 20
After swap: More > How to reset root password of my Fedora Linux system using GRUB
Nov 13th
There are multiple ways of recovering your Linux system if you have forgotten the password for root account. In this article I will discuss about how to do it using GRUB and single user mode.
GRUB provides great flexibility to recover a system by providing on the fly editing of boot options. The standard Fedora Linux systems support single user mode for which no log in is required. The following explains step by step procedure. Though this article takes Fedora system as an example, This procedure will work fine for any Linux system that has GRUB as boot loader and supports More >
How to resolve “Access denied for user” error with MySQL database connection
Nov 11th
Typically “Access denied for user” error occurs while connecting to MySQL Database because the privileges are not properly set at the server.
Let us examine the following real-time scenario where a user from the host 192.168.1.1 is trying to connect to MySQL server running on 192.168.1.6.
My PC MySQL Server 192.168.1.1 ---------- 192.168.1.6
Let us solve the problem if user encounters an error like the following while connecting to the server.
[neo@techpulp ~]# mysql -u root -p -h 192.168.1.6 Enter password: ERROR 1045 (28000): Access denied for user 'root'@'192.168.1.1' (using password: YES) [neo@techpulp ~]#
To solve this problem, enough privileges should be assigned for the More >


Recent Comments