C函数,打印到缓冲区

时间:2014-12-23 17:44:21

标签: c unix

我有一个客户端/服务器应用程序,我在那里收到错误 - > 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;
}

2 个答案:

答案 0 :(得分:0)

我建议添加一个char *类型的参数(一个指向char数组的指针),并在每次递归时推进指针。我的尝试(假设缓冲区足够输出):

void printdir(char *dir, int depth, char* output)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    char tmp[100];
    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;
            sprintf(tmp, "%*s%s/\n",depth,"",entry->d_name);
            strcpy(buffer, tmp);
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth+4, buffer+strlen(tmp));
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}

int main()
{   char buffer[1000]; //buffer for printdir output.
    printf("Directory scan of /home:\n");
    printdir("/home",0);
    printf("done.\n");

    exit(0);
}

您可能希望在每次执行之间添加\n,具体取决于您希望对输出执行的操作。请注意,在此程序中,每个字符串末尾的\0都将被删除。

答案 1 :(得分:0)

这不是一个很好的主意,但是因为这是你在这里要求的,你有一个动态内存分配的解决方案

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

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