【问题标题】:Search in a directory a file with the length equal or greater than a given int in c language in windows在目录中搜索长度等于或大于windows中c语言给定int的文件
【发布时间】:2013-10-21 19:02:36
【问题描述】:

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

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

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

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

这是“客户端”代码:

#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;
}

【问题讨论】:

  • GetFileSize 或者,一个更好的选择:FindFirstFile(更高效)
  • 但是他们是用 C 语言工作的吗?我也找到了它们……但上面写着“C++”……
  • 见下面的例子。我只是像在我的 C 编译器中一样编译它。在 wfd 结构中查找文件属性。当然,请确保您将其传递给您自己的目录路径 :)
  • 任何反馈Tripon - 这对你有用吗?

标签: c windows named-pipes filesize


【解决方案1】:

FindFirstFile() 和 FindNextFile() 遍历文件...支持通配符。完成后调用 FindClose() http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx

【讨论】:

    【解决方案2】:

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

    查看下面的其他 cmets 代码示例

    #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;
    }
    

    文件属性将为您提供文件名和大小等。

    您可以然后将 100 个字节写入 char 缓冲区,方法是将找到的文件的名称和路径传递给 fopen(): FILE *fp = fopen(fileNamePath, "r"); 然后类似 char buf[101];for(i=0;i ***用完fp后不要忘记使用fclose()

    【讨论】:

    • 我是 c 的初学者...所以很难理解上面的代码中发生了什么,但我会再试一次...谢谢。或者如果你能用一些话来解释我它的作用,那将对我有很大帮助。
    • @TriponIonut - 您最初的问题是如何确定找到的文件的大小,然后返回前 n 个字节,其中 n 是调用函数的输入,我相信。 FindFirstFile()FindNextFile() 函数 [几乎] 总是相互配合,共同提供您所要求的信息。我提供的代码示例(和链接)提供的详细信息足以让您轻松上手。如果您还有其他问题,请发布它,并附上一些示例代码来展示您的努力,我敢打赌在第一个小时内会有 10 次浏览。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 2016-10-26
    • 2010-09-05
    相关资源
    最近更新 更多