AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

ReadFile -_-

Ein Thema von Pseudemys Nelsoni · begonnen am 28. Feb 2004 · letzter Beitrag vom 2. Mär 2004
Antwort Antwort
Seite 1 von 2  1 2   
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#1

ReadFile -_-

  Alt 28. Feb 2004, 10:08
sorry, schon wieder (fast) das gleiche...
habe das ganze forum (u.a auch DF) nach readfile durchsucht, aber was richtiges war nie dabei...

Also, was muss ich ReadFile alles übergeben?

Also ic habe das bis jetzt so: ReadFile(hFile, buf, ...und nun? woher weiss ich wieviel bytes gelesen werden sollen?);

also ich weiss nicht was ich noch übergeben muss, und wa sist lpOverlapped?
Mario
  Mit Zitat antworten Zitat
Benutzerbild von nailor
nailor

Registriert seit: 12. Dez 2002
Ort: Karlsruhe
1.989 Beiträge
 
#2

Re: ReadFile -_-

  Alt 28. Feb 2004, 10:26
Zitat:
The ReadFile function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read, unless the file handle is created with the overlapped attribute. If the file handle is created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the read operation.

BOOL ReadFile(

HANDLE hFile, // handle of file to read
LPVOID lpBuffer, // address of buffer that receives data
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // address of number of bytes read
LPOVERLAPPED lpOverlapped // address of structure for data
);


Parameters

hFile

Identifies the file to be read. The file handle must have been created with GENERIC_READ access to the file.

Windows NT

For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept functions.

Windows 95

For asynchronous read operations, hFile can be a communications resource, mailslot, or named pipe handle opened with the FILE_FLAG_OVERLAPPED flag by CreateFile, or a socket handle returned by the socket or accept functions. Windows 95 does not support asynchronous read operations on disk files.

lpBuffer

Points to the buffer that receives the data read from the file.

nNumberOfBytesToRead

Specifies the number of bytes to be read from the file.

lpNumberOfBytesRead

Points to the number of bytes read. ReadFile sets this value to zero before doing any work or error checking. If this parameter is zero when ReadFile returns TRUE on a named pipe, the other end of the message-mode pipe called the WriteFile function with nNumberOfBytesToWrite set to zero.
If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL.
If lpOverlapped is not NULL, lpNumberOfBytesRead can be NULL. If this is an overlapped read operation, you can get the number of bytes read by calling GetOverlappedResult. If hFile is associated with an I/O completion port, you can get the number of bytes read by calling GetQueuedCompletionStatus.

lpOverlapped

Points to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED.

If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was created with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the read operation is complete.
If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure and ReadFile may return before the read operation has been completed. In this case, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue while the read operation finishes. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the read operation.

If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed.
If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed.



Return Values

If the function succeeds, the return value is nonzero.
If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation. However, if the file was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the return value is FALSE and GetLastError returns ERROR_HANDLE_EOF when the file pointer goes beyond the current end of file.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

ReadFile returns when one of the following is true: a write operation completes on the write end of the pipe, the number of bytes requested has been read, or an error occurs.
If part of the file is locked by another process and the read operation overlaps the locked portion, this function fails.
Applications must not read from nor write to the input buffer that a read operation is using until the read operation completes. A premature access to the input buffer may lead to corruption of the data read into that buffer.

Characters can be read from the console input buffer by using ReadFile with a handle to console input. The console mode determines the exact behavior of the ReadFile function.
If a named pipe is being read in message mode and the next message is longer than the nNumberOfBytesToRead parameter specifies, ReadFile returns FALSE and GetLastError returns ERROR_MORE_DATA. The remainder of the message may be read by a subsequent call to the ReadFile or PeekNamedPipe function.

When reading from a communications device, the behavior of ReadFile is governed by the current communication timeouts as set and retrieved using the SetCommTimeouts and GetCommTimeouts functions. Unpredictable results can occur if you fail to set the timeout values. For more information about communication timeouts, see COMMTIMEOUTS.
If ReadFile attempts to read from a mailslot whose buffer is too small, the function returns FALSE and GetLastError returns ERROR_INSUFFICIENT_BUFFER.

If the anonymous write pipe handle has been closed and ReadFile attempts to read using the corresponding anonymous read pipe handle, the function returns FALSE and GetLastError returns ERROR_BROKEN_PIPE.
The ReadFile function may fail and return ERROR_INVALID_USER_BUFFER or ERROR_NOT_ENOUGH_MEMORY whenever there are too many outstanding asynchronous I/O requests.
The ReadFile code to check for the end-of-file condition (eof) differs for synchronous and asynchronous read operations.

When a synchronous read operation reaches the end of a file, ReadFile returns TRUE and sets *lpNumberOfBytesRead to zero. The following sample code tests for end-of-file for a synchronous read operation:

// attempt a synchronous read operation
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL) ;
// check for eof
if (bResult && nBytesRead == 0, ) {
// we're at the end of the file
}


An asynchronous read operation can encounter the end of a file during the initiating call to ReadFile, or during subsequent asynchronous operation.
If EOF is detected at ReadFile time for an asynchronous read operation, ReadFile returns FALSE and GetLastError returns ERROR_HANDLE_EOF.
If EOF is detected during subsequent asynchronous operation, the call to GetOverlappedResult to obtain the results of that operation returns FALSE and GetLastError returns ERROR_HANDLE_EOF.

To cancel all pending asynchronous I/O operations, use the CancelIO function. This function only cancels operations issued by the calling thread for the specified file handle. I/O operations that are canceled complete with the error ERROR_OPERATION_ABORTED.
The following sample code illustrates testing for end-of-file for an asynchronous read operation:

// set up overlapped structure fields
// to simplify this sample, we'll eschew an event handle
gOverLapped.Offset = 0;
gOverLapped.OffsetHigh = 0;
gOverLapped.hEvent = NULL;

// attempt an asynchronous read operation
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead,
&gOverlapped) ;

// if there was a problem, or the async. operation's still pending ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())

{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
// during the call to ReadFile

// code to handle that
}

case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress

// do something else for a while
GoDoSomethingElse() ;

// check on the results of the asynchronous read
bResult = GetOverlappedResult(hFile, &gOverlapped,

&nBytesRead, FALSE) ;

// if there was a problem ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
//during asynchronous operation
}

// deal with other error cases

}
}
} // end case

// deal with other error cases

} // end switch
} // end if


See Also

CancelIo, CreateFile, GetCommTimeouts, GetOverlappedResult, GetQueuedCompletionStatus, OVERLAPPED, PeekNamedPipe, ReadFileEx, SetCommTimeouts, WriteFile
HFile: Mit OpenFile erhaltenes Handle
lpBuffer: Wohin das Zeug aus der Datei gelesen werden soll
nNumberOfBytesToRead: Wieviel gelesen werden soll
lpNumberOfBytesRead: Rückmeldung, wieviel tatsächlich gelesen werden konnte
lpOverlapped: normalerweise reicht ein simples "nil"
Michael N.
http://nailor.devzero.de/code/sharpmath/testing/ --- Tests, Feedback, Anregungen, ... aller Art sehr willkommen!
::: don't try so hard - it'll happen for a reason :::
  Mit Zitat antworten Zitat
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#3

Re: ReadFile -_-

  Alt 28. Feb 2004, 12:03
hoi, jo, hab schon gelesen, danke.

aber ich weiss nicht was ich für nen typ übergeben muss für "wieviel bytes sollen gelesen werden" bzw "wurden gelesen". woher weiss ich wieviele bytes ich lesen will?
Mario
  Mit Zitat antworten Zitat
Christian Seehase
(Co-Admin)

Registriert seit: 29. Mai 2002
Ort: Hamburg
11.105 Beiträge
 
Delphi 11 Alexandria
 
#4

Re: ReadFile -_-

  Alt 28. Feb 2004, 13:07
Moin Pseudemys Nelsoni,

Zitat von Pseudemys Nelsoni:
woher weiss ich wieviele bytes ich lesen will?
Gegenfrage: Wer ausser Dir (bzw. Deinem Programm) soll es sonst wissen?
Es könnte sein, dass Du beispielsweise immer 64K Blöcke lesen willst, oder byteweise, oder auch die ganze Datei auf einmal.
Das hängt dann von der jeweiligen Situation ab.

Mit nNumberOfBytesToRead übergibst Du ja der Funktion die Anzahl der Bytes, die Du lesen willst, und die Funktion liefert Dir dann in lpNumberOfBytesRead zurück, wieviele gelesen wurden, denn Du könntest ja auch mehr abfordern, als (noch) zur Verfügung stehen.
Tschüss Chris
Die drei Feinde des Programmierers: Sonne, Frischluft und dieses unerträgliche Gebrüll der Vögel.
Der Klügere gibt solange nach bis er der Dumme ist
  Mit Zitat antworten Zitat
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#5

Re: ReadFile -_-

  Alt 28. Feb 2004, 14:37
hallo christian, ich möchte eine ganze datei auslesen, jo, aber wie geb ich die grösse in byte an?
Mario
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#6

Re: ReadFile -_-

  Alt 28. Feb 2004, 14:47
Am besten mit dem Ziffernblock. Sorry aber ich verstehe die Frage nicht. Noch mal: Du gibst so viel an wie du aufeinaml lesen möchtest, zum Beipsiel 1024 Bytes. Dann liest ReadFile 1024 Bytes so lange wie es geht. Irgendwann wird ReadFile, wenn du das in einer Schleife tust nichtmehr 1024 Bytes lesen können, weil nicht mehr so viele Bytes da sind. Wie viel es dann noch gelesen hat steht im vorletzten Parameter. Das heißt mit PseudoCode:

Delphi-Quellcode:
var
  Gelesen: Cardinal;
begin
  while Gelesen = 1024 do
  begin
    ReadFile(..., ..., 1024, Gelesen, nil)
    // mach was mit den Bytes
  end;
Wenn Gelesen also nicht mehr 1024 ist, dann weißt du, dass du die ganze Datei in der Schleife eingelesen hast.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#7

Re: ReadFile -_-

  Alt 28. Feb 2004, 19:35
Zitat:
Am besten mit dem Ziffernblock
hehe.

danke für den code, das hilft mir ^^
Mario
  Mit Zitat antworten Zitat
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#8

Re: ReadFile -_-

  Alt 2. Mär 2004, 00:17
hoi,

also so ganz bin ich doch noch nicht durchgestiegen.
Ich weiss nicht wie ich damit in einer dll z.b ein wort auslesen kann. gibt es für diese funktion kein kleines beispiel?
Mario
  Mit Zitat antworten Zitat
Benutzerbild von nailor
nailor

Registriert seit: 12. Dez 2002
Ort: Karlsruhe
1.989 Beiträge
 
#9

Re: ReadFile -_-

  Alt 2. Mär 2004, 14:27
häh? ein wort das in einer dll steht? oder ein wort in einer textdatei auslesen und das ganze in einer dll?
Michael N.
http://nailor.devzero.de/code/sharpmath/testing/ --- Tests, Feedback, Anregungen, ... aller Art sehr willkommen!
::: don't try so hard - it'll happen for a reason :::
  Mit Zitat antworten Zitat
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#10

Re: ReadFile -_-

  Alt 2. Mär 2004, 14:31
es ist eine dll die u.a(!) text enthält, dort steht ein server drin, und den will ich durch einen anderen ersetzen.

z.b "blub.server1.de" durch blub.server2.de"
Mario
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2   

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:17 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz