C++ provides the following classes to perform output and input of characters to/from files:
2.Restore english.nlg file from the Recycle Bin. If your english.nlg file is missing or not found and you think it may be mistakenly deleted by yourself, the easiest way to get it back is to restore it from the Recycle Bin. What if you have emptied the Recycle Bin? You may try to recover english.nlg file with a recovery program.
ofstream
: Stream class to write on filesifstream
: Stream class to read from filesfstream
: Stream class to both read and write from/to files.
These classes are derived directly or indirectly from the classes
istream
and ostream
. We have already used objects whose types were these classes: cin
is an object of class istream
and cout
is an object of class ostream
. Therefore, we have already been using classes that are related to our file streams. And in fact, we can use our file streams the same way we are already used to use cin
and cout
, with the only difference that we have to associate these streams with physical files. Let's see an example:This code creates a file called
example.txt
and inserts a sentence into it in the same way we are used to do with cout
, but using the file stream myfile
instead.But let's go step by step:
Open a file
The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to open a file. An open file is represented within a program by a stream (i.e., an object of one of these classes; in the previous example, this wasmyfile
) and any input or output operation performed on this stream object will be applied to the physical file associated to it.In order to open a file with a stream object we use its member function
open
:open (filename, mode);
Where
filename
is a string representing the name of the file to be opened, and mode
is an optional parameter with a combination of the following flags:ios::in | Open for input operations. |
ios::out | Open for output operations. |
ios::binary | Open in binary mode. |
ios::ate | Set the initial position at the end of the file. If this flag is not set, the initial position is the beginning of the file. |
ios::app | All output operations are performed at the end of the file, appending the content to the current content of the file. |
ios::trunc | If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one. |
All these flags can be combined using the bitwise operator OR (
|
). For example, if we want to open the file example.bin
in binary mode to add data we could do it by the following call to member function open
:Each of the
open
member functions of classes ofstream
, ifstream
and fstream
has a default mode that is used if the file is opened without a second argument:class | default mode parameter |
---|---|
ofstream | ios::out |
ifstream | ios::in |
fstream | ios::in | ios::out |
For
ifstream
and ofstream
classes, ios::in
and ios::out
are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the open
member function (the flags are combined).For
fstream
, the default value is only applied if the function is called without specifying any value for the mode parameter. If the function is called with any value in that parameter the default mode is overridden, not combined.File streams opened in binary mode perform input and output operations independently of any format considerations. Non-binary files are known as text files, and some translations may occur due to formatting of some special characters (like newline and carriage return characters).
Since the first task that is performed on a file stream is generally to open a file, these three classes include a constructor that automatically calls the
open
member function and has the exact same parameters as this member. Therefore, we could also have declared the previous myfile
object and conduct the same opening operation in our previous example by writing:Combining object construction and stream opening in a single statement. Both forms to open a file are valid and equivalent.
To check if a file stream was successful opening a file, you can do it by calling to member
is_open
. This member function returns a bool
value of true
in the case that indeed the stream object is associated with an open file, or false
otherwise:Closing a file
When we are finished with our input and output operations on a file we shall close it so that the operating system is notified and its resources become available again. For that, we call the stream's member functionclose
. This member function takes flushes the associated buffers and closes the file:Once this member function is called, the stream object can be re-used to open another file, and the file is available again to be opened by other processes.
In case that an object is destroyed while still associated with an open file, the destructor automatically calls the member function
close
.Text files
Text file streams are those where theios::binary
flag is not included in their opening mode. These files are designed to store text and thus all values that are input or output from/to them can suffer some formatting transformations, which do not necessarily correspond to their literal binary value.Writing operations on text files are performed in the same way we operated with
cout
:Reading from a file can also be performed in the same way that we did with
cin
:This last example reads a text file and prints out its content on the screen. We have created a while loop that reads the file line by line, using getline. The value returned by getline is a reference to the stream object itself, which when evaluated as a boolean expression (as in this while-loop) is
true
if the stream is ready for more operations, and false
if either the end of the file has been reached or if some other error occurred.Checking state flags
The following member functions exist to check for specific states of a stream (all of them return abool
value): bad()
- Returns
true
if a reading or writing operation fails. For example, in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left. fail()
- Returns
true
in the same cases asbad()
, but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number. eof()
- Returns
true
if a file open for reading has reached the end. good()
- It is the most generic state flag: it returns
false
in the same cases in which calling any of the previous functions would returntrue
. Note thatgood
andbad
are not exact opposites (good
checks more state flags at once).
The member function
clear()
can be used to reset the state flags.get and put stream positioning
All i/o streams objects keep internally -at least- one internal position:ifstream
, like istream
Could Not Open Language File English.lng Dev C 2017
, keeps an internal get position with the location of the element to be read in the next input operation.ofstream
, like ostream
, keeps an internal put position with the location where the next element has to be written.Finally,
fstream
, keeps both, the get and the put position, like iostream
.These internal stream positions point to the locations within the stream where the next reading or writing operation is performed. These positions can be observed and modified using the following member functions:
tellg() and tellp()
These two member functions with no parameters return a value of the member typestreampos
, which is a type representing the current get position (in the case of tellg
) or the put position (in the case of tellp
).seekg() and seekp()
These functions allow to change the location of the get and put positions. Both functions are overloaded with two different prototypes. The first form is:seekg ( position );
seekp ( position );
Using this prototype, the stream pointer is changed to the absolute position
position
(counting from the beginning of the file). The type for this parameter is streampos
, which is the same type as returned by functions Could Not Open Language File English.lng Dev C 4
tellg
and tellp
.The other form for these functions is:
seekg ( offset, direction );
seekp ( offset, direction );
Using this prototype, the get or put position is set to an offset value relative to some specific point determined by the parameter
direction
. offset
is of type streamoff
. And direction
is of type seekdir
, which is an enumerated type that determines the point from where offset is counted from, and that can take any of the following values:ios::beg | offset counted from the beginning of the stream |
ios::cur | offset counted from the current position |
ios::end | offset counted from the end of the stream |
The following example uses the member functions we have just seen to obtain the size of a file:
Notice the type we have used for variables
begin
and end
:streampos
is a specific type used for buffer and file positioning and is the type returned by file.tellg()
. Values of this type can safely be subtracted from other values of the same type, and can also be converted to an integer type large enough to contain the size of the file.These stream positioning functions use two particular types:
streampos
and streamoff
. These types are also defined as member types of the stream class:Type | Member type | Description |
---|---|---|
streampos | ios::pos_type | Defined as fpos<mbstate_t> .It can be converted to/from streamoff and can be added or subtracted values of these types. |
streamoff | ios::off_type | It is an alias of one of the fundamental integral types (such as int or long long ). |
Each of the member types above is an alias of its non-member equivalent (they are the exact same type). It does not matter which one is used. The member types are more generic, because they are the same on all stream objects (even on streams using exotic types of characters), but the non-member types are widely used in existing code for historical reasons.
Binary files
For binary files, reading and writing data with the extraction and insertion operators (<<
and >>
) and functions like getline
is not efficient, since we do not need to format any data and data is likely not formatted in lines.File streams include two member functions specifically designed to read and write binary data sequentially:
write
and read
. The first one (write
) is a member function of ostream
(inherited by ofstream
). And read
is a member function of istream
(inherited by ifstream
). Objects of class fstream
have both. Their prototypes are:write ( memory_block, size );
read ( memory_block, size );
Where
memory_block
is of type char*
(pointer to char
), and represents the address of an array of bytes where the read data elements are stored or from where the data elements to be written are taken. The size
parameter is an integer value that specifies the number of characters to be read or written from/to the memory block.In this example, the entire file is read and stored in a memory block. Let's examine how this is done:
First, the file is open with the
ios::ate
flag, which means that the get pointer will be positioned at the end of the file. This way, when we call to member tellg()
, we will directly obtain the size of the file.Once we have obtained the size of the file, we request the allocation of a memory block large enough to hold the entire file:
Right after that, we proceed to set the get position at the beginning of the file (remember that we opened the file with this pointer at the end), then we read the entire file, and finally close it:
At this point we could operate with the data obtained from the file. But our program simply announces that the content of the file is in memory and then finishes.
Buffers and Synchronization
When we operate with file streams, these are associated to an internal buffer object of typestreambuf
. This buffer object may represent a memory block that acts as an intermediary between the stream and the physical file. For example, with an ofstream
, each time the member function put
(which writes a single character) is called, the character may be inserted in this intermediate buffer instead of being written directly to the physical file with which the stream is associated.The operating system may also define other layers of buffering for reading and writing to files.
When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream). This process is called synchronization and takes place under any of the following circumstances:
- When the file is closed: before closing a file, all buffers that have not yet been flushed are synchronized and all pending data is written or read to the physical medium.
- When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized.
- Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are:
flush
andendl
. - Explicitly, with member function sync(): Calling the stream's member function
sync()
causes an immediate synchronization. This function returns anint
value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns0
.
Previous: Preprocessor directives | Index |
The articles in this section of the documentation explain a subset of the error messages that are generated by the Microsoft C/C++ compiler.
Important
The Visual Studio compilers and build tools can report many kinds of errors and warnings. After an error or warning is found, the build tools may make assumptions about code intent and attempt to continue, so that more issues can be reported at the same time. If the tools make the wrong assumption, later errors or warnings may not apply to your project. When you correct issues in your project, always start with the first error or warning that's reported, and rebuild often. One fix may make many subsequent errors go away.
To get help on a particular diagnostic message in Visual Studio, select it in the Output window and press the F1 key. Visual Studio opens the documentation page for that error, if one exists. You can also use the search tool above to find articles about specific errors or warnings. Or, browse the list of errors and warnings by tool and type in the navigation pane on this page.
Note
Not every Visual Studio error or warning is documented. In many cases, the diagnostic message provides all of the information that's available. If you landed on this page when you used F1 and you think the error or warning message needs additional explanation, let us know. You can use the feedback buttons on this page to raise a documentation issue on GitHub, or a product issue on the Developer Community site. You can also send feedback and enter bugs within the IDE. In Visual Studio, go to the menu bar and choose Help > Send Feedback > Report a Problem, or submit a suggestion by using Help > Send Feedback > Send a Suggestion.
You may find additional assistance for errors and warnings in Microsoft's public forums. Or, search for the error or warning number on the Visual Studio C++ Developer Community site. You can also search for errors and warnings and ask questions on Stack Overflow to find solutions.
For links to additional help and community resources, see Visual C++ Help and Community.
Error messages
Error | Message |
---|---|
Fatal error C999 | UNKNOWN MESSAGE Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more information |
Fatal error C1001 | An internal error has occurred in the compiler. (compiler file 'file', line number) To work around this problem, try simplifying or changing the program near the locations listed above. Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more information |
Fatal error C1002 | compiler is out of heap space in pass 2 |
Fatal error C1003 | error count exceeds number; stopping compilation |
Fatal error C1004 | unexpected end-of-file found |
Fatal error C1005 | string too big for buffer |
Fatal error C1007 | unrecognized flag 'string' in 'option' |
Fatal error C1008 | no input file specified |
Fatal error C1009 | compiler limit: macros nested too deeply |
Fatal error C1010 | unexpected end of file while looking for precompiled header. Did you forget to add '#include <file>' to your source? |
Fatal error C1012 | unmatched parenthesis: missing 'character' |
Fatal error C1013 | compiler limit: too many open parentheses |
Fatal error C1014 | too many include files: depth = number |
Fatal error C1016 | #ifdef/#ifndef expected an identifier |
Fatal error C1017 | invalid integer constant expression |
Fatal error C1018 | unexpected #elif |
Fatal error C1019 | unexpected #else |
Fatal error C1020 | unexpected #endif |
Fatal error C1021 | invalid preprocessor command 'string' |
Fatal error C1022 | expected #endif |
Fatal error C1023 | 'file': unexpected error with pch, try rebuilding the pch |
Fatal error C1026 | parser stack overflow, program too complex |
Fatal error C1033 | cannot open program database 'file' |
Fatal error C1034 | file: no include path set |
Fatal error C1035 | expression too complex; simplify expression |
Fatal error C1036 | cannot overwrite earlier program database format, delete 'file' and recompile |
Fatal error C1037 | cannot open object file 'file' |
Fatal error C1038 | compiler limit: 'function': control flow state too complex; simplify function |
Fatal error C1041 | cannot open program database 'file'; if multiple CL.EXE write to the same .PDB file, please use /FS |
Fatal error C1045 | compiler limit: linkage specifications nested too deeply |
Fatal error C1046 | compiler limit: structure nested too deeply |
Fatal error C1047 | The object or library file 'file' was created with an older compiler than other objects; rebuild old objects and libraries |
Fatal error C1048 | unknown option 'string' in 'option' |
Fatal error C1049 | invalid numerical argument 'value' |
Fatal error C1051 | program database file, 'file', has an obsolete format, delete it and recompile |
Fatal error C1052 | program database file, 'filename', was generated by the linker with /DEBUG:fastlink; compiler cannot update such PDB files; please delete it or use /Fd to specify a different PDB filename |
Fatal error C1053 | 'function': function too large |
Fatal error C1054 | compiler limit: initializers nested too deeply |
Fatal error C1055 | compiler limit: out of keys |
Fatal error C1057 | unexpected end of file in macro expansion |
Fatal error C1060 | compiler is out of heap space |
Fatal error C1061 | compiler limit: blocks nested too deeply |
Fatal error C1063 | compiler limit: compiler stack overflow |
Fatal error C1064 | compiler limit: token overflowed internal buffer |
Fatal error C1065 | compiler limit: out of tags |
Fatal error C1067 | compiler limit: 64K limit on size of a type record has been exceeded |
Fatal error C1068 | cannot open file 'file' |
Fatal error C1069 | cannot read compiler command line |
Fatal error C1070 | mismatched #if/#endif pair in file 'file' |
Fatal error C1071 | unexpected end of file found in comment |
Fatal error C1073 | Internal error involving incremental compilation(compiler file 'file', line number) |
Fatal error C1074 | 'IDB' is illegal extension for PDB file: file |
Fatal error C1075 | the left token was unmatched at the end of the file |
Fatal error C1076 | compiler limit: internal heap limit reached; use /Zm to specify a higher limit |
Fatal error C1077 | compiler limit: cannot have more than number command line options |
Fatal error C1079 | compiler limit: PCH file size limit exceeded |
Fatal error C1080 | compiler limit: command line option exceeded limit of number characters |
Fatal error C1081 | 'file': file name too long |
Fatal error C1082 | cannot close type file: 'file': message |
Fatal error C1083 | cannot open type file: 'file': message |
Fatal error C1084 | cannot read type file: 'file': message |
Fatal error C1085 | cannot write type file: 'file': message |
Fatal error C1086 | cannot seek type file: 'file': message |
Fatal error C1087 | cannot tell type file: 'file': message |
Fatal error C1088 | cannot flush type file: 'file': message |
Fatal error C1089 | cannot truncate type file: 'file': message |
Fatal error C1090 | PDB API call failed, error code 'code': 'message' |
Fatal error C1091 | compiler limit: string exceeds number bytes in length |
Fatal error C1092 | Edit and Continue does not support changes to data types; build required |
Fatal error C1093 | API call 'function' failed 'HRESULT' : 'description' |
Fatal error C1094 | '-Zmnumber': command line option is inconsistent with value used to build precompiled header ('-Zmnumber') |
Fatal error C1098 | Version mismatch with Edit and Continue engine |
Fatal error C1099 | Edit and Continue engine terminating compile |
Fatal error C1100 | unable to initialize OLE: error |
Fatal error C1101 | cannot create handler for attribute 'identifier' |
Fatal error C1102 | unable to initialize: error |
Fatal error C1103 | fatal error importing progid: 'message' |
Fatal error C1104 | fatal error importing libid: 'message' |
Fatal error C1105 | message: error |
Fatal error C1107 | could not find assembly 'assembly': please specify the assembly search path using /AI or by setting the LIBPATH environment variable |
Fatal error C1108 | unable to find DLL: 'file' |
Fatal error C1109 | unable to find 'symbol' in DLL 'file' |
Fatal error C1110 | too many nested template/generic definitions |
Fatal error C1111 | too many template/generic parameters |
Fatal error C1112 | compiler limit: 'number ' too many macro arguments, only number allowed |
Fatal error C1113 | #using failed on 'file' |
Fatal error C1114 | 'file': WinRT does not support #using of a managed assembly |
Fatal error C1120 | call to GetProcAddress failed for 'function' |
Fatal error C1121 | call to CryptoAPI failed |
Fatal error C1126 | automatic allocation exceeds size |
Fatal error C1128 | number of sections exceeded object file format limit: compile with /bigobj |
Fatal error C1189 | #error: message |
Fatal error C1190 | managed targeted code requires a '/clr' option |
Fatal error C1191 | 'file' can only be imported at global scope |
Fatal error C1192 | #using failed on 'file' |
Fatal error C1193 | an error expected in file(line) not reached |
Fatal error C1195 | use of /Yu and /Yc on the same command line is incompatible with the /clr option |
Fatal error C1196 | 'identifier' : identifier found in type library 'typelib' is not a valid C++ identifier |
Fatal error C1197 | cannot reference 'file' as the program has already referenced 'file' |
Fatal error C1201 | unable to continue after syntax error in class template definition |
Fatal error C1202 | recursive type or function dependency context too complex |
Fatal error C1205 | Generics are not supported by the version of the runtime installed |
Fatal error C1206 | Per-appdomain data is not supported by the version of the runtime installed |
Fatal error C1207 | Managed templates not supported by the version of the runtime installed |
Fatal error C1208 | Allocating reference classes on the stack is not supported by the version of the runtime installed |
Fatal error C1209 | Friend assemblies not supported by the version of the runtime installed |
Fatal error C1210 | /clr:pure and /clr:safe are not supported by the version of the runtime installed |
Fatal error C1211 | The TypeForwardedTo Custom Attribute is not supported by the version of the runtime installed |
Fatal error C1300 | error accessing program database file (message) |
Fatal error C1301 | error accessing program database file, invalid format, please delete and rebuild |
Fatal error C1302 | no profile data for module 'module' in profile database 'file' |
Fatal error C1305 | profile database 'file' is for a different architecture |
Fatal error C1306 | last change to profile data base 'file' was not optimization analysis; optimization decisions may be out of date |
Fatal error C1307 | program has been edited since profile data was collected |
Fatal error C1308 | file: linking assemblies is not supported |
Fatal error C1309 | Mismatched versions of C2.DLL and pgodbver.DLL |
Fatal error C1310 | profile guided optimizations are not available with OpenMP |
Fatal error C1311 | COFF format cannot statically initialize 'symbol' with number byte(s) of an address |
Fatal error C1312 | Too many conditional branches in function. Simplify or refactor source code. |
Fatal error C1313 | compiler limit: type blocks may not be nested deeper than number levels |
Fatal error C1350 | error loading dll 'file': dll not found |
Fatal error C1351 | error loading dll 'file': incompatible version |
Fatal error C1352 | Invalid or corrupt MSIL in function 'function' from module 'module' |
Fatal error C1353 | metadata operation failed: runtime not installed or version mismatch |
Fatal error C1382 | the PCH file 'file' has been rebuilt since 'obj' was generated. Please rebuild this object |
Fatal error C1383 | compiler option /GL is incompatible with the installed version of common language runtime |
Fatal error C1384 | Incorrect setting for PGO_PATH_TRANSLATION when linking 'file' |
Fatal error C1451 | Failed to generate debug information when compiling the call graph for the concurrency::parallel_for_each at: 'callsite' |
Fatal error C1505 | unrecoverable parser look-ahead error |
Fatal error C1506 | unrecoverable block scoping error |
Fatal error C1508 | compiler limit: 'function': more than 65535 argument bytes |
Fatal error C1509 | compiler limit: too many exception handler states in function 'function'; simplify function |
Fatal error C1510 | Cannot open language resource clui.dll |
Fatal error C1601 | unsupported inline assembly opcode |
Fatal error C1602 | unsupported intrinsic |
Fatal error C1603 | inline assembly branch target out of range by number bytes |
Fatal error C1852 | 'file' is not a valid precompiled header file |
Fatal error C1853 | 'file' precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa) |
Fatal error C1854 | cannot overwrite information formed during creation of the precompiled header in object file: 'file' |
Fatal error C1900 | Il mismatch between 'tool' version 'number' and 'tool' version 'number' |
Fatal error C1901 | Internal memory management error |
Fatal error C1902 | Program database manager mismatch; please check your installation |
Fatal error C1903 | unable to recover from previous error(s); stopping compilation |
Fatal error C1904 | bad provider interaction: 'file' |
Fatal error C1905 | Front end and back end not compatible (must target same processor). |