Где ошибка? Помогите найти.
От: oi2005  
Дата: 01.08.06 08:20
Оценка:
Уважаемые эксперты!

Помогите найти ошибку, я только начинающий прогр. на С/С++, поэтому не судите строго.

Написана на MS VISUAL 2005
Программа пытается найти файлы в папке C:\WINDOWS\TEMP\
и удалить, потом должна создать лог файл(дата | выполнение).
#include <windows.h>
#include <stdio.h>
#include <string.h>

#pragma warning(disable: 4996)

void removeallfiles(char *path);

int main(int argc, char **argv)
{            
    removeallfiles("C:\\WINDOWS\\TEMP\\");
    HANDLE hFile;
    hFile = CreateFile(TEXT("E:\\temp.log"),
                       GENERIC_READ | GENERIC_WRITE, 
                       FILE_SHARE_READ | FILE_SHARE_WRITE, 
                       NULL,
                       CREATE_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL,
                       0);
    char buf[1024];
    SYSTEMTIME lpSystemTime;            
    GetLocalTime((LPSYSTEMTIME) &lpSystemTime);
    sprintf(buf,"TIME [%d.%d.%d %d:%d:%d:%d] EVENT [DELETING ALL INFORMATION FROM C:\\WINDOWS\\TEMP\\\13\10]",
            lpSystemTime.wDay,    
            lpSystemTime.wMonth,
            lpSystemTime.wYear,
            lpSystemTime.wHour,
            lpSystemTime.wMinute,
            lpSystemTime.wSecond,
            lpSystemTime.wMilliseconds);
    WriteFile(hFile,buf,sizeof(buf),NULL,NULL);
    CloseHandle(hFile);            
    return 0;
}

void removeallfiles(char* path)    
{
    char *buf = NULL;
    if ((buf = (char* )malloc(strlen(path) + 2 + MAX_PATH)) == NULL)
    {
        printf("not enough memory!");
        exit(1);
    }    
    strcat(path,"*.*");    
    WIN32_FIND_DATA ff;
    HANDLE h = FindFirstFileW((LPCWSTR)path,&ff);        
    do
    {
        buf[strlen(buf)-strlen((char* )ff.cFileName)] = '\0';
        FindNextFile(h, &ff);
        strcat(buf,(char* )ff.cFileName);
        if(!DeleteFile((LPCWSTR)buf))
        {
            printf("ERROR is %d", GetLastError());
            exit(1);
        }
    }while(h != INVALID_HANDLE_VALUE);
    free(buf);
}

Добавлено форматирование — Кодт
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.