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 ]...);
|
|
|||||||||||||||
|