Short Answers for File Handling and STL in C++
1. Write a short note on adapted containers
Adapted containers are wrappers around existing STL containers to provide restricted interfaces.
Examples:
- stack (adapts deque/list/vector)
- queue (adapts deque/list)
- priority_queue (adapts vector)
2. Describe the difference between manipulators and ios functions
- Manipulators: Format output using syntax like cout << setw(10).
- ios functions: Member functions like setf(), unsetf() to set formatting flags.
3. List the various types of I/O manipulators with example
- setw(n): Set width
- setprecision(n): Set precision
- setfill(ch): Fill empty space
- endl: Newline
Example:
cout << setw(10) << setfill('*') << 123 << endl;
4. What is the need for iterators? What is the role in STL.
Iterators provide a uniform way to access elements of containers.
Role: Help traverse and manipulate container data.
Example: vector<int>::iterator it;
5. Explain classes for the file stream operations
- ifstream: for input
- ofstream: for output
- fstream: for input/output
Derived from istream, ostream, or both.
6. What is difference between IO using put(), get() and IO using getline() and write().
Short Answers for File Handling and STL in C++
- put(char): Outputs one character
- get(): Reads one character
- getline(): Reads a whole line
- write(): Outputs binary data
7. Define Standard Template Library.
STL is a set of C++ template classes and functions for data structures and algorithms.
Includes:
- Containers (vector, list)
- Algorithms (sort, find)
- Iterators
8. How may ways a file can be opened? List them.
File open modes:
- ios::in
- ios::out
- ios::app
- ios::binary
- ios::ate
- ios::trunc
9. List the parameters of open() function.
Syntax: file.open("filename", mode);
Parameters:
- filename: name of the file
- mode: ios::in, ios::out, etc.
10. What are the functions for manipulation of file pointer?
- seekg(): move get pointer
- seekp(): move put pointer
- tellg(): get get pointer position
Short Answers for File Handling and STL in C++
- tellp(): get put pointer position