Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Системное программирование и WinAPI > WriteFile


Автор: G0L1a7h 21.5.2008, 16:39
Здравствуйте!
Столкнулся с проблемой, раньше вроде такова не было..

Имеются несколько массивов байт вида:
BYTE buf[5] = {0x00, 0x01, 0x02, 0x03, 0x04};
после некоторых действий с ними записываю их в файл функцией WriteFile();
т.е. функцию вызываю столько раз сколько массивов.
Но записанном файле появляется много "левых" символов.
Почему так получается? как этого избежать? 


Автор: Alexeis 21.5.2008, 17:28
G0L1a7h, скорее всего неверный код записи. smile

Автор: kosmonaFFFt 21.5.2008, 17:32
ИМХО у тебя не обрезается файл после записи, когда ты пишешь больше чем старый размер файла. Попробуй обрезать.


Код

SetEndOfFile Function

Sets the physical file size for the specified file to the current position of the file pointer.
The physical file size is also referred to as the end of the file. The SetEndOfFile function can be used to truncate or extend a file. To set the logical end of a file, use the SetFileValidData function.
Syntax
BOOL WINAPI SetEndOfFile(
  __in  HANDLE hFile
);

Parameters
hFile
A handle to the file to be extended or truncated.
 The file handle must be created with the GENERIC_WRITE access right. For more information, see File Security and Access Rights.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
Remarks
The SetEndOfFile function can be used to truncate or extend a file. If the file is extended, the contents of the file between the old end of the file and the new end of the file are not defined.
Each file stream has the following:
File size: the size of the data in a file, to the byte.
Allocation size: the size of the space that is allocated for a file on a disk, which is always an even multiple of the cluster size.
Valid data length: the length of the data in a file that is actually written, to the byte. This value is always less than or equal to the file size.
 The SetEndOfFile function sets the file size. Use SetFileValidData to set the valid data length. 
If CreateFileMapping is called to create a file mapping object for hFile, UnmapViewOfFile must be called first to unmap all views and call CloseHandle to close the file mapping object before you can call SetEndOfFile.
Transacted Operations
If there is a transaction bound to the handle, then the change in the end-of-file position is transacted.
Requirements
Client
Requires Windows Vista, Windows XP, or Windows 2000 Professional.
  Server
Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
  Header
Declared in WinBase.h; include Windows.h.
  Library
Use Kernel32.lib.
  DLL
Requires Kernel32.dll

Автор: G0L1a7h 21.5.2008, 17:44
ммм...незнаю что и сказать =\
После прочтения поста Alexeis`a открыл проект, попробовал еще раз -- "левые" символы присутствуют..
Ни чего не меняя пошел не перекур, пришел все работает как надо  smile  smile  smile 

Автор: dizzy1984 22.5.2008, 05:37
По-моему, без кода тут ничего конкретно не скажешь... Если у тебя сейчас работает, проблема скорее всего осталась...

Автор: G0L1a7h 22.5.2008, 05:54
не, проблема исчезла.. 500 раз уже проверил.. все прекрасно работает..

Цитата

когда ты пишешь больше чем старый размер файла

файл создается новый.

как-то так.. ну короче все стандартно..


Код

BYTE buf[5] = {0x00, 0x01, 0x02, 0x03, 0x04};
WriteFile(hFile,buf,5,&writen,0);


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)