Sunday, December 27, 2009

How would you get the character positions 10-20 from a text file?

How would you get the character positions 10-20 from a text file?

cut -c10-20

Or

substring ${string_variable_name:starting_position:length}

Or

cat filename.txt | cut -c 10-20

How to read arguments in shell program

How to read arguments in shell program

#!/bin/sh
for i in $*
do
echo $i
done

$# returns the number of parameters that are passed to a shell script
$? returns the exit code of the last executed command (0 : Successful, 1 or other: Failed)




On executig the above script with any number of command-line arguments it will display all the parameters.

fseek C/C++

fseek

int fseek ( FILE * stream, long int offset, int origin );

header:

Sets the position indicator associated with the stream to a new position defined by adding offset to a reference position specified by origin.
The End-of-File internal indicator of the stream is cleared after a call to this function, and all effects from previous calls to ungetc are dropped.
When using fseek on text files with offset values other than zero or values retrieved with ftell, bear in mind that on some platforms some format transformations occur with text files which can lead to unexpected repositioning.
On streams open for update (read+write), a call to fseek allows to switch between reading and writing.

stream:Pointer to a FILE object that identifies the stream.
offset:Number of bytes to offset from origin.
origin:Position from where offset is added. It is specified by one of the following constants defined in :
SEEK_SET Beginning of file
SEEK_CUR Current position of the file pointer
SEEK_END End of file

Return Value:
If successful, the function returns a zero value.
Otherwise, it returns nonzero value.

/* fseek example */
#include

int main ()
{
FILE * pFile;
pFile = fopen ( "example.txt" , "w" );
fputs ( "This is an apple." , pFile );
fseek ( pFile , 9 , SEEK_SET );
fputs ( " sam" , pFile );
fclose ( pFile );
return 0;
}


After this code is successfully executed, the file example.txt contains:
This is a sample.

fopen

fopen

FILE * fopen ( const char * filename, const char * mode );

Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE object whose pointer is returned. The operations that are allowed on the stream and how these are performed are defined by the mode parameter.

filename
C string containing the name of the file to be opened. This paramenter must follow the file name specifications of the running environment and can include a path if the system supports it.

mode
C string containing a file access modes. It can be:
"r" Open a file for reading. The file must exist.
"w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+" Open a file for update both reading and writing. The file must exist.
"w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
Additional characters may follow the sequence, although they should have no effect. For example, "t" is sometimes appended to make explicit the file is a text file.
In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability.

Return Value

If the file has been succesfully opened the function will return a pointer to a FILE object that is used to identify the stream on all further operations involving it. Otherwise, a null pointer is returned.

/* fopen example */
#include
int main ()
{
FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile); //this is same as puts(str);
//fputs() - Writes the string to a file as we can see from
//mode as "w"
fclose (pFile);
}
return 0;
}


/* fopen example */
#include
int main ()
{
FILE * pFile;
pFile = fopen ("myfile.txt","r");
if (pFile!=NULL)
{
fgets ("fopen example",pFile); //this is same as puts(str);
//fgets() - Reads the string from a file as we can see from
//mode as "r"
fclose (pFile);
}
return 0;
}

size_t in C/C++

size_t

size_t corresponds to the integral data type returned by the language operator sizeof and is defined in the header file (among others) as an unsigned integral type.

This is an unsigned integer type used to represent the sizes of objects. The result of the sizeof operator is of this type, and functions such as malloc and memcpy accept arguments of this type to specify object sizes.

It is also used as the return type for strcspn, strlen, strspn and strxfrm to return sizes and lengths.

Here is a sample problem, which will help us understand it better:
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning

warning C4018: '<' : signed/unsigned mismatch

strlen returns a number of type 'size_t'. size_t is an unsigned type and
you are comparing it to an int, a signed type.

Two solutions to remove the warning:

1. Change the type of the variable 'i' to 'size_t'.
2. staic_cast i to "unsigned" type.

Which way is better in C++?

Change 'i' to type 'size_t'.

Or better yet, replace your char array 'pathcmd'
with a 'std::string' object, and write:

std::string pathcmd("whatever");
for(std::string::size_type i = 0; i < pathcmd.size(); ++i)
/***end of example*****/


Careful--->
assigning size_t type variable to int works fine.
Only if the value in the size_t variable can be represented by int. Otherwise you've invoked undefined behavior. The only way this assignment could be safe is if size_t is smaller than int, which isn't a common case.


in printf size_t has format specifier as %z ( courtesy Amitesh Singh, thanks :)

Thursday, December 24, 2009

Funny "C"- Weird Pointers

You have a declaration:

int* ptr_a, ptr_b;

Given this, what is the type of ptr_b? int *, right?

Wrong!

C’s declaration syntax ignores the pointer asterisks when carrying a type over to multiple declarations. If you split the declaration of ptr_a and ptr_b into multiple statements, you get this:

int *ptr_a;
int ptr_b;

It’s possible to do the single-line declaration in a clear way. This is the immediate improvement:

int *ptr_a, ptr_b;

Notice that the asterisk has moved. It is now right next to the word ptr_a. A subtle implication of association.

Finally, I should point out that you can do this just fine:

int *ptr_a, *ptr_b;

Call by Value and Call by Reference

The arguments passed to function can be of two types namely

1. Values passed
2. Address passed

The first type refers to call by value and the second type refers to call by reference.

For instance consider program1

main()
{
int x=50, y=70;
interchange(x,y);
printf(“x=%d y=%d”,x,y);
}

interchange(x1,y1)
int x1,y1;
{
int z1;
z1=x1;
x1=y1;
y1=z1;
printf(“x1=%d y1=%d”,x1,y1);
}

Here the value to function interchange is passed by value.

Consider program2

main()
{
int x=50, y=70;
interchange(&x,&y);
printf(“x=%d y=%d”,x,y);
}

interchange(x1,y1)
int *x1,*y1;
{
int z1;
z1=*x1;
*x1=*y1;
*y1=z1;
printf(“*x=%d *y=%d”,x1,y1);
}

Here the function is called by reference. In other words address is passed by using symbol & and the value is accessed by using symbol *.

The main difference between them can be seen by analyzing the output of program1 and program2.

The output of program1 that is call by value is

x1=70 y1=50
x=50 y=70

But the output of program2 that is call by reference is

*x=70 *y=50
x=70 y=50

This is because in case of call by value the value is passed to function named as interchange and there the value got interchanged and got printed as

x1=70 y1=50

and again since no values are returned back and therefore original values of x and y as in main function namely

x=50 y=70 got printed.

But in case of call by reference address of the variable got passed and therefore what ever changes that happened in function interchange got reflected in the address location and therefore the got reflected in original function call in main also without explicit return value. So value got printed as
*x=70 *y=50 and x=70 y=50

Call By Value/Call By Reference is not new to C++. It has been around for a while. In C, we call/pass by value by passing variables, and we call/pass by reference by passing either pointers, or addresses of variables. See example below.
Eg:
main()
{
int myInt, *pMyIntPtr;
myInt = 10;
pMyIntPtr = &myInt; // assigning address of myInt to pMyIntPtr

MyCbyVal (myInt); // Call by value, or pass by value
MyCbyRef (pMyIntPtr); // Call/pass by reference (passing a pointer)
MyCbyRef (&myInt); // Call/pass by reference (passing address of myInt)
return myInt;
}

Any changes made to the parameter by the MyCbyVal function cannot be seen in the main function above. However, any changes made to the parameters of MyCbyRef (both statements) will be reflected in MyInt of main function, after the statements are executed.

P.S. In call by value the values may change in the calling function but those are not reflected in the main function but in the call by reference the calling function causes a change in the value of the arguments passed in the main function.

Wednesday, December 23, 2009

printf C++

Here's a quick summary of the available print format specifiers:

%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign
%p print the pointer address

Controlling printf integer width
The "%3d" specifier means a minimum width of three spaces, which, by default, will be right-justified. (Note: the alignment is not currently being displayed properly here.)

printf("%3d", 0); 0
printf("%3d", 123456789); 123456789
printf("%3d", -10); -10
printf("%3d", -123456789); -123456789

Left-justifying printf integer output
To left-justify those previous printf examples, just add a minus sign (-) after the % symbol, like this:

printf("%-3d", 0); 0
printf("%-3d", 123456789); 123456789
printf("%-3d", -10); -10
printf("%-3d", -123456789); -123456789

The printf zero-fill option
To zero-fill your integer output, just add a zero (0) after the % symbol, like this:

printf("%03d", 0); 000
printf("%03d", 1); 001
printf("%03d", 123456789); 123456789
printf("%03d", -10); -10
printf("%03d", -123456789); -123456789

printf - integers with formatting
Here is a collection of examples for integer printing. Several different options are shown, including a minimum width specification, left-justified, zero-filled, and also a plus sign for positive numbers.

Description Code Result
At least five wide printf("'%5d'", 10); ' 10'
At least five-wide, left-justified printf("'%-5d'", 10); '10 '
At least five-wide, zero-filled printf("'%05d'", 10); '00010'
At least five-wide, with a plus sign printf("'%+5d'", 10); ' +10'
Five-wide, plus sign, left-justified printf("'%-+5d'", 10); '+10 '

printf - floating point numbers
Here are several examples showing how to print floating-point numbers.

Description Code Result
Print one position after the decimal printf("'%.1f'", 10.3456); '10.3'
Two positions after the decimal printf("'%.2f'", 10.3456); '10.35'

Eight-wide, two positions after the decimal
printf("'%8.2f'", 10.3456); ' 10.35'

Eight-wide, four positions after the decimal
printf("'%8.4f'", 10.3456); ' 10.3456'

Eight-wide, two positions after the decimal, zero-filled
printf("'%08.2f'", 10.3456); '00010.35'

Eight-wide, two positions after the decimal, left-justified
printf("'%-8.2f'", 10.3456); '10.35 '
Printing a much larger number with that same format
printf("'%-8.2f'", 101234567.3456); '101234567.35'

How to print strings with printf formatting
Here are several printf formatting examples that show how to format string output with printf format specifiers.

Description Code Result
A simple string printf("'%s'", "Hello"); 'Hello'
A string with a minimum length printf("'%10s'", "Hello"); ' Hello'
Minimum length, left-justified printf("'%-10s'", "Hello"); 'Hello '

rand() and srand() in C/C++

RAND:

C++ header
C header

int rand ( void );
Generate random number

Returns a pseudo-random integral number in the range 0 to RAND_MAX.

This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using srand.

RAND_MAX is a constant defined in . Its default value may vary between implementations but it is granted to be at least 32767.

A typical way to generate pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range:

( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014

Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans.

#include
#include
#include

int main ()
{
int iSecret, iGuess;

/* initialize random seed: */
srand ( time(NULL) );

/* generate secret number: */
iSecret = rand() % 10 + 1;

do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&iGuess);
if (iSecret else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);

puts ("Congratulations!");
return 0;
}

Output:

Guess the number (1 to 10): 5
The secret number is higher
Guess the number (1 to 10): 8
The secret number is lower
Guess the number (1 to 10): 7
Congratulations!

In this example, the random seed is initialized to a value representing the second in which the program is executed (time is defined in the header ). This way to initialize the seed is generally a good enough option for most randoming needs.


SRAND:

void srand ( unsigned int seed );
Initialize random number generator

The pseudo-random number generator is initialized using the argument passed as seed.

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.

In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header ) is different each second, which is distinctive enough for most randoming needs.

#include
#include
#include

int main ()
{
printf ("First number: %d\n", rand() % 100);
srand ( time(NULL) );
printf ("Random number: %d\n", rand() % 100);
srand ( 1 );
printf ("Again the first number: %d\n", rand() %100);

return 0;
}

Output:

First number: 41
Random number: 13
Again the first number: 41


random number, random number generator


Also, check out Free ISTQB Training Material here

www.testing4success.com - Application QA/Testing     Mobile App QA/Testing     Web Testing     Training
Iphone App QA  Android App QA  Web QA 

sizeof Operator- C/C++

sizeof Operator
The sizeof operator yields the size of its operand with respect to the size of type char.

Grammar

unary-expression:
sizeof unary-expression
sizeof ( type-name )
The result of the sizeof operator is of type size_t, an integral type defined in the include file STDDEF.H. This operator allows you to avoid specifying machine-dependent data sizes in your programs.

The operand to sizeof can be one of the following:

A type name. To use sizeof with a type name, the name must be enclosed in parentheses.
An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated.
When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof. For example:



#include

size_t getPtrSize( char *ptr )
{
return sizeof( ptr );
}

using namespace std;
int main()
{
char szHello[] = "Hello, world!";

cout << "The size of a char is: "
<< sizeof( char )
<< "\nThe length of " << szHello << " is: "
<< sizeof szHello
<< "\nThe size of the pointer is "
<< getPtrSize( szHello ) << endl;
}
Output

The size of a char is: 1
The length of Hello, world! is: 14
The size of the pointer is 4


When the sizeof operator is applied to a class, struct, or union type, the result is the number of bytes in an object of that type, plus any padding added to align members on word boundaries. The result does not necessarily correspond to the size calculated by adding the storage requirements of the individual members. The /Zp compiler option and the pack pragma affect alignment boundaries for members.

The sizeof operator never yields 0, even for an empty class.

The sizeof operator cannot be used with the following operands:

Functions. (However, sizeof can be applied to pointers to functions.)
Bit fields.
Undefined classes.
The type void.
Dynamically allocated arrays.
External arrays.
Incomplete types.
Parenthesized names of incomplete types.
When the sizeof operator is applied to a reference, the result is the same as if sizeof had been applied to the object itself.

If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.

The sizeof operator is often used to calculate the number of elements in an array using an expression of the form:

sizeof array / sizeof array[0]

Static Variable in C++

Static Variable in C++:

You have to define the static data member outside the class declaration.

When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.

When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function.

When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. A static data member must be defined at file scope. An integral data member that you declare as const static can have an initializer.

When you declare a member function in a class declaration, the static keyword specifies that the function is shared by all instances of the class. A static member function cannot access an instance member because the function does not have an implicit this pointer. To access an instance member, declare the function with a parameter that is an instance pointer or reference.



//statictest.h

class

statictest

{

public:

static int a;

void sum();

};

///////////////////////////////////////////////

//statictest.cpp

#include "statictest.h"

int statictest::a; // Defining outside the class. By default value of a=0.

void statictest::sum(){

a=0;

}

///////////////////////////////////////////////

//main.cpp

#include "statictest.h"

int main()

{

statictest test;

test.sum();

}

///////////////////////////////////////////////


#include

using namespace std;
class CMyClass {
public:
static int m_i;
};

int CMyClass::m_i = 0;
CMyClass myObject1;
CMyClass myObject2;

int main() {
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;

myObject1.m_i = 1;
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;

myObject2.m_i = 2;
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;

CMyClass::m_i = 3;
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;
}


Output--
0
0
1
1
2
2
3
3

Static Variable in C

STATIC VARIABLE in C:

Static Variable as Local Variable in a function:

Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and
values are reinitialized. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.

Had it been global variable, it would have been available outside the scope of the function, but static variable is not available outside the scope of a function (helpful in localizing errors - as it can't be changed outside the function scope).

Static Variable as Global Variable in a File:
The Scope of the variable is limited to the file only. It cannot be used outside the file. It also retains it's value throughout the life.
It works in a same way as namespace in C++, as the same variable with the same name can be used in another file and yet will be different from the static variable in the first file.

extern in C/C++

How exactly to use extern in C and C++ ?

The extern keyword is used to tell the compiler that a data object is declared in a different *.cpp or *.c file (code unit). Its required for data objects but optional for function declarations. For example, you have two *.cpp files named A.cpp and B.cpp. B.cpp has a global int that needs to be used in A.cpp.

// A.cpp
#include
// other includes here
...
extern int hours; // this is declared globally in B.cpp

int foo()
{
hours = 1;
}



// B.cpp
#include
// other includes here
...
int hours; // here we declare the object WITHOUT extern
extern void foo(); // extern is optional on this line

int main()
{
foo();
}

Data Types in C/C++

Type--Size in Bits--Range

char--8-- -127 to +127
unsigned char--8-- 0 to 255
signed char--8-- -127 to +127
int-- 16 or 32 -- -32767 to 32767
unsigned int-- 16 or 32-- 0 to 65535
signed int-- 16 or 32-- -32767 to 32767
short int-- 16-- -32767 to 32767
unsigned short int-- 16 -- 0 to 65535
signed short int -- 16 -- -32767 to 32767
long int & signed long int-- 32 -- -2147438647 to +2147438647
unsigned long int -- 32 -- 0 to 4294967295
float -- 32 -- Six digits of precision
double -- 64 -- Ten digits of precision
long double -- 80 -- Ten digits of precision

P.S. All the data types are application in C++ as well will additional bool and wchar_t.

Sunday, January 18, 2009

Function Pointer and its relation to function parameters

Its a very basic fact but an important one so mentioning it over here will help for sure.

  • A pointer to a function must have the same type as the function. Attempts to take the address of a function by reference without specifying the type of the function will produce an error.
  • The type of a function is not affected by arguments with default values.
  • The following example shows that default arguments are not considered part of a function's type. The default argument allows you to call a function without specifying all of the arguments, it does not allow you to create a pointer to the function that does not specify the types of all the arguments. Function f can be called without an explicit argument, but the pointer badpointer cannot be defined without specifying the type of the argument:

int f(int = 0);
void g()
{
int a = f(1); // ok
int b = f(); // ok, default argument used
}
int (*pointer)(int) = &f; // ok, type of f() specified (int)
int (*badpointer)() = &f; // error, badpointer and f have
// different types. badpointer must
// be initialized with a pointer to
// a function taking no arguments.

So, it means the argument should be passed of same type as argument as it was done to function.