Since a file is a unit of storage and it stores information, it has a size, which is the number of bits it contains. To manage it, a file also has a location, also called a path that specifies where and/or how the file can be retrieved. Also, for better management, a file has attributes that indicate what can be done on a file or that provide specific information that the programmer or the operating system can use when dealing with the file.
There are differences and similarities between a directory and a file. Among the differences:
The similarities of both types are:
File processing consists of creating, storing, and/or retrieving the contents of a file from a recognizable medium. For example, it is used to save word-processed files to a hard drive, to store a presentation on floppy disk, or to open a file from a CD-ROM. To perform file processing on your application, you have many choices: the C and/or C++ languages, the Win32 library, or the MFC.
In order to manage files stored in a computer, each file must be able to provide basic pieces of information about itself. This basic information is specified when the file is created but can change during the life time of a file.
To create a file, a user must first decide where it would be located: this is a requirement. A file can be located on the root drive. Alternatively, a file can be positioned inside of an existing folder. Based on security settings, a user may not be able to create a file just anywhere in the (file system of the) computer. Once the user has decided where the file would reside, there are various means of creating files that the users are trained to use.
When creating a file, the user must give it a name following the rules of the operating system combined with those of the file system.
The most fundamental piece of information a file must have is a name. Once the user has created a file, whether the file is empty or not, the operating system assigns basic pieces of information to it. Once a file is created, it can be opened, updated, modified, renamed, etc.
Because files on a computer can be stored in various places, Microsoft Windows provides various means of creating, locating, and managing files through objects named Windows Common Dialog Boxes. Indeed, these dialog boxes are part of the operating system and are equipped with all the necessary operations pertinent to their functionality.
The common dialog boxes available in the MFC share a parent as the CCommonDialog class. The CCommonDialog class is derived from CDialog:
The only member the CCommonDialog class has a constructor that takes a point to CWnd as argument.
The C language provides very impressive support for file processing through a structure and many functions. The fundamental structure used to perform file processing in C is the FILE structure. In reality, to use this structure, you must call another function and get its return value.
The primary operation of file processing consists of opening one. In the strict sense, this means that you are initiating file processing. This can be done in C by calling one of the fopen-variant functions. Their syntax are:
FILE *fopen(const char *filename, const char *mode); FILE *_wfopen(const wchar_t *filename, const wchar_t *mode);
To support Unicode, you should use the _wfopen() version. In both cases, the first argument specifies the name of the file you want to work on. If you pass just the name of the file, the compiler will consider the directory of the current project. Otherwise, you can provide the complete path of the project. The second argument specifies the type of operation you want to perform. It is passed as a string.
When this function has been called, if it succeeds in carrying its operation (we will see what types of operations are available), it returns a FILE object. If the function fails, it returns NULL.
To create a file, you can pass the second argument of fopen withe one the following values:
The C language provides various functions you can use to write values to a file. Some functions deal with strings while others are supposed to handle any type of value. All functions that a FILE object as argument. This is the object that holds the file.
To write a character to a FILE object, you can call one of the following functions:
int fputc(int c, FILE *stream); wint_t fputwc(wchar_t c, FILE *stream);
The first version of these functions takes a character as argument. The second takes a wide character. The character would be written to the file.
To write a string to a file, you can call one of these functions:
int fputs(const char *str, FILE *stream); int fputws(const wchar_t *str, FILE *stream);
To write other types of values to a file, you can call one of the following functions:
size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream);
To format a value before writting it to the file, you can call one of these functions:
int fprintf(FILE *stream, const char *format [, argument ]...); int _fprintf_l(FILE *stream, const char *format, locale_t locale [, argument ]...); int fwprintf(FILE *stream, const wchar_t *format [, argument ]...); int _fwprintf_l(FILE *stream, const wchar_t *format, locale_t locale [, argument ]...);
After using a FILE object, you should close it to free the resources it was using. This is done by calling the fclose() or _fcloseall() function. Their syntaxes are:
int fclose(FILE *stream); int _fcloseall(void);
in the same way, C provides various functions to read values from a file. Like their writing counterparts, function readers take a FILE object as argument.
To read a character from a FILE object, you can call one of the following functions:
int fgetc(FILE *stream); wint_t fgetwc(FILE *stream);
The first version reads a regular character and the second reads a wide character. To read a string from a file, you can call one of these functions:
char *fgets(char *str, int n, FILE *stream); wchar_t *fgetws(wchar_t *str, int n, FILE *stream);
To read other types of values from a file, you can call one of the following functions:
size_t fread(void *buffer, size_t size, size_t count, FILE *stream);
To read values that were formatted, you can call one of these functions:
int fscanf(FILE *stream, const char *format [, argument ]...); int _fscanf_l(FILE *stream, const char *format, locale_t locale [, argument ]...); int fwscanf(FILE *stream, const wchar_t *format [, argument ]...); int _fwscanf_l(FILE *stream, const wchar_t *format, locale_t locale [, argument ]...); int fscanf_s(FILE *stream, const char *format [, argument ]...); int _fscanf_s_l(FILE *stream, const char *format, locale_t locale [, argument ]... ); int fwscanf_s(FILE *stream, const wchar_t *format [, argument ]...); int _fwscanf_s_l(FILE *stream, const wchar_t *format, locale_t locale [, argument ]...);