PureBasic - форум

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » PureBasic - форум » Вопросы по PureBasic » ? ReadData()


? ReadData()

Сообщений 1 страница 3 из 3

1

Есть давнишняя процедура - запись строки в лог-файл
Понадобилось добавить контроль размера файла что-бы не рос безгранично.
Почему то ReadData() выдает всегда 0.
Может у меня глаз замылился - очевидного не вижу?

Код:
Procedure WriteToLog(entry.s)
	Protected hFile.l, LenFile.q, p, bytes, *MemoryID
	LockMutex(LogFileMutex)
	hFile = OpenFile(#PB_Any, glLogFile$)
	If hFile = #Null
    ProcedureReturn #False
	EndIf
	FileSeek(hFile, Lof(hFile))
	WriteStringN(hFile, FormatDate("%yyyy%mm%dd %hh:%ii:%ss:", Date())+entry)

	;контроль размера файла
	LenFile=Lof(hFile)
	If LenFile>51200 ;50k
    FlushFileBuffers(hFile)
    Debug Str(LenFile)+">51200"
    
    *MemoryID = AllocateMemory(LenFile)   ; allocate the needed memory
    If *MemoryID
    	Debug "Lof(hFile)="+Str(Lof(hFile))
    	bytes = ReadData(hFile, *MemoryID, LenFile)   ; read all data into the memory block
    	Debug "bytes="+Str(bytes)
    	For p=(LenFile-51200) To (LenFile-1)
        
        If PeekS(*MemoryID+p , 2, #PB_Ascii)=#CRLF$
        	CloseFile(hFile)
        	If CreateFile(hFile, glLogFile$)
            WriteData(hFile, *MemoryID+p, LenFile-p-1)
        	EndIf
        EndIf
    	Next
    EndIf
    
	EndIf
	CloseFile(hFile)
	UnlockMutex(LogFileMutex)
	ProcedureReturn #True
EndProcedure

0

2

SadStar написал(а):

LockMutex(LogFileMutex)
hFile = OpenFile(#PB_Any, glLogFile$)
If hFile = #Null
    ProcedureReturn #False
EndIf

И будет глюк! Мьютекс захватывается но не освобождается.

SadStar написал(а):

Почему то ReadData() выдает всегда 0.

Скорее всего потому что нет строки FileSeek(hFile, 0) перед ReadData(). Иначе производится попытка чтения с текущей позиции, а она судя по всему, равна окончанию файла.

0

3

Да. Действительно - этот косяк.
Оставил работать такой вариант - может кому пригодится.
Обрезает файл до заданной длины и делает так
чтобы первая строка не оказалась с обрезаным началом.
Можно еще пооптимизоровать.

Код:
Procedure WriteToLog(entry.s)
	Protected hFile.l, LenFile.q, MaxLen=51000, p, bytes, *Memory
	LockMutex(LogFileMutex)
	hFile = OpenFile(#PB_Any, glLogFile$)
	If hFile = #Null
    UnlockMutex(LogFileMutex)
    ProcedureReturn #False
	EndIf
	FileSeek(hFile, Lof(hFile))
	WriteStringN(hFile, FormatDate("%yyyy%mm%dd %hh:%ii:%ss:", Date())+entry)
	;контроль размера файла
	LenFile=Lof(hFile)
	If LenFile>MaxLen ;50k
    FlushFileBuffers(hFile)
    *Memory = AllocateMemory(LenFile)   ; allocate the needed memory
    If *Memory
    	FileSeek(hFile, 0)
    	If ReadData(hFile, *Memory, LenFile)   ; read all data into the memory block
        For p=(LenFile-MaxLen) To (LenFile-1)
        	If PeekS(*Memory+p , 2, #PB_Ascii)=#CRLF$
            CloseFile(hFile)
            hFile = CreateFile(#PB_Any , glLogFile$)
            If hFile
            	WriteData(hFile, *Memory+p, LenFile-p-1)
            	Break
            EndIf
        	EndIf
        Next
    	EndIf
    EndIf
	EndIf
	CloseFile(hFile)
	UnlockMutex(LogFileMutex)
	ProcedureReturn #True
EndProcedure

0


Вы здесь » PureBasic - форум » Вопросы по PureBasic » ? ReadData()