Originally Posted by
Babkockdood
goto statements, maybe? I don't know, recursion and looping sound like the same thing to me.
They're not... a loop is a group of instructions repeated several times, recursion is when a function calls itself.
Example for looping: test reading a file.
Code:
// single file test
void TestFile(PVOID FName)
{ DWORD BytesRead = 1;
if (!Scanning)
return;
++ FileCount;
HANDLE fh = CreateFile(FName,FILE_READ_DATA,FILE_SHARE_READ,NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_NO_BUFFERING |
FILE_FLAG_SEQUENTIAL_SCAN,NULL);
if (fh == INVALID_HANDLE_VALUE)
{ AddError(GetLastError(),FName);
return; }
while ((BytesRead > 0) && Scanning)
{ if (!ReadFile(fh,FileBuffer,BufferSize,&BytesRead,NULL))
{ PostMessage(WHandle[0],UM_ADDERR,GetLastError(),(LPARAM)FName);
break; }
ByteCount += BytesRead; }
CloseHandle(fh); }
Example for recursion: Tunneling down subdirectories in a file system.
Code:
// recursive directory search
void TestDir(PVOID SPath)
{ HANDLE HDir;
++ FolderCount;
if (!SetCurrentDirectory(SPath))
{ PostMessage(WHandle[0],UM_ADDERR,GetLastError(),(LPARAM)SPath);
return; }
HDir = FindFirstFile("*.*",&DirData);
if (HDir == INVALID_HANDLE_VALUE)
{ PostMessage(WHandle[0],UM_ADDERR,GetLastError(),0);
return; }
do
{ if (DirData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{if (Recurse && (DirData.cFileName[0] != '.'))
TestDir(DirData.cFileName); }
else
TestFile(DirData.cFileName); }
while (FindNextFile(HDir,&DirData) && Scanning);
FindClose(HDir);
SetCurrentDirectory(".."); }