CS201 Quiz 1

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Basic file code with getline

#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string text; ifstream in("input.txt"); if (in.fail()) { cout << "File tasted bad!!!\n"; exit(1); } while (getline(in, text)) { cout << text << endl; cout << "The first letter of text is " << text[0] << endl; } }

Basic code taking in a file

#include <iostream> #include <fstream> using namespace std; int main() { string text; ifstream in("file-loop.cc"); if (in.fail()) { cout << "File tasted bad\n"; exit(1); } do { in >> text; cout << text; cout << "The first letter of text is " << text[0]; } while (!in.eof()); }

modes of what the files can be opened

1 ios::app Append mode. All output to that file to be appended to the end. 2 ios::ate Open a file for output and move the read/write control to the end of the file. 3 ios::in Open a file for reading. 4 ios::out Open a file for writing. 5 ios::trunc If the file already exists, its contents will be truncated before opening the file.

Opening a file

A file must be opened before you can read from it or write to it. Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only. Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.

File position pointers

Both istream and ostream provide member functions for repositioning the file-position pointer. These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream. The argument to seekg and seekp normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream. The file-position pointer is an integer value that specifies the location in the file as a number of bytes from the file's starting location.

non-parameterized manipulators

Examples are endl, fixed, showpoint and flush. • endl - Gives a new line • ends - Adds null character to close an output string • flush - Flushes the buffer stream • ws - Omits the leading white spaces present before the first field • hex, oct, dec - Displays the number in hexadecimal or octal or in decimal format

parameterized manipulators

Manipulator -> Meaning setw (int n) -> To set field width to n setprecision (int p) -> The precision is fixed to p setfill (Char f) -> To set the character to be filled setiosflags (long l) -> Format flag is set to l resetiosflags (long l) -> Removes the flags indicated by l Setbase(int b) -> To set the base of the number to b

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. needs to include fstream and iostream

ifstream

This data type represents the input file stream and is used to read information from files. needs to include fstream and iostream

ofstream

This data type represents the output file stream and is used to create files and to write information to files. needs to include fstream and iostream

Closing a file

When a C++ program terminates it automatically flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination. Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects. void close();

writing to a file

While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object.

reading from a file

You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.

manipulators without arguments

endl: It is defined in ostream. It is used to enter a new line and after entering a new line it flushes (i.e. it forces all the output written on the screen or in the file) the output stream. ws: It is defined in istream and is used to ignore the whitespaces in the string sequence. ends: It is also defined in ostream and it inserts a null character into the output stream. It typically works with std::ostrstream, when the associated output buffer needs to be null-terminated to be processed as a C string. flush: It is also defined in ostream and it flushes the output stream, i.e. it forces all the output written on the screen or in the file. Without flush, the output would be the same, but may not appear in real-time

manipulators

helping functions that can modify the input/output stream. It does not mean that we change the value of a variable, it only modifies the I/O stream using insertion (<<) and extraction (>>) operators. Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Manipulators are operators that are used to format the data display. To access manipulators, the file iomanip.h should be included in the program.

ifstream is a

input file stream, a class (a type).

types of manipulators

manipulators without arguments manipulators with arguments parameterized non-parameterized

manipulators with arguments

must be included in headers Some important manipulators in <iomanip> are: setw (val): It is used to set the field width in output operations. setfill (c): It is used to fill the character 'c' on output stream. setprecision (val): It sets val as the new value for the precision of floating-point values. setbase(val): It is used to set the numeric base value for numeric values. setiosflags(flag): It is used to set the format flags specified by parameter mask. resetiosflags(m): It is used to reset the format flags specified by parameter mask. Some important manipulators in <ios> are: showpos: It forces to show a positive sign on positive numbers. noshowpos: It forces not to write a positive sign on positive numbers. showbase: It indicates the numeric base of numeric values. uppercase: It forces uppercase letters for numeric values. nouppercase: It forces lowercase letters for numeric values. fixed: It uses decimal notation for floating-point values. scientific: It uses scientific floating-point notation. hex: Read and write hexadecimal values for integers and it works same as the setbase(16). dec: Read and write decimal values for integers i.e. setbase(10). oct: Read and write octal values for integers i.e. setbase(10). left: It adjusts output to the left. right: It adjusts output to the right.

ofstream is a

output file stream, a class (a type).


Set pelajaran terkait

Life Skills-Reproduction Unit Test

View Set

3- Unit 8 Trading Securities Practice

View Set

Business Law and Ethics Test 3 Chapter 14

View Set

Q&A Chapter 16 Nursing Management During the Postpartum Period

View Set