In the C++ programming language, seekg is a function in the fstream
library (part of the standard library) that allows you to seek to an arbitrary position in a file. This function is defined for ifstream class - for ofstream class there's a similar function seekp (this is to avoid conflicts in case of classes that derive both istream and ostream, such as iostream).
istream&seekg(streamposposition);istream&seekg(streamoffoffset,ios_base::seekdirdir);
position
is the new position in the stream buffer. This parameter is an object of type streampos.offset
is an integer value of type streamoff representing the offset in the stream's buffer. It is relative to the dir
parameter.dir
is the seeking direction. It is an object of type ios_base::seekdir
that can take any of the following constant values:
ios_base::beg
(offset from the beginning of the stream's buffer).ios_base::cur
(offset from the current position in the stream's buffer).ios_base::end
(offset from the end of the stream's buffer).Note: If you have previously got an end of file on the stream, seekg
will not reset it but will return an error in many implementations. - use the clear()
method to clear the end of file bit first. This is a relatively common mistake and if seekg()
is not performing as expected, it is wise to clear the fail bit, as shown below.
#include<fstream>#include<iostream>intmain(){// Open a new file for input/output operations discarding any current// content in the file (assumes a length of zero on opening)std::fstreammyFile("test.txt",std::ios::in|std::ios::out|std::ios::trunc);// Add the characters "Hello World" to the filemyFile<<"Hello World";// Seek to 6 characters from the beginning of the filemyFile.seekg(6,std::ios::cur);// Read the next 5 characters from the file into a buffercharbuffer[6];myFile.read(buffer,5);// End the buffer with a null terminating characterbuffer[5]=0;// Output the contents read from the file and close it std::cout<<buffer<<std::endl;myFile.close();}
#include<fstream>#include<iostream>#include<string>intmain(){std::stringline;// Creates a new ifstream object and associates it with a file passed in via the parameters.// The object is then checked to see if the end-of-file has been reached, if the file has data// then this will be false.std::ifstreammyFile(argv[1],std::ios::in);std::cout<<myFile.eof()<<std::endl;// Continuously loops over each line of the file until the end of the filewhile(!myFile.eof()){std::getline(myFile,line);}// Again outputs the end-of-file status for the stream, this time the answer will be truestd::cout<<myFile.eof()<<std::endl;// Seeks to the very beginning of the file, clearing any fail bits first (such as the end-of-file bit)myFile.clear();myFile.seekg(0,std::ios::beg);// Outputs the status of the stream again, the result will be falsestd::cout<<myFile.eof()<<std::endl;myFile.close();}
In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern as well as to the Open-Closed Principle, by allowing the functionality of a class to be extended without being modified. Decorator use can be more efficient than subclassing, because an object's behavior can be augmented without defining an entirely new object.
Template metaprogramming (TMP) is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates can include compile-time constants, data structures, and complete functions. The use of templates can be thought of as compile-time polymorphism. The technique is used by a number of languages, the best-known being C++, but also Curl, D, Nim, and XL.
In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console, but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.
The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>. The functionality descends from a "portable I/O package" written by Mike Lesk at Bell Labs in the early 1970s, and officially became part of the Unix operating system in Version 7.
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments just as in a normal function call. Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed identifier or address.
In computer programming, run-time type information or run-time type identification (RTTI) is a feature of some programming languages that exposes information about an object's data type at runtime. Run-time type information may be available for all types or only to types that explicitly have it. Run-time type information is a specialization of a more general concept called type introspection.
In computing, a here document is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace in the text.
A class in C++ is a user-defined type or data structure declared with keyword class
that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. #line is used to determine the number of lines in given file.
sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.
The curiously recurring template pattern (CRTP) is an idiom, originally in C++, in which a class X
derives from a class template instantiation using X
itself as a template argument. More generally it is known as F-bound polymorphism, and it is a form of F-bounded quantification.
Substitution failure is not an error (SFINAE) is a principle in C++ where an invalid substitution of template parameters is not in itself an error. David Vandevoorde first introduced the acronym SFINAE to describe related programming techniques.
In the programming language C++, unordered associative containers are a group of class templates in the C++ Standard Library that implement hash table variants. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. The following containers are defined in the current revision of the C++ standard: unordered_set
, unordered_map
, unordered_multiset
, unordered_multimap
. Each of these containers differ only on constraints placed on their elements.
SymbolicC++ is a general purpose computer algebra system written in the programming language C++. It is free software released under the terms of the GNU General Public License. SymbolicC++ is used by including a C++ header file or by linking against a library.
In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects.
In the context of the programming language C++, functional
refers to a header file that is part of the C++ Standard Library and provides a set of predefined class templates for function objects, including operations for arithmetic, comparisons, and logic. Instances of these class templates are C++ classes that define a function call operator, and the instances of these classes can be called as if they were functions. It is possible to perform very sophisticated operations without writing a new function object, simply by combining predefined function objects and function object adaptors.
In computing, associative containers refer to a group of class templates in the standard library of the C++ programming language that implement ordered associative arrays. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. The following containers are defined in the current revision of the C++ standard: set
, map
, multiset
, multimap
. Each of these containers differ only on constraints placed on their elements.
In the C++ programming language, input/output library refers to a family of class templates and supporting functions in the C++ Standard Library that implement stream-based input/output capabilities. It is an object-oriented alternative to C's FILE-based streams from the C standard library.
Access modifiers are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.
Although C++ is one of the most widespread programming languages, many prominent software engineers criticize C++ for being overly complex and fundamentally flawed. Among the critics have been: Robert Pike, Joshua Bloch, Linus Torvalds, Donald Knuth, Richard Stallman, and Ken Thompson. C++ was widely adopted and implemented as a systems language through most of its existence, it has been used to build many pieces of very important software.
RE/flex is a free and open source computer program written in C++ that generates fast lexical analyzers in C++. RE/flex offers full Unicode support, indentation anchors, word boundaries, lazy quantifiers, and performance tuning options. RE/flex accepts Flex lexer specifications and offers options to generate scanners for Bison parsers. RE/flex includes a fast C++ regular expression library.