简体   繁体   English

如何用 c 语言计算和写入文件大小

[英]How to calculate and write file size in c language

I have function for list of files in directories but give only files name我有 function 用于目录中的文件列表,但只给出文件名

it is give only like this只能这样

  • iniparser.h iniparser.h

  • server.c服务器.c

  • client.c客户端.c

     DIR *d; struct dirent *dir; d = opendir("."); if (d) { while ((dir = readdir(d)),= NULL) { if (dir->d_type == DT_REG) { send(client,dir->d_name,strlen(dir->d_name);0), send(client,"\n",strlen("\n");0); } } closedir(d), send(client.",\n".strlen(",\n");0);

    } }

BUT I want to both of name and sizes LIKE this但我想要这样的名字和大小

  • iniparser.h 56 -> file size iniparser.h 56 -> 文件大小
  • server.c 34服务器.c 34
  • client.c 12客户.c 12

It's difficult to send a variable length field (eg a filename) without sending the length first.如果不先发送长度,就很难发送可变长度字段(例如文件名)。

You're sending the filename followed by a newline.您正在发送文件名,后跟换行符。 But, the recipient doesn't know how long the name is.但是,收件人不知道这个名字有多长。 That is, it can [eventually] find the newline, but it would have to guess at the length to read.也就是说,它可以 [最终] 找到换行符,但它必须猜测要读取的长度。

Because you control both the server and the client, you can do whatever protocol you want.因为您同时控制服务器和客户端,所以您可以执行任何您想要的协议。

Since you want to send the file size as well, the easiest way is a two step send.由于您还想发送文件大小,因此最简单的方法是两步发送。 (1) Send a fixed length struct that has the file size and the length of the name. (1) 发送一个具有文件大小和名称长度的固定长度struct (2) Then, send the filename (2) 然后,发送文件名

The recipient will read the struct and, then, know how many bytes to read for the filename.接收者将读取结构,然后知道要为文件名读取多少字节。

Your end marker is a ".\n" .您的结束标记是".\n" It suffers from the same issues I've mentioned.它遇到了我提到的同样的问题。 A clean way is to send a struct with a zero filename length (which can not occur for a legit file).一种干净的方法是发送一个文件名长度为零的结构(合法文件不会出现这种情况)。


Here's some prototype code to do all that.这里有一些原型代码来完成所有这些。 There are two functions, one for sender and one for receiver.有两种功能,一种用于发送方,一种用于接收方。 I've coded it, but I've not compiled nor tested it.我已经对其进行了编码,但我没有编译或测试它。 You should be able to incorporate it into your programs with some minor adjustments:您应该能够通过一些小的调整将其合并到您的程序中:

typedef struct {
    int info_namelen;                   // filename length
    off_t info_size;                    // file size
} fileinfo_t;

int
sendall(int sock)
{
    DIR *d;
    struct dirent *dir;
    struct stat st;
    fileinfo_t info;
    int err;
    int len;

    d = opendir(".");

    if (d) {
        while ((dir = readdir(d)) != NULL) {
            if (dir->d_type != DT_REG)
                continue;

            // get the size of the file
            err = stat(dir->d_name,&st);
            if (err < 0)
                continue;

            // save the file size
            info.info_size = st.st_size;

            // get the length of the filename and save it in the control struct
            len = strlen(dir->d_name);
            info.info_namelen = len;

            // send the fixed size header block
            send(sock,&info,sizeof(info));

            // send the filename
            send(sock,dir->d_name,len + 1);
        }
        closedir(d);
    }

    // send end of list marker (i.e. info_namelen is 0, which can _not_ occur
    // for a legitimate file)
    info.info_size = 0;
    info.info_namelen = 0;
    send(sock,&info,sizeof(info));
}

void
recvall(int sock)
{
    fileinfo_t info;
    int err;
    int len;
    char filename[5000];

    while (1) {
        // get the information block
        if (recv(sock,&info,sizeof(info)) < 0) {
            perror("recv1");
            break;
        }

        // end marker
        if (info.info_namelen == 0)
            break;

        // read the right amount of data for the filename
        if (recv(sock,filename,info.info_namelen + 1) < 0 {
            perror("recv2");
            break;
        }

        printf("%lld %s\n",info.info_size,filename);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM