0

我正在尝试在 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;
}
4

2 回答 2

0

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

于 2013-10-21T19:15:42.983 回答
0

这是使用来自cprogramming的 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;
}

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

然后,您可以通过将找到的文件的名称和路径传递给 char 缓冲区,将100个字节写入 char buf[101];for(i=0;i<100;i++){buf[i] = fgetc(fp);} ***完成后不要忘记使用.fopen()FILE *fp = fopen(fileNamePath, "r");
fclose()fp

于 2013-10-21T19:16:08.570 回答