Programming CC++ by conte's editor

Programming CC++ by conte's editor

Author:conte's editor [conte's editor]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2020-01-18T16:00:00+00:00


int main()

{

ofstream out("test");

if(!out) {

cout << "Cannot open file.\n";

return 1;

}

// output data

out << 10 << " " << 123.23 << "\n"; out << "This is a short text file.\n"; out.close();

// now, read it back char ch;

int i;

float f;

char str[80];

ifstream in("test");

if(!in) {

cout << "Cannot open file.\n"; return 1;

}

in >> i; in >> f; in >> ch; in >> str;

cout << "Here is the data: ";

cout << i << " " << f << " " << ch << "\n"; cout << str;

in.close();

return 0; }

When reading text files using the >> operator, keep in mind that certain character translations occur. For example, whitespace characters are omitted. If you want to prevent any character translations, you must open the file for binary I/O and use the binary I/O functions.

Including ios::app causes all output to that file to be appended to the end. This value can only be used with files capable of output. Including ios::ate causes a seek to the end of the file to occur when the file is opened. Although ios::ate causes a seek to the end of file, I/O operations can still occur anywhere within the file.

The ios::binary value causes the file to be opened for binary I/O operations. By default, files are opened in text mode.

The ios::in value specifies that the file is capable of input. The ios::out value specifies that the file is capable of output. However, creating a stream using ifstream implies input, and creating a stream using ofstream implies output, so in these cases it is unnecessary to supply these values.

The ios::trunc value causes the contents of a preexisting file by the same name to be destroyed and the file is truncated to zero length.

Including ios::nocreate causes the open( ) function to fail if the file does not already exist. The ios::noreplace value causes the open( ) function to fail if the file already exists and ios::ate or ios::app is not also specified.

The value of access determines how the file can be accessed. Its default value is filebuf::openprot (filebuf is a base class of the file classes), which means a normal file. Check your compiler’s documentation for other legal values of access .

When opening a file, both mode and access will default. When opening an input file, mode will default to ios::in . When opening an output file, mode will default to ios::out. In either case, the default for access is a normal file. For example, this opens a file called “test” for output:

out.open("test"); // defaults to output and normal file

To open a stream for input and output, you must usually specify both the ios::in and the ios::out mode values, as shown here:

mystream.open("test", ios::in | ios::out);

For many compilers, no default value for mode is supplied when opening read/write files.

In all cases, if open( ) fails, the stream will be zero. Therefore, before using a file, you should test to make sure that the open operation succeeded.

Related functions are close( ) , fstream( ) , ifstream( ) , and ofstream( ) .

peek

#include <iostream.h>int peek();

The peek( ) function is a member of istream .



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.