What happens when a file is opened in write mode which is in fact not existing?
The insertion and extraction operators (i.e. << and >> 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. Show
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.
int main() { ifstream infile; infile.open("hello.dat", ios::binary | ios::in); // rest of program } Writing to a Binary FileI mentioned once that << is used to write data to a text file. If you had a variable x that contained the value 354 and you used the statment outfile << x; this would cause the character 3, the character 5, and the character 4 to be written (in ASCII form) to the file. This is not binary form which would only require 16-bits. The ofstream class provides a member function named write that allows for information to be written in binary form to the stream. The prototype of the write function isostream& write(void *buffer, streamsize n);This function causes n bytes to be written from the memory location given by the buffer to the disk and moves the file pointer ahead n bytes. The parameters types require a little bit of explanation. Even though the return type is ofstream& the return value is usually ignored by most programers. The buffer pointer is of type void this allows for any type of variable to be used as the first parameter. You should not be writing functions with void parameters, this is a very tricky part of programming. The type streamsize is simply a positive integer. It is rare that you will know exactly how many bytes a particular variable is. To obtain this information, C++ provides a macro (its like a function) named sizeof that takes exactly one parameter and returns the size of the parameter in terms of bytes required for storage. Below is an example of using the sizeof macro to obtain the size of a variable and writing the contents of a variable to disk. Notice the use of a structure rather than a class; you should not use this method for writing classes to binary files! See the section entitled Writing Classes to Files for a description of how this should be done. struct Person { char name[50]; int age; char phone[24]; }; int main() { Person me = {"Robert", 28, "364-2534"}; Person book[30]; int x = 123; double fx = 34.54; ofstream outfile; outfile.open("junk.dat", ios::binary | ios::out); outfile.write(&x, sizeof(int)); // sizeof can take a type outfile.write(&fx, sizeof(fx)); // or it can take a variable name outfile.write(&me, sizeof(me)); outfile.write(book, 30*sizeof(Person)) outfile.close(); } Reading from a Binary FileReading data from a binary file is just like writing it except that the function is now called read instead of write When reading data from a file there are a couple of new things to watch out for:
{ int x; ifstream infile; infile.open("silly.dat", ios::binary | ios::in) infile.read(&x, 7); // reads 7 bytes into a cell that is either 2 or 4 } File PointerThe tellp() member function has a prototype of the form streampos tellp(); This function accepts no parameters, but returns the location given in bytes from the beginning of the file where the file pointer is currently sitting. The next read or write will take place from this location. The seekp() member function has a prototype of the form void seekp(streampos location, int relative); This causes the file
pointer to move to another location within the file. The location specifies the number of bytes that will be used to determine the location and the relative parameter indicates whether this is some sort of absolute or relative positioning request. Possible values for relative are:
int main() Writing Classes to Binary FilesThe easiest way to store records in files is to use astruct If you are keeping track of records in memory structures using classes, then saving these classes to disk takes a little extra work. You cannot simply use a write member function and give the address of the object as the buffer. The reason for this is the presence of member functions. It would not make sense to save the member functions; these member functions end up getting saved as memory locations which would cause your computer to crash if you ever loaded one from disk with an old memory location. It is possible to write objects to disk but it requires that the object have a member function associated with it.My usual approach is to insert a member function named read and write in each member function. These functions should take an fstream as a parameter as the stream to save itself to. Your program should then open the stream and call the member function with the appropriate stream. The member function should then go through each data field of the object writing them out in a particular order. The read member function must retrieve the information from the disk in exactly the same order. The example for this section is a little involved, so I've eliminated the non-file member functions. \begin{verbatim} class Student main() void Student::save(ofstream& of) void Student::load(ifstream& inf) What Went Wrong?In this section, I will point out a couple of methods of determining if a file operation was successful and if not, a couple of methods of determing roughly what went wrong. After every disk operation, a well written program will call the member function fail() to see if the operation completed successfully. It is up to the programmer to determine what should occur when a file operation goes bad. Essentially there are three possibilities:
ANSI Errors
main()
DOS Extended Errors
What happens when a file opened for writing does not exist?Opening a file for writing always creates a new file if it does not already exist. There is no option to change that behavior.
What happens when we open a file in writing mode if the file already exists?To open a file in write mode, “w” is specified. When mode “w” is specified, it creates an empty file for output operations. What if the file already exists? If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
When a file is opened for writing what happens if the file doesn't already exist Python?If the file does not already exist in the folder, a new one gets created. Write and Read ('w+'): This mode opens the file for both reading and writing. The text is overwritten and deleted from an existing file. The start of the file is where the handle is located.
What will happen when a file is opened for writing if it is a already existing B file does not exist?Open a text file for writing. If the file already exists, its contents are destroyed. Open a text file in append mode for writing at the end of the file. The fopen function creates the file if it does not exist.
|