如何在 C 中的 recv 中插入换行符
How to insert a newline in a recv in C
我编写此代码是为了从服务器发送文件夹中的文件内容列表,以便在客户端中查看。代码有效,但我看到所有文件都没有换行符。如何查看带有换行符或 space 的文件?
例如,现在我看到:"file1.txtfile2.txtfile3.txt" 我会看到“file1.txt
file2.txt file3.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 ... */
}
我编写此代码是为了从服务器发送文件夹中的文件内容列表,以便在客户端中查看。代码有效,但我看到所有文件都没有换行符。如何查看带有换行符或 space 的文件? 例如,现在我看到:"file1.txtfile2.txtfile3.txt" 我会看到“file1.txt file2.txt file3.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 ... */
}