在windows中用c语言在目录中搜索长度等于或大于给定整数的文件

在windows中用c语言在目录中搜索长度等于或大于给定整数的文件,c,windows,named-pipes,filesize,C,Windows,Named Pipes,Filesize,我试图在C中创建一个命名的Windows管道。该管道由用户和服务器使用。用户通过管道发送一个随机整数。然后服务器在其当前目录中搜索一个大小大于或等于接收到的整数的文件,然后将文件名和文件中最多100字节的内容发送回用户 我的问题是,我不知道如何验证目录中每个文件的大小,然后返回文件名和100字节 这是我试图用来测量文件大小的函数: 这是“客户端”代码: #包括 #包括 #定义MaxLine 100 int main(int argc,char*argv[]){ char rcvMsg[100];

我试图在C中创建一个命名的Windows管道。该管道由用户和服务器使用。用户通过管道发送一个随机整数。然后服务器在其当前目录中搜索一个大小大于或等于接收到的整数的文件,然后将文件名和文件中最多100字节的内容发送回用户

我的问题是,我不知道如何验证目录中每个文件的大小,然后返回文件名和100字节

这是我试图用来测量文件大小的函数:

这是“客户端”代码:

#包括
#包括
#定义MaxLine 100
int main(int argc,char*argv[]){
char rcvMsg[100];
char-sndMsg[100];
德沃德·雷兹;
句柄readHandle,writeHandle;
int r=rand();
char-str[15];
sprintf(str,“%d”,r);
writeHandle=CreateFile(“\\.\\PIPE\\FirstPipe”),通用写入,文件共享写入,
空,打开(存在,0,空);
readHandle=CreateFile(“\\.\\PIPE\\SecondPipe”),通用\u读取,文件\u共享\u读取,
空,打开(存在,0,空);
strcpy(sndMsg,r);
printf(“向服务器发送消息:%s\n”,sndMsg);
WriteFile(writeHandle,sndMsg,strlen(sndMsg),&rez,NULL);
printf(“消息已发送!正在等待服务器读取消息!\n”);
printf(“正在等待服务器在管道中写入…\n”);
ReadFile(readHandle,rcvMsg,100,&rez,NULL);
printf(“客户端重新接收的消息:%s\n”,rcvMsg);
CloseHandle(readHandle);
闭合手柄(书写手柄);
返回1;
}
这是“服务器”代码,但文件解析部分除外:

>

#包括
#包括
#定义MaxLine 100
//文件长度
int fsize(字符*文件)
{
文件*f=fopen(文件“r”);
fseek(f,0,SEEK_END);
int len=(无符号长)ftell(f);
fclose(f);
回程透镜;
}
int main(int argc,char*argv[]){
char rcvMsg[100];
char-sndMsg[100];
德沃德·雷兹;
句柄readHandle,writeHandle;
readHandle=CreateNamedPipe(“\\\.\\PIPE\\FirstPipe”,管道访问入站,
管道类型(字节)(管道等待,3,0,0,0,空);
writeHandle=CreateNamedPipe(“\\\.\\PIPE\\SecondPipe”,管道访问出站,
管道类型(字节)(管道等待,3,0,0,0,NULL);
printf(“正在等待客户端连接…\n”);
ConnectNamedPipe(writeHandle,NULL);
ConnectNamedPipe(readHandle,NULL);
printf(“等待客户端在管道中写入…\n”);
ReadFile(readHandle,rcvMsg,100,&rez,NULL);
printf(“服务器接收到的消息:%s\n”,rcvMsg);
int num=atoi(rcvMsg);//我要查找的文件长度
//我应该在这里处理这些文件
strcpy(sndMsg,“文件名+100字节”);
printf(“服务器发送应答:%s\n”,sndMsg);
WriteFile(writeHandle,sndMsg,strlen(sndMsg),&rez,NULL);
printf(“等待客户端读取消息…\n”);
//断开和关闭手柄
断开NamedPipe(写句柄);
闭合手柄(书写手柄);
断开NamedPipe(readHandle);
CloseHandle(readHandle);
返回1;
}
FindFirstFile()和FindNextFile()来迭代文件。。。支持通配符。完成后调用FindClose()

以下是使用来自的FindFirstFile的示例代码:(在wfd结构中查找大小属性)

参见下面代码示例的其他注释

#include <stdio.h>
#include <string.h>
#include <windows.h>

void find(char* path,char* file)  //Note: pass the path string appropriate for your scenario
{
    static int found =0;
    HANDLE fh;
    WIN32_FIND_DATA wfd;
    int i=0;
    int j=0;
    fh=FindFirstFile(path,&wfd);
    if(fh)
    {
        if(strcmp(wfd.cFileName,file)==0)
        {
            path[strlen(path)-3]='\0';
            strcat(path,file);
            FindClose(fh);
            return;
        }
        else
        {
            while(FindNextFile(fh,&wfd) && found ==0)
            {              
                if(strcmp(wfd.cFileName,file)==0)
                {
                    path[strlen(path)-3]='\0';
                    strcat(path,file);
                    FindClose(fh);
                    found =1;
                    return;
                }
                if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
                    strcmp(wfd.cFileName,"..")!=0 && strcmp(wfd.cFileName,".")!=0)
                {
                    path[strlen(path)-3]='\0';
                    strcat(path,wfd.cFileName);
                    strcat(path,"\\*.*");
                    find(path,file);
                }
            }

            if(found==0)
                {
                for(i=strlen(path)-1;i>0;i--)
                {
                    if(j==1 && path[i]=='\\')
                    {
                        path[i]='\0';
                        strcat(path,"\\*.*");
                        break;
                    }
                    if(path[i]=='\\')
                        j=1;
                }
            }
        }
        FindClose(fh);
    }



}

int main()
{
    TCHAR path[512] = "C:\\*.*";  //Change this as necessary 
    find(path,"notepad.exe");
    printf("%s\n",path);

    return 0;
}
#包括
#包括
#包括
void find(char*path,char*file)//注意:传递适合您场景的路径字符串
{
找到的静态int=0;
处理跳频;
WIN32_查找_数据wfd;
int i=0;
int j=0;
fh=FindFirstFile(路径和wfd);
如果(fh)
{
if(strcmp(wfd.cFileName,file)==0)
{
路径[strlen(path)-3]='\0';
strcat(路径、文件);
FindClose(fh);
返回;
}
其他的
{
while(FindNextFile(fh和wfd)和&found==0)
{              
if(strcmp(wfd.cFileName,file)==0)
{
路径[strlen(path)-3]='\0';
strcat(路径、文件);
FindClose(fh);
发现=1;
返回;
}
if(wfd.dwFileAttributes和文件属性目录&&
strcmp(wfd.cFileName,“…”)!=0和strcmp(wfd.cFileName,“.”!=0)
{
路径[strlen(path)-3]='\0';
strcat(路径,wfd.cFileName);
strcat(路径,\\*.*);
查找(路径、文件);
}
}
如果(找到==0)
{
对于(i=strlen(路径)-1;i>0;i--)
{
如果(j==1&&path[i]='\\')
{
路径[i]='\0';
strcat(路径,\\*.*);
打破
}
如果(路径[i]='\\')
j=1;
}
}
}
FindClose(fh);
}
}
int main()
{
TCHAR路径[512]=“C:\\*.*;//根据需要更改此路径
查找(路径“notepad.exe”);
printf(“%s\n”,路径);
返回0;
}
文件属性将提供文件名和大小等


通过将找到的文件的名称和路径传递到
fopen()
file*fp=fopen(fileNamePath,“r”);您可以然后将100个字节写入字符缓冲区然后类似于char buf[101];对于(i=0;i或,一个更好的选项:(更有效)但它们在C中工作吗?我也发现了它们…但它上面写着“C++”…请参见下面的示例。我只是像在我的C编译器中一样编译它。在wfd结构中查找文件属性。当然,确保您将自己的目录路径传递给它:)任何反馈Tripon-这对您有用吗?
#include <stdio.h>
#include <windows.h>
#define MAXLINIE 100
int main(int argc, char* argv[]) {
    char rcvMsg[100];
    char sndMsg[100];
    DWORD rez;
    HANDLE readHandle, writeHandle;

    int r = rand();
    char str[15];
    sprintf(str, "%d", r);

    writeHandle = CreateFile("\\\\.\\PIPE\\FirstPipe", GENERIC_WRITE, FILE_SHARE_WRITE,
                   NULL, OPEN_EXISTING, 0, NULL);

    readHandle = CreateFile("\\\\.\\PIPE\\SecondPipe",GENERIC_READ, FILE_SHARE_READ,
                   NULL, OPEN_EXISTING, 0, NULL);

    strcpy(sndMsg, r);
    printf("Sending message to server: %s\n", sndMsg);
    WriteFile(writeHandle, sndMsg, strlen(sndMsg), &rez, NULL);
    printf("Message sent! Waiting for server to read the message!\n");

    printf("Waiting for SERVER to write in the pipe...\n");
    ReadFile(readHandle, rcvMsg, 100, &rez, NULL);  
    printf("Client revceived message: %s\n", rcvMsg);

    CloseHandle(readHandle);
    CloseHandle(writeHandle);

    return 1;

}
 #include <stdio.h>
#include <windows.h>
#define MAXLINIE 100

    //file lenght
int fsize(char* file)
    {
    FILE * f = fopen(file, "r");
    fseek(f, 0, SEEK_END);
    int len = (unsigned long)ftell(f);
    fclose(f);
    return len;
    }


int main(int argc, char* argv[]) {
    char rcvMsg[100];
    char sndMsg[100];
    DWORD rez;
    HANDLE readHandle, writeHandle;

    readHandle = CreateNamedPipe("\\\\.\\PIPE\\FirstPipe", PIPE_ACCESS_INBOUND, 
                        PIPE_TYPE_BYTE|PIPE_WAIT,3,0,0,0,NULL);

    writeHandle = CreateNamedPipe("\\\\.\\PIPE\\SecondPipe", PIPE_ACCESS_OUTBOUND,
                        PIPE_TYPE_BYTE|PIPE_WAIT, 3, 0, 0, 0, NULL);    

    printf("Waiting for clients to connect...\n");
    ConnectNamedPipe(writeHandle, NULL);
        ConnectNamedPipe(readHandle, NULL);

    printf("Waiting for a CLIENT to write in the pipe...\n");
    ReadFile(readHandle, rcvMsg, 100, &rez, NULL);  
    printf("Server revceived message: %s\n", rcvMsg);

    int num = atoi(rcvMsg); //the file lenght i'm looking for

    //here i should process the files

    strcpy(sndMsg, "File_name + 100 bytes");
    printf("Server sends an answer: %s\n", sndMsg);
    WriteFile(writeHandle, sndMsg, strlen(sndMsg), &rez, NULL);
    printf("Waiting for client to read the message...\n");

    // disconnecting and closing the handles
    DisconnectNamedPipe(writeHandle);
    CloseHandle(writeHandle);

    DisconnectNamedPipe(readHandle);
    CloseHandle(readHandle);

    return 1;
}
#include <stdio.h>
#include <string.h>
#include <windows.h>

void find(char* path,char* file)  //Note: pass the path string appropriate for your scenario
{
    static int found =0;
    HANDLE fh;
    WIN32_FIND_DATA wfd;
    int i=0;
    int j=0;
    fh=FindFirstFile(path,&wfd);
    if(fh)
    {
        if(strcmp(wfd.cFileName,file)==0)
        {
            path[strlen(path)-3]='\0';
            strcat(path,file);
            FindClose(fh);
            return;
        }
        else
        {
            while(FindNextFile(fh,&wfd) && found ==0)
            {              
                if(strcmp(wfd.cFileName,file)==0)
                {
                    path[strlen(path)-3]='\0';
                    strcat(path,file);
                    FindClose(fh);
                    found =1;
                    return;
                }
                if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
                    strcmp(wfd.cFileName,"..")!=0 && strcmp(wfd.cFileName,".")!=0)
                {
                    path[strlen(path)-3]='\0';
                    strcat(path,wfd.cFileName);
                    strcat(path,"\\*.*");
                    find(path,file);
                }
            }

            if(found==0)
                {
                for(i=strlen(path)-1;i>0;i--)
                {
                    if(j==1 && path[i]=='\\')
                    {
                        path[i]='\0';
                        strcat(path,"\\*.*");
                        break;
                    }
                    if(path[i]=='\\')
                        j=1;
                }
            }
        }
        FindClose(fh);
    }



}

int main()
{
    TCHAR path[512] = "C:\\*.*";  //Change this as necessary 
    find(path,"notepad.exe");
    printf("%s\n",path);

    return 0;
}