begin process at 2012 02 12 00:29:10
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Fichiers / Disque

 > RETAILLER UN FICHIER

RETAILLER UN FICHIER


 Information sur la source

Note :
7 / 10 - par 1 personne
7,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Fichiers / Disque Niveau :Débutant Date de création :07/12/2003 Vu :1 499

Auteur : gilles78

Ecrire un message privé
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

un p'tit prog C++ qui permet de 'retailler' un fichier de manière à en modifier sa taille (en octets). A utiliser sous DOS en ligne de commande. On passe à la commande le nom du fichier et la nouvelle taille désirée, le reste... il s'en charge.

Source

  • #include <iostream>
  • #include <stdlib.h>
  • #include <windows.h>
  • using namespace std;
  • int main(int argc, char *argv[])
  • {
  • ////////////////////////////////////////////////////
  • // Gestion des parametres de la ligne de commande
  • if(argc!=3) {
  • cout<<"\nErreur ligne de commande\ntruncFile <filename> <newsize>\n"<<endl;
  • return -1;
  • } // end if
  • ////////////////////////////////////////////////////
  • // Ouverture du fichier
  • HANDLE fileH = CreateFile(
  • argv[1],GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  • if(fileH == INVALID_HANDLE_VALUE) {
  • LPVOID lpMsgBuf;
  • FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  • NULL,
  • GetLastError(),
  • MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  • (LPTSTR) &lpMsgBuf, 0, NULL
  • );
  • cout<<"\nErreur: acces au fichier "<<argv[1]<<"\nMsg = "<<(char*)lpMsgBuf<<
  • "\n"<<endl;
  • LocalFree( lpMsgBuf );
  • return(-1);
  • } // end if
  • ////////////////////////////////////////////////////
  • // Creation du LARGE_INTEGER pour le resizing
  • LARGE_INTEGER newSize;
  • newSize.LowPart = 0L; newSize.HighPart = 0L;
  • if( strlen(argv[2])>10 ) {
  • newSize.LowPart = atoi(argv[2]+10);
  • *(argv[2]+10) = '\0';
  • newSize.HighPart = atoi(argv[2]);
  • } else {
  • newSize.LowPart = atoi(argv[2]);
  • }// end if
  • ////////////////////////////////////////////////////
  • // Et on retaille....
  • if( !SetFilePointerEx(fileH,newSize,NULL,FILE_BEGIN)) {
  • LPVOID lpMsgBuf;
  • FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  • NULL,
  • GetLastError(),
  • MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  • (LPTSTR) &lpMsgBuf, 0, NULL
  • );
  • cout<<"\nErreur: resizing etape 1.\nMsg = "<<(char*)lpMsgBuf<<"\n"<<endl;
  • LocalFree( lpMsgBuf );
  • } // end if
  • if( !SetEndOfFile(fileH)) {
  • LPVOID lpMsgBuf;
  • FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  • NULL,
  • GetLastError(),
  • MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  • (LPTSTR) &lpMsgBuf, 0, NULL
  • );
  • cout<<"\nErreur: resizing etape 2.\nMsg = "<<(char*)lpMsgBuf<<"\n"<<endl;
  • LocalFree( lpMsgBuf );
  • } // end if
  • ////////////////////////////////////////////////////
  • // Cloture du fichier
  • CloseHandle(fileH);
  • return 0;
  • }
#include <iostream>
#include <stdlib.h>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{  
  ////////////////////////////////////////////////////
  // Gestion des parametres de la ligne de commande
  if(argc!=3) {
    cout<<"\nErreur ligne de commande\ntruncFile <filename> <newsize>\n"<<endl;
    return -1;
  } // end if

  ////////////////////////////////////////////////////
  // Ouverture du fichier
  HANDLE fileH = CreateFile(
    argv[1],GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  
  if(fileH == INVALID_HANDLE_VALUE) {
    LPVOID lpMsgBuf;

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR) &lpMsgBuf, 0, NULL
    );

    cout<<"\nErreur: acces au fichier "<<argv[1]<<"\nMsg = "<<(char*)lpMsgBuf<<
        "\n"<<endl;

    LocalFree( lpMsgBuf );
    return(-1);
  } // end if

  ////////////////////////////////////////////////////
  // Creation du LARGE_INTEGER pour le resizing
  LARGE_INTEGER newSize;
  
  newSize.LowPart = 0L; newSize.HighPart = 0L;
  if( strlen(argv[2])>10 ) {
    newSize.LowPart  = atoi(argv[2]+10);
    *(argv[2]+10) = '\0';
    newSize.HighPart = atoi(argv[2]);
  } else {
    newSize.LowPart  = atoi(argv[2]);
  }// end if

  ////////////////////////////////////////////////////
  // Et on retaille....
  if( !SetFilePointerEx(fileH,newSize,NULL,FILE_BEGIN)) {
    LPVOID lpMsgBuf;

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR) &lpMsgBuf, 0, NULL
    );

    cout<<"\nErreur: resizing etape 1.\nMsg = "<<(char*)lpMsgBuf<<"\n"<<endl;

    LocalFree( lpMsgBuf );
  } // end if

  if( !SetEndOfFile(fileH)) {
    LPVOID lpMsgBuf;

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR) &lpMsgBuf, 0, NULL
    );

    cout<<"\nErreur: resizing etape 2.\nMsg = "<<(char*)lpMsgBuf<<"\n"<<endl;

    LocalFree( lpMsgBuf );
  } // end if

  ////////////////////////////////////////////////////
  // Cloture du fichier
  CloseHandle(fileH);
  return 0;
}



 Sources de la même categorie

Source avec Zip Source avec une capture GENERE BMP par lajouad
Source avec Zip Source avec une capture GETIONNAIRE D'UNE BIBLIOTHÉQUE EN C par benzarabel
FONCTION D'ÉDITION DE FICHIER BIT À BIT [C-MULTIPLATEFORME] par lynxtyle
Source avec Zip Source avec une capture UN GESTIONNAIRE DU FICHIER par benzarabel
Source avec Zip COPIE DE FICHIERS PAR RESEAU LOCAL par cczerty

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,919 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales