|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
le fopen, _wfopen
FILE *fopen( const char *filename, const char *mode ); FILE *_wfopen( const wchar_t *filename, const wchar_t *mode );
Prise en charge Unicode
Remarque
|
|---|
|
|
|
|
|
|
|
|---|---|---|---|
|
UNICODE |
UTF-16LE |
UTF-8 |
UTF-16LE |
|
UTF-8 |
UTF-8 |
UTF-8 |
UTF-16LE |
|
UTF-16LE |
UTF-16LE |
UTF-8 |
UTF-16LE |
|
|
|
|
|
|---|---|---|---|
|
_tfopen |
fopen |
fopen |
_wfopen |
|
|
|
|---|---|
|
a |
|
|
a+ |
|
|
r |
_O_RDONLY |
|
r+ |
_O_RDWR |
|
w |
|
|
w+ |
|
|
b |
_O_BINARY |
|
t |
_O_TEXT |
|
c |
|
|
n |
|
|
S |
_O_SEQUENTIAL |
|
R |
_O_RANDOM |
|
T |
_O_SHORTLIVED |
|
D |
_O_TEMPORARY |
|
ccs=UNICODE |
_O_WTEXT |
|
ccs=UTF-8 |
_O_UTF8 |
|
ccs=UTF-16LE |
_O_UTF16 |
|
|
|
|---|---|
|
fopen |
|
|
_wfopen |
|
// crt_fopen.c
// compile with: /W3
// This program opens two files. It uses
// fclose to close the first file and
// _fcloseall to close all remaining files.
#include <stdio.h>
FILE *stream, *stream2;
int main( void )
{
int numclosed;
// Open for read (will fail if file "crt_fopen.c" does not exist)
if( (stream = fopen( "crt_fopen.c", "r" )) == NULL ) // C4996
// Note: fopen is deprecated; consider using fopen_s instead
printf( "The file 'crt_fopen.c' was not opened\n" );
else
printf( "The file 'crt_fopen.c' was opened\n" );
// Open for write
if( (stream2 = fopen( "data2", "w+" )) == NULL ) // C4996
printf( "The file 'data2' was not opened\n" );
else
printf( "The file 'data2' was opened\n" );
// Close stream if it is not NULL
if( stream)
{
if ( fclose( stream ) )
{
printf( "The file 'crt_fopen.c' was not closed\n" );
}
}
// All other files are closed:
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}
Le fichier « crt_fopen.c » a été ouvert le fichier « data2 » a été ouvert nombre de fichiers fermés par le _fcloseall : 1
// crt__wfopen.c
// compile with: /W3
// This program creates a file (or overwrites one if
// it exists), in text mode using Unicode encoding.
// It then writes two strings into the file
// and then closes the file.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>
#define BUFFER_SIZE 50
int main(int argc, char** argv)
{
wchar_t str[BUFFER_SIZE];
size_t strSize;
FILE* fileHandle;
// Create an the xml file in text and Unicode encoding mode.
if ((fileHandle = _wfopen( L"_wfopen_test.xml",L"wt+,ccs=UNICODE")) == NULL) // C4996
// Note: _wfopen is deprecated; consider using _wfopen_s instead
{
wprintf(L"_wfopen failed!\n");
return(0);
}
// Write a string into the file.
wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"<xmlTag>\n");
strSize = wcslen(str);
if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
{
wprintf(L"fwrite failed!\n");
}
// Write a string into the file.
wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"</xmlTag>");
strSize = wcslen(str);
if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
{
wprintf(L"fwrite failed!\n");
}
// Close the file.
if (fclose(fileHandle))
{
wprintf(L"fclose failed!\n");
}
return 0;
}
Remarque