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++ > File Operations Code Examples

C++ Code to List Files in Current Directory

/* C++ Code to List Files in Current Directory To list and display/print all the files present inside the current directory in C++ programming, use a pointer say dir of DIR and the pointer say pdir of dirent to open and read directory to print all the files present in the directory as shown here in the following program. Following C++ program ask to the user to press any key to list all the files present inside the current directory. Here all the files display along with serial number like (1=FirstFileName.extension, 2=SecondFileName.extension, 3=ThirdFileName.extension, etc) and it will be separated by comma: C++ Program - List Files in Current Directory */ #include<iostream.h> #include<conio.h> #include<dirent.h> void main() { clrscr(); int done, i=1; DIR *dir; dirent *pdir; cout<<"Press any key to list and view all the files in the current directory : \n"; getch(); dir=opendir("."); while(pdir=readdir(dir)) { cout<<i<<"="pdir->d_name<<", "; i++; } closedir(dir); getch(); }

The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.

The pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a value. In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type. You should see a pointer as a symbolic representation of a memory address. With pointers, programs can simulate call-by-reference. They can also create and manipulate dynamic data structures. In C++, a pointer variable refers to a variable pointing to a specific address in a memory pointed by another variable.

Read a directory entry. Returns a pointer to a dirent structure describing the next directory entry in the directory stream associated with dir. A call to readdir() overwrites data produced by a previous call to readdir() or __readdir2() on the same directory stream. Calls for different directory streams do not overwrite each other's data. Each call to readdir() updates the st_atime (access time) field for the directory. A dirent structure contains the character pointer d_name, which points to a string that gives the name of a file in the directory. This string ends in a terminating NULL, and has a maximum of NAME_MAX characters. Save the data from readdir(), if required, before calling closedir(), because closedir() frees the data.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

open a directory file. The opendir() function is used in conjunction with the functions readdir() and closedir() to obtain the list of file names contained in the directory specified by dirname. The path indicated by dirname can be either relative to the current working directory, or it can be an absolute path name. The file <dirent.h> contains definitions for the structure dirent and the DIR type. In QNX the dirent structure contains a stat structure in the d_stat member. To speed up applications, which often want both the name and the stat data, a resource manager may return the stat information at the same time the readdir() function is called.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

#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.

In C++ programming we are using the iostream standard library, it provides cin and cout methods for reading from input and writing to output respectively. To read and write from a file we are using the standard C++ library called fstream. Let us see the data types define in fstream library is: • ofstream: This data type represents the output file stream and is used to create files and to write information to files. • ifstream: This data type represents the input file stream and is used to read information from files. • fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

close a directory. The closedir() function closes the directory stream associated with dirp. A successful call to closedir() also closes the underlying file descriptor associated with dirp. The directory stream descriptor dirp is not available after this call. The closedir() function returns 0 on success. On error, -1 is returned, and errno is set appropriately. The result of using a directory stream after calling one of the exec*() or spawn*() family of functions is undefined. After a call to the fork() function, either the parent or the child (but not both) may continue processing the directory stream using the readdir() and rewinddir() functions. If both the parent and child processes use these functions, the result is undefined. Either or both processes may call the closedir() function.