readFile 把数据读到缓冲区 怎样取换行符

wyqiang 2006-06-17 01:12:23
实现类似于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;
}
...全文
672 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
healer_kx 2006-06-19
  • 打赏
  • 举报
回复
一个字符一个字符的读, 读 \n就是一行了,如果后面存在\r,忽略它即可。
wyqiang 2006-06-18
  • 打赏
  • 举报
回复
/*
6.文件操作(*)

实现类似于 的搜索程序。在指定目录的所有文件中搜寻指定的字符串。


假设:这个目录下的所有文件均为文本文件;并且目录深度不超过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>

int length(char T[])
{
int i = 0;
while(T[i] != '\0')
i++;
return i;
}

void readOneFile(char *strFile,char *strMatch)
{
unsigned long i = 0;
int line = 1;
int j = 0;
BOOL bResult;
HANDLE handle;
char inBuffer[1024];
char upBuffer[1024];
char upMatch[20];
strcpy(upMatch, strupr(strMatch));
ZeroMemory(inBuffer,1024);
DWORD nBytesRead=0;
handle = CreateFile(strFile,GENERIC_READ,
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] != 0x0d)
{
while(upBuffer[i] == upMatch[j])
{
i++;
j++;
if(j == length(upMatch))
printf("the line is:%d the file name is: %s\r\n",line,strFile);
}
j=0;
i++;

if(i >= nBytesRead)
break;
}
i++;
if(upBuffer[i] == 0x0a)
{
line++;
i++;
}
}
ZeroMemory(inBuffer,1024);
nBytesRead=0;
i=0;
bResult = ReadFile(handle, &inBuffer,1024, &nBytesRead, NULL);
}

CloseHandle(handle);
return;
}


void FindFileInDir( const char *rootDir, char *strRet, char *strMatch)
{
char fname[256];
ZeroMemory(fname, 256);
WIN32_FIND_DATA fd;
ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));

HANDLE h_Search;
char filePathName[256];
char tempPath[256];
char szTempRootDir[256];
ZeroMemory(filePathName, 256);
ZeroMemory(tempPath, 256);

strcpy(filePathName, rootDir);
strcpy(szTempRootDir, 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, strMatch);

}
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);
//只读 *.TXT文件
if( strcmp(StrTemp, ".TXT")==0 )
{
strcpy(tempPath, rootDir);
strcat(tempPath, "\\");
strcat(tempPath, fd.cFileName);
// printf("%s\r\n", tempPath);
readOneFile(tempPath,strMatch);
strcpy(fname, fd.cFileName);
strcat(strRet+strlen(strRet), "\\");
strcat(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, strMatch);
}
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 )
{
ZeroMemory(tempPath, 256);
strcpy(tempPath, rootDir);
strcat(tempPath, "\\");
strcat(tempPath, fd.cFileName);
// printf("%s\r\n", tempPath);
readOneFile(tempPath,strMatch);
strcpy(fname, fd.cFileName);
strcat(strRet+strlen(strRet), "\\");
strcat(strRet+strlen(strRet), fname);
}
}
}
else
{
bSearchFindShed = TRUE;
}
}

FindClose(h_Search);
}

int main(int argc,char *argv[])
{
char rootDir[256] = {0};
char strRet[1024] = {0};
char strMatch[20] = {0};
printf("please input the string!\r\n");
scanf("%s",strMatch);

printf("please input the Directory!\r\n");
scanf("%s", rootDir);

FindFileInDir(rootDir, strRet, strMatch);
return 0;
}
/*
please input the string!
MAIN
please input the Directory!
C:\HELLO
the line is:2 the file name is: C:\HELLO\sort.txt
the line is:4 the file name is: C:\HELLO\sort.txt
the line is:5 the file name is: C:\HELLO\sort.txt
the line is:2 the file name is: C:\HELLO\pp.txt
the line is:5 the file name is: C:\HELLO\?? ???? (2).txt
the line is:41 the file name is: C:\HELLO\?? ???? (2).txt
the line is:139 the file name is: C:\HELLO\hello.txt
the line is:2 the file name is: C:\HELLO\?????\add.txt
the line is:2 the file name is: C:\HELLO\?????\pp.txt
the line is:5 the file name is: C:\HELLO\?????\?? ???? (2).txt
the line is:41 the file name is: C:\HELLO\?????\?? ???? (2).txt
Press any key to continue
*/
力为 2006-06-17
  • 打赏
  • 举报
回复
这么长!

问题在哪一行?
wyqiang 2006-06-17
  • 打赏
  • 举报
回复
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int length(char T[])
{
int i = 0;
while(T[i] != '\0')
i++;
return i;
}

void readOneFile(char *strFile,char *strMatch)
{
unsigned long i = 0;
int line = 1;
int j = 0;
BOOL bResult;
HANDLE handle;
char inBuffer[1024];
char upBuffer[1024];
char upMatch[20];
strcpy(upMatch, strupr(strMatch));
ZeroMemory(inBuffer,1024);
DWORD nBytesRead=0;
handle = CreateFile(strFile,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] != 0x0d)
{
while(upBuffer[i] == upMatch[j])
{
i++;
j++;
if(j == length(upMatch))
printf("%d,%s\r\n",line,upMatch);
}
j=0;
i++;

if(i >= nBytesRead)
break;
}
i++;
if(upBuffer[i] == 0x0a)
{
line++;
i++;
}
}
ZeroMemory(upBuffer,1024);
printf("%s",upBuffer);
//×?ò?D?????μ??ˉ×÷
ZeroMemory(inBuffer,1024);
nBytesRead=0;
i=0;
bResult = ReadFile(handle, &inBuffer,1024, &nBytesRead, NULL);
}

CloseHandle(handle);
return;
}


void FindFileInDir(const char *rootDir, char *strRet,char *strMatch)
{
char fname[50];
ZeroMemory(fname, 50);
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,strMatch);

}
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,strMatch);
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);
//strcpy(filePathName,rootDir);
strcat(tempPath, "\\");
strcat(tempPath,fd.cFileName);
FindFileInDir(tempPath,strRet,strMatch);
}
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 )
{
ZeroMemory(tempPath, 256);
strcpy(tempPath, rootDir);
//strcpy(filePathName,rootDir);
strcat(tempPath, "\\\\");
strcat(tempPath, fd.cFileName);
printf("%s\r\n", tempPath);
// readOneFile(tempPath,strMatch);
strcpy(fname, fd.cFileName);
strcat(strRet+strRet[strlen(strRet)], fname);

}
}
}
else
{
bSearchFindShed = TRUE;
}
}

FindClose(h_Search);
}

int main(int argc,char *argv[])
{
/*
char rootDir[50];// = "E:\\\\h";
char strRet[1024];
char strMatch[20];
printf("please input the string!\r\n");
scanf("%s",strMatch);

printf("please input the Directory!\r\n");
scanf("%s",rootDir);

FindFileInDir(rootDir, strRet,strMatch);
*/
char strMatch[20];
printf("please input the string!\r\n");
scanf("%s",strMatch);

char strFile[20];
printf("please input the string!\r\n");
scanf("%s",strFile);

readOneFile(strFile,strMatch);
return 0;
}

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧