What happens when a file is opened in write mode which is in fact not existing?

The insertion and extraction operators [i.e. > are meant to be used by programs for writing to and reading from text files; it is assumed that the programmer is familiar with the differences between these two file formats.

In reality there are dozens of extensions with little documentation of ordinary text streams. An additional section will be added to this document at a later time.

Before any operation can take place on a file, it of course must be opened, and when you are finished with the file, it should be closed to avoid loss of data.

open member function also provides for a couple of optional arguments that are not often described. The most general prototype of this function is

  void open[const char *filename[, int mode][, int prot]];
The format that I've used indicates that the mode and prot arguments are optional.

The first argument is always the name of the file on the disk that the stream will be attached to. The  const modifier is included so that a programmer can write the name of the file [inside double quotes] in the function call. The only tricky part about using the open member function is under DOS based systems [includes Windows] in which directories are separated by a \; recall that the backslash character has a special meaning in C++ strings.

The prot parameter is used to specify the protection [permission] of the file under multiuser operating systems such as Unix. It allows you to specify which users are allowed to look at this file. Under DOS/Windows, this parameter is never used. The mode parameter is usually left out when dealing with text files, but there are some very useful situations under binary files for which this parameter must be set. There are a number of options that can be given for this parameter. If you need to specify more than one of them simply place a vertical bar between them.

  • ios::in This indicates that the stream will be used for input. This may seem redundant for ifstreams which are automatically marked for input when they are opened, but it has to be used occasionally. When you call open with no second parameter, the parameter is assumed to be ios::in but if you give any other parameter such as ios::binary you will need to specify that the file is an input file as well.
  • ios::out This indicates that the stream will be used for output. Like ios::in this may seem redundant for ofstreams but for the same reason as above, it usually has to be given.
  • ios::ate This causes the file pointer to point  at the  end of the file when the file is opened.
  • ios::trunc This causes the all the existing data in the file to be discarded [erased] when the file is opened. Be very careful not to use this option on a file you do not want destroyed!
  • ios::binary This causes the file to be accessed as a binary file. Most likely you will need to set this option. If you forget to set this option, many strange problems will occur when reading certain characters like `end of line' and `end of file'.
Example of opening a binary file:
int main[]
{
  ifstream infile;
  infile.open["hello.dat", ios::binary | ios::in];
// rest of program

}

Writing to a Binary File

I mentioned once that 

Bài Viết Liên Quan

Chủ Đề