- #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;
}