C函数,打印到缓冲区

加里·克罗斯

我有一个客户端/服务器应用程序,并且在那里出现错误-> perror(“ [服务器]无法将消息发送到客户端。\ n“)。因此服务器无法发送msgrasp(缓冲区)。如果可以的话,我将不胜感激。

void printdir(char *dir, int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    if((dp = opendir(dir)) == NULL) {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode)) {
            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 ||
                strcmp("..",entry->d_name) == 0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth+4);
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}

int printForClient(int fd)
{
  char buffer[100];
  int bytes;
  char msg[100];
  char *msgrasp=NULL;

  bytes = read (fd, msg, sizeof (buffer));
  if (bytes < 0)
    {
      perror ("Can't read from client.\n");
      return 0;
    }
  printf ("[server]..%s\n", msg);
  printdir(&msgrasp,msj,0);    
  printf("[server]%s\n",msgrasp);

  if (bytes && write (fd, msgrasp, bytes) < 0)
    {
      perror ("[server] Can't send the message to client.\n");
      return 0;
    }

  return bytes;
}
伊哈罗布·阿西米(Iharob Al Asimi)

这不是一个很好的主意,但是由于这是您在此处所要求的,因此您有了动态内存分配的解决方案

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

void printdir(char **output, const char *const dir, int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;

    if ((dp = opendir(dir)) == NULL)
    {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);

    while ((entry = readdir(dp)) != NULL)
    {
        /* check if stat succeeded */
        if (lstat(entry->d_name, &statbuf) == -1)
            continue;
        if (S_ISDIR(statbuf.st_mode) != 0)
        {
            char *buffer;
            size_t length;

            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 ||
                strcmp("..",entry->d_name) == 0)
                continue;
            length = 4 + depth + strlen(entry->d_name);
            if (*output != NULL)
                length += strlen(*output);
            buffer = realloc(*output, length);
            if (buffer != NULL)
            {
                char current[length];

                if (*output == NULL)
                    buffer[0] = '\0';
                *output = buffer;

                snprintf(current, length, "%*s%s/\n", depth, " ", entry->d_name);
                strcat(*output, current);
                //printf("%*s%s/\n",depth,"",entry->d_name);
                /* Recurse at a new indent level */
            }
            else
            {
                fprintf(stderr, "Out of memory\n");

                free(*output);
                *output = NULL;

                closedir(dp);
                return;
            }
            printdir(output, entry->d_name, depth + 4);
        }
        else
        {
            char  *buffer;
            size_t length;

            length = 4 + depth + strlen(entry->d_name);
            if (*output != NULL)
                length += strlen(*output);
            buffer = realloc(*output, length);
            if (buffer != NULL)
            {
                char current[length];

                if (*output == NULL)
                    buffer[0] = '\0';
                *output = buffer;

                snprintf(current, length, "%*s%s/\n", depth, " ", entry->d_name);
                strcat(*output, current);
            }
            else
            {
                fprintf(stderr, "Out of memory\n");

                free(*output);
                *output = NULL;

                closedir(dp);
                return;
            }
        }
    }
    chdir("..");

    closedir(dp);
}

int main()
{
    char *buffer = NULL; //buffer for printdir output.

    printf("Directory scan of /home:\n");
    printdir(&buffer, "/home", 0);
    printf("done.\n");

    printf("%s\n", buffer);
    free(buffer);

    exit(0);
}

这不是一个好主意,因为您不知道是否有足够的内存来容纳所有文本,它可能会增长很多,我在这段代码中针对这种情况添加了检查,以防出现内存不足的情况,此函数将中止读取,释放资源并返回。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章