gpt4 book ai didi

c - 如何在C中的recv中插入换行符

转载 作者:行者123 更新时间:2023-11-30 15:23:31 25 4
gpt4 key购买 nike

我编写此代码是为了从服务器发送文件夹中的文件内容列表,以便在客户端中查看它。该代码有效,但我看到所有文件都没有换行符。如何查看带有换行符或空格的文件?例如,现在我看到:“file1.txtfile2.txtfile3.txt”,我会看到“file1.txt”文件2.txt 文件3.txt"

谢谢!

DIR *dp;           
int rv, stop_received;
struct dirent *ep;

dp = opendir ("./");
char *newline="\n";
if (dp != NULL) {
while (ep = readdir(dp))
rv = send(conn_fd, ep->d_name, strlen(ep->d_name), 0);

(void)closedir(dp);
} else
perror ("Couldn't open the directory");
close(conn_fd);

最佳答案

简单,像这样声明换行符

char newline = '\n';

然后发送

rv = send(conn_fd, &newline, 1, 0);

因此,如果您想发送目录名称和其后的换行符,请这样做

char newline;

newline = '\n';
while (ep = readdir(dp))
{
size_t length;

length = strlen(ep->d_name);
rv = send(conn_fd, ep->d_name, length, 0);
if (rv != length)
pleaseDoSomething_ThereWasAProblem();
rv = send(conn_fd, &newline, 1, 0);
/* ... continue here ... */
}

关于c - 如何在C中的recv中插入换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28772357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com