Fichier

freemaan -  
ghuysmans99 Messages postés 2496 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour, je voudrais avoir un exemple de code sur les fichier binaires et texts merci.

1 réponse

  1. ghuysmans99 Messages postés 2496 Date d'inscription   Statut Contributeur Dernière intervention   342
     
    Je suis dans mon jour de bonté ... Ce code fonctionne sous Windows :
    #include <stdio.h>
    #include <windows.h>
    #include <tchar.h>
    
    int main(int argc, char* argv[])
    {
    	HANDLE hFile = CreateFile(_T("monfichier.bin"),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    	char buffer[] = {1,2,3,4,5,6,7,8,9,10};
    	DWORD dwSize = 10, dwBW = 0, err = 0;
    	if (hFile == INVALID_HANDLE_VALUE)
    	{
    		_tprintf(_T("Can't open the file for writing.\n"));
    		return 1;
    	}
    	WriteFile(hFile,(LPVOID)buffer,dwSize,&dwBW,NULL);
    	if (dwBW != dwSize)
    	{
    		_tprintf(_T("Can't write to the file.\n"));
    		err=2; goto end;
    	}
    	if (SetEndOfFile(hFile) == 0)
    	{
    		_tprintf(_T("Can't set the file's end pointer.\n"));
    		err=3; goto end;
    	}
    end:
    	CloseHandle(hFile);
    	return err;
    }
    0