Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > C on Unix Code Examples

Output of one program is input of another program Using Pipes

/* Output of one program is input of another program Using Pipes */ #include <unistd.h> #include <process.h> /* Pipe the output of program to the input of another. */ int main() { int pipe_fds[2]; int stdin_save, stdout_save; if (pipe(pipe_fds) < 0) return -1; /* Duplicate stdin and stdout so we can restore them later. */ stdin_save = dup(STDIN_FILENO); stdout_save = dup(STDOUT_FILENO); /* Make the write end of the pipe stdout. */ dup2(pipe_fds[1], STDOUT_FILENO); /* Run the program. Its output will be written to the pipe. */ spawnl(P_WAIT, "/dev/env/DJDIR/bin/ls.exe", "ls.exe", NULL); /* Close the write end of the pipe. */ close(pipe_fds[1]); /* Restore stdout. */ dup2(stdout_save, STDOUT_FILENO); /* Make the read end of the pipe stdin. */ dup2(pipe_fds[0], STDIN_FILENO); /* Run another program. Its input will come from the output of the first program. */ spawnl(P_WAIT, "/dev/env/DJDIR/bin/less.exe", "less.exe", "-E", NULL); /* Close the read end of the pipe. */ close(pipe_fds[0]); /* Restore stdin. */ dup2(stdin_save, STDIN_FILENO); return 0; }

duplicate an open file descriptor. The dup() system call allocates a new file descriptor that refers to the same open file description as the descriptor oldfd. The new file descriptor number is guaranteed to be the lowest-numbered file descriptor that was unused in the calling process. After a successful return, the old and new file descriptors may be used interchangeably. Since the two file descriptors refer to the same open file description, they share file offset and file status flags; for example, if the file offset is modified by using lseek() on one of the file descriptors, the offset is also changed for the other file descriptor. The two file descriptors do not share file descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC;) for the duplicate descriptor is off. Upon successful completion a non-negative integer, namely the file descriptor, shall be returned; otherwise, -1 shall be returned and errno set to indicate the error.

Duplicate a file descriptor, specifying the new descriptor. The dup2() system call is similar to dup() but the basic difference between them is that instead of using the lowest-numbered unused file descriptor, it uses the descriptor number specified by the user. The dup2() system function is used to create a copy of an existing file descriptor. The dup2() function returns a descriptor with the value fildes2. The descriptor refers to the same file as fildes, and it will close the file that fildes2 was associated with. For more information about the processing which may occur when the file is closed, see close()--Close File or Socket Descriptor. If the original file descriptor was opened in text mode, data conversion is also done on the duplicated file descriptor.

Spawn a child process, given a list of arguments. spawnl() function loads and executes a new child process. The spawnl() function creates and executes a new child process, named in path with a NULL-terminated list of arguments in arg0 ... argn. This function calls spawnve(). If the new child process is a shell script, the first line must start with #!, followed by the path of the program to run to interpret the script, optionally followed by one argument. The script must also be marked as executable. For more information, see "The first line" in the Writing Shell Scripts chapter of the Neutrino User's Guide. The spawnl() function isn't a POSIX 1003.1 function, and isn't guaranteed to behave the same on all operating systems. It builds an argv[ ] array before calling spawn().

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement. When we say Output, it means to display some data on screen, printer, or in any file. C programming provides a set of built-in functions to output the data on the computer screen as well as to save it in text or binary files. C programming treats all the devices as files. So devices such as the display are addressed in the same way as files and the following three files are automatically opened when a program executes to provide access to the keyboard and screen.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

The pipe() function shall create a pipe and place two file descriptors, one each into the arguments fildes[0] and fildes[1], that refer to the open file descriptions for the read and write ends of the pipe. Their integer values shall be the two lowest available at the time of the pipe() call. The O_NONBLOCK and FD_CLOEXEC flags shall be clear on both file descriptors. (The fcntl() function can be used to set both these flags.) Data can be written to the file descriptor fildes[1] and read from the file descriptor fildes[0]. A read on the file descriptor fildes[0] shall access data written to the file descriptor fildes[1] on a first-in-first-out basis. It is unspecified whether fildes[0] is also open for writing and whether fildes[1] is also open for reading.

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

Closes a file descriptor, fildes. This frees the file descriptor to be returned by future open() calls and other calls that create file descriptors. The fildes argument must represent a hierarchical file system (HFS) file. When the last open file descriptor for a file is closed, the file itself is closed. If the file's link count is 0 at that time, its space is freed and the file becomes inaccessible. When the last open file descriptor for a pipe or FIFO file is closed, any data remaining in the pipe or FIFO file is discarded. close() unlocks (removes) all outstanding record locks that a process has on the associated file. Behavior for sockets: close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.

Open text file for reading, Create a text file for writing, Open binary file for reading, Create binary file for writing, Open text file for read/write, Create text file for read/write,

Read stoplist into binary tree, expects one entry per line. Split string into tokens, return token array. Make sure, copy string to a safe place. Always keep last entry null terminated.

Firstly Declare two file pointers for two files. Open 2 files in read mode. Now Inside while loop read both files character by character. Check both characters whether they're equal

This c language program takes any year as input and prints its last two digits. Take any year as input. Divide the input by 100 and obtain its remainder. The remainder got is

Using cricket, declare an array player with 10 elements and write C program code read the information about all the 10 players and print a team wise list containing Names of Players

Write a C program to find maximum between three numbers using Ladder If Else or nested if. We will continue our discussion and we will write code to find maximum between three...




C input length and width of a rectangle and calculate perimeter of the rectangle. How to find perimeter of a rectangle in C language. Input length and width of the rectangle using

C program to compare two strings using loop character by character. Logic to compare two strings. Input two strings from user and Store it in some variable say str1 and str2. Compare

C programming code example for Number Conversions: Binary to decimal, decimal to binary, decimal to octal, decimal to hex, octal to decimal, octal to binary, binary to octal,