readFile 把数据读到缓冲区 怎样取换行符
实现类似于grep的搜索程序。在指定目录的所有文件中搜寻指定的字符串。
假设:这个目录下的所有文件均为文本文件;并且目录深度不超过32.
要求:
1)输出匹配的行数及所在文件名。
2)匹配时,大小写不敏感。
3) 指定目录和指定字符串均从main或WinMain的参数中读入。
4) 不能限定将要搜索的文件的大小。
5) 使用Win32 API中的文件I/O函数,例如: CreateFile, ReadFile, WriteFile等。
6) 也可使用NSPR中的文件I/O函数,例如: PR_Open, PR_Read等。
7) 不能使用C或C++的I/O方法。
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAC_FILENAMELENOPATH 50
int length(char T[])
{
int i = 0;
while(T[i] != '\0')
i++;
return i;
}
void readOneFile(char strFile[])
{
unsigned long i = 0;
int line = 1;
int j = 0;
BOOL bResult;
HANDLE handle;
char inBuffer[1024];
char upBuffer[1024];
char lowBuffer[1024];
ZeroMemory(inBuffer,1024);
DWORD nBytesRead;
handle = CreateFile("c:\\hello\\hello.txt",GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_ALWAYS,FILE_FLAG_RANDOM_ACCESS,NULL);
if(INVALID_HANDLE_VALUE == handle)
{
printf("error!");
CloseHandle(handle);
return;
}
bResult = ReadFile(handle, &inBuffer,1024, &nBytesRead, NULL);
while(bResult)
{
if ( nBytesRead == 0)
{
CloseHandle(handle);
return;
}
/*
strcpy(upBuffer, _strupr(inBuffer));
while(i != nBytesRead)
{
while(upBuffer[i] != '0x0A')
{
while(upBuffer[i] == strMatch[j])
{
i++;
j++;
if(j == length(strMatch))
printf("%d,%s\r\n",line,strMatch);
}
j=0;
i++;
}
line++;
}
*/
strcpy(upBuffer, _strupr(inBuffer));
printf("%s",upBuffer);
// strcpy(lowBuffer, _strlwr(inBuffer));
ZeroMemory(inBuffer,1024);
bResult = ReadFile(handle, &inBuffer,1024, &nBytesRead, NULL);
i=0;
}
CloseHandle(handle);
return;
}
void FindFileInDir(char *rootDir, char *strRet)
{
char fname[MAC_FILENAMELENOPATH];
ZeroMemory(fname, MAC_FILENAMELENOPATH);
WIN32_FIND_DATA fd;
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
HANDLE h_Search;
char filePathName[256];
char tempPath[256];
ZeroMemory(filePathName, 256);
ZeroMemory(tempPath, 256);
strcpy(filePathName, rootDir);
BOOL bSearchFindShed = FALSE;
if(filePathName)
{
strcat(filePathName, "\\\\");
}
strcat(filePathName, "*.*");
h_Search = FindFirstFile(filePathName, &fd);
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY &&
strcmp(fd.cFileName, "..")!=0 && strcmp(fd.cFileName, ".")!=0)
{
strcpy(tempPath, rootDir);
strcat(tempPath, "\\\\");
strcat(tempPath, fd.cFileName);
printf("%s\r\n", fd.cFileName);
FindFileInDir(tempPath, strRet);
}
else if( fd.dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE )
{
char HouZhui[5];
strncpy(HouZhui, fd.cFileName+strlen(fd.cFileName)-4, 4);
HouZhui[4] = '\0';
char *StrTemp = _strupr(HouZhui);
if( strcmp(StrTemp, ".TXT")==0 )
{
strcpy(tempPath, rootDir);
strcat(tempPath, "\\\\");
strcat(tempPath, fd.cFileName);
// printf("%s\r\n", tempPath);
readOneFile(tempPath);
strcpy(fname, fd.cFileName);
strcat(strRet+strRet[strlen(strRet)], fname);
}
}
while(!bSearchFindShed)
{
if(FindNextFile(h_Search, &fd))
{
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY &&
strcmp(fd.cFileName, "..")!=0 && strcmp(fd.cFileName, ".")!=0)
{
strcpy(tempPath,rootDir);
strcat(tempPath, "\\\\");
strcat(tempPath,fd.cFileName);
FindFileInDir(tempPath,strRet);
}
else if(fd.dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE)
{
char HouZhui[5];
strncpy(HouZhui, fd.cFileName+strlen(fd.cFileName)-4, 4);
HouZhui[4] = '\0';
char *StrTemp = strupr(HouZhui);
if( strcmp(StrTemp, ".TXT")==0 )
{
strcpy(tempPath, rootDir);
strcat(tempPath, "\\\\");
strcat(tempPath, fd.cFileName);
//printf("%s\r\n", tempPath);
readOneFile(tempPath);
strcpy(fname, fd.cFileName);
strcat(strRet+strRet[strlen(strRet)], fname);
}
}
}
else
{
bSearchFindShed = TRUE;
}
}
FindClose(h_Search);
}
int main(int argc,char *argv[])
{
char *strPath = "E:\\\\h";
char FilePath[1024];
ZeroMemory(FilePath, 1024);
FindFileInDir(strPath, FilePath);
// readOneFile();
return 0;
}