字符数组输出烫烫烫
时间: 2024-02-23 15:57:51 浏览: 64
可以使用以下代码实现:
```c++
#include <iostream>
#include <cstring>
int main() {
char str[] = {'\xe7', '\83', '\xab', '\xe7', '\83', '\xab', '\xe7', '\83', '\xab', '\0'};
std::cout << str << std::endl;
return 0;
}
```
其中,字符数组 `str` 存储了烫烫烫三个汉字的 UTF-8 编码。使用 `std::cout` 输出即可得到烫烫烫的结果。
相关问题
C语言通过socket发送接收二维字符串数组,二维字符串数组长度不确定,并将二维字符串数组输出,给出具体代码。
以下是一个示例代码,可以通过socket发送接收二维字符串数组,并将其输出:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
char **str_array = NULL; // 二维字符串数组
int rows = 0; // 二维数组的行数
// 创建socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置socket选项
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// 绑定socket
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听socket
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
// 接收客户端连接
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// 接收二维字符串数组的行数
if ((valread = read(new_socket, buffer, 1024)) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
sscanf(buffer, "%d", &rows); // 将字符串转换为整数
// 动态分配二维字符串数组
str_array = (char **)malloc(rows * sizeof(char *));
for (int i = 0; i < rows; i++) {
str_array[i] = (char *)malloc(1024 * sizeof(char));
}
// 接收二维字符串数组的内容
for (int i = 0; i < rows; i++) {
if ((valread = read(new_socket, str_array[i], 1024)) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
}
// 输出二维字符串数组
printf("Received string array:\n");
for (int i = 0; i < rows; i++) {
printf("%s\n", str_array[i]);
}
// 发送响应
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
// 释放内存
for (int i = 0; i < rows; i++) {
free(str_array[i]);
}
free(str_array);
return 0;
}
```
客户端代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char **str_array = NULL; // 二维字符串数组
int rows = 3; // 二维数组的行数
// 动态分配二维字符串数组
str_array = (char **)malloc(rows * sizeof(char *));
for (int i = 0; i < rows; i++) {
str_array[i] = (char *)malloc(1024 * sizeof(char));
sprintf(str_array[i], "String%d", i+1); // 生成测试数据
}
// 创建socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// 将IP地址转换为网络字节序
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
// 连接服务器
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
// 发送二维字符串数组的行数
char rows_str[10];
sprintf(rows_str, "%d", rows);
send(sock, rows_str, strlen(rows_str), 0);
// 发送二维字符串数组的内容
for (int i = 0; i < rows; i++) {
send(sock, str_array[i], strlen(str_array[i]), 0);
}
// 接收响应
char buffer[1024] = {0};
if ((valread = read(sock, buffer, 1024)) < 0) {
perror("read");
exit(EXIT_FAILURE);
}
printf("%s\n", buffer);
// 释放内存
for (int i = 0; i < rows; i++) {
free(str_array[i]);
}
free(str_array);
return 0;
}
```
在Windows环境下C语言通过socket发送接收二维字符串数组,二维字符串数组长度不确定,并将二维字符串数组输出,给出具体代码。
以下是一个简单的示例代码,它通过 socket 在 Windows 环境下发送接收二维字符串数组,并将其输出:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") // 链接 ws2_32.dll 库
#define PORT 8888
#define MAX_BUFFER_SIZE 1024
int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int clientAddrLen = sizeof(clientAddr);
char buffer[MAX_BUFFER_SIZE];
char **stringArray;
int i, j, rows, cols, len;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
printf("Create socket failed.\n");
return 1;
}
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Bind socket failed.\n");
closesocket(serverSocket);
return 1;
}
if (listen(serverSocket, 1) == SOCKET_ERROR) {
printf("Listen socket failed.\n");
closesocket(serverSocket);
return 1;
}
printf("Server is listening on port %d...\n", PORT);
clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET) {
printf("Accept socket failed.\n");
closesocket(serverSocket);
return 1;
}
printf("Client connected.\n");
// 接收行数和列数
recv(clientSocket, buffer, MAX_BUFFER_SIZE, 0);
sscanf(buffer, "%d%d", &rows, &cols);
// 动态分配二维数组内存
stringArray = (char **)malloc(rows * sizeof(char *));
for (i = 0; i < rows; i++) {
stringArray[i] = (char *)malloc(cols * sizeof(char));
}
// 接收二维数组数据
for (i = 0; i < rows; i++) {
recv(clientSocket, stringArray[i], cols, 0);
}
// 输出二维数组
printf("Received 2D array:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%c", stringArray[i][j]);
}
printf("\n");
}
// 释放内存
for (i = 0; i < rows; i++) {
free(stringArray[i]);
}
free(stringArray);
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
```
在上面的代码中,我们通过 socket 建立了一个 TCP 服务器,并等待客户端的连接。客户端连接后,服务器会先接收一行字符串,其中包含了二维数组的行数和列数。然后服务器会动态分配内存来存储二维数组,并接收客户端发送的二维数组数据。最后服务器将二维数组输出,并释放内存。
下面是客户端的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") // 链接 ws2_32.dll 库
#define SERVER_IP "127.0.0.1"
#define PORT 8888
#define MAX_BUFFER_SIZE 1024
int main() {
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddr;
char buffer[MAX_BUFFER_SIZE];
char **stringArray;
int i, j, rows, cols, len;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
printf("Create socket failed.\n");
return 1;
}
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(SERVER_IP);
serverAddr.sin_port = htons(PORT);
if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Connect to server failed.\n");
closesocket(clientSocket);
return 1;
}
printf("Connected to server.\n");
// 发送行数和列数
rows = 3;
cols = 4;
sprintf(buffer, "%d %d", rows, cols);
send(clientSocket, buffer, strlen(buffer), 0);
// 动态分配二维数组内存
stringArray = (char **)malloc(rows * sizeof(char *));
for (i = 0; i < rows; i++) {
stringArray[i] = (char *)malloc(cols * sizeof(char));
}
// 初始化二维数组
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
stringArray[i][j] = 'a' + i * cols + j;
}
}
// 发送二维数组数据
for (i = 0; i < rows; i++) {
send(clientSocket, stringArray[i], cols, 0);
}
// 释放内存
for (i = 0; i < rows; i++) {
free(stringArray[i]);
}
free(stringArray);
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
在上面的代码中,我们通过 socket 建立了一个 TCP 客户端,连接到了之前建立的服务器。客户端先发送一行字符串,其中包含了二维数组的行数和列数。然后客户端动态分配内存来存储二维数组,并初始化数组数据。最后客户端将二维数组数据发送给服务器,并释放内存。
注意,以上代码仅供参考,实际使用时还需要进行错误处理和防止缓冲区溢出等安全措施。
阅读全文