sscanf,sprintf,fread,fwrite

本文详细介绍了sscanf、sprintf、fread及fwrite等函数的功能与用法。sscanf用于从固定字符串中读取格式化数据;sprintf则负责将格式化的数据写入字符串缓冲区;fread和fwrite分别用于从文件流中读取数据和向指定文件写入数据块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sscanf 读取格式化的字符串中的数据

定义函数 int sscanf (const char *str,const char * format,........);     

sscanf与scanf类似,都是用于输入,子是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。


sprintf 指的是字符串格式化命令,主要功能是把格式化的数据写入某个字符串缓冲区中。

定义函数 int sprintf( char *buffer, const char *format, [ argument] … );

sprintf 是个变参函数。使用sprintf 对于写入buffer的字符数是没有限制的,这就存在了buffer溢出的可能性。


fread

size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;

fread 是一个函数,它从文件流中读数据,最多读取count个项,每个项size个字节,如果调用成功返回实际读取到的项个数(小于或等于count),如果不成功或读到文件末尾返回 0。


fwrite

size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);

fwrite() 是 C 语言标准库中的一个文件处理函数,功能是向指定的文件中写入若干数据块,如成功执行则返回实际写入的数据块数目。该函数以二进制形式对文件进行操作,不局限于文本文件。

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <sys/socket.h> #include <netinet/in.h> #include <dirent.h> #include <signal.h> #define PORT 8888 #define WEB_ROOT “./web” #define MAX_THREADS 100 void log_request(const char *method, const char *path, int status) { printf(“[%s] %s -> %d\n”, method, path, status); } void send_response(int sock, int status, const char *content_type, const char *body, int body_len) { char header[512]; sprintf(header, “HTTP/1.1 %d OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n”, status, content_type, body_len); size_t result = 0; result = send(sock, header, strlen(header), 0); if (result == -1) { close(sock); } result = send(sock, body, body_len, 0); if (result == -1) { close(sock); } } void *handle_request(void *arg) { int sock = *(int *)arg; char buffer[4096]; recv(sock, buffer, sizeof(buffer), 0); char method[16], path[256]; sscanf(buffer, "%s %s", method, path); if (strcmp(method, "GET") == 0) { char filepath[512]; if (strcmp(path, "/") == 0) { strcpy(path, "/Index.html"); } sprintf(filepath, "%s%s", WEB_ROOT, path); FILE *file = fopen(filepath, "rb"); if (file) { fseek(file, 0, SEEK_END); long len = ftell(file); fseek(file, 0, SEEK_SET); char *content = malloc(len); fread(content, 1, len, file); fclose(file); const char *content_type = "text/html"; if (strstr(path, ".css")) { content_type = "text/css"; } else if (strstr(path, ".js")) { content_type = "application/json"; } else if (strstr(path, ".png")) { content_type = "image/png"; } else if (strstr(path, ".jpg")) { content_type = "image/jpeg"; } send_response(sock, 200, content_type, content, len); free(content); log_request(method, path, 200); } else { const char *not_found = "<h1>404 Not Found</h1>"; send_response(sock, 404, "text/html", not_found, strlen(not_found)); log_request(method, path, 404); } } close(sock); return NULL; } int main() { signal(SIGPIPE, SIG_IGN); int server_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(PORT), .sin_addr.s_addr = htonl(INADDR_ANY)}; bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)); listen(server_fd, 10); printf("Threaded server running on port %d\n", PORT); while (1) { int client_fd = accept(server_fd, NULL, NULL); pthread_t tid; int *sock_ptr = malloc(sizeof(int)); *sock_ptr = client_fd; pthread_create(&tid, NULL, handle_request, sock_ptr); pthread_detach(tid); } return 0; } 在此基础上拓展post功能
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值