Before we can write to a file, we must open it. What this really means is that we must tell the system that we want to write to a file and what the filename is. We do this with the "fopen" function illustrated in the first line of the program. The file pointer, "fp" in our case, points to the file and two arguments are required in the parentheses, the filename first, followed by the file type. The filename is any valid 1616/OS filename, and can be expressed in upper or lower case letters, or even mixed if you so desire. It is enclosed in double quotes. For this example we have chosen the name TENLINES.TXT. This file should not exist on your disk at this time.
If you have a file with this name, you should change its name or move it because when we execute this program, its contents will be erased. If you don’t have a file by this name, that is good because we will create one and put some data into it. READING ("r")
The second parameter is the file attribute and can be any of three letters, "r", "w", or "a", and must be lower case. When an "r" is used, the file is opened for reading, a "w" is used to indicate a file to be used for writing, and an "a" indicates that you desire to append additional data to the data already in an existing file. Opening a file for reading requires that the file already exist. If it does not exist, the file pointer will be set to NULL and can be checked by the program. WRITING ("w") When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does resulting in deletion of any data already there.
APPENDING ("a")
When a file is opened for appending, it will be created if it does not already exist and it will be initially empty. If it does exist, the data input point will be the end of the present data so that any new data will be added to any data that already exists in the file.
If you have a file with this name, you should change its name or move it because when we execute this program, its contents will be erased. If you don’t have a file by this name, that is good because we will create one and put some data into it. READING ("r")
The second parameter is the file attribute and can be any of three letters, "r", "w", or "a", and must be lower case. When an "r" is used, the file is opened for reading, a "w" is used to indicate a file to be used for writing, and an "a" indicates that you desire to append additional data to the data already in an existing file. Opening a file for reading requires that the file already exist. If it does not exist, the file pointer will be set to NULL and can be checked by the program. WRITING ("w") When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does resulting in deletion of any data already there.
APPENDING ("a")
When a file is opened for appending, it will be created if it does not already exist and it will be initially empty. If it does exist, the data input point will be the end of the present data so that any new data will be added to any data that already exists in the file.