PHP4用户手册:函数->fputs

博客介绍了PHP中的fputs函数,它是fwrite()的别名,二者完全相同。同时提到length参数是可选的,若不指定则会写入整个字符串。

fputs

(PHP 3, PHP 4 >= 4.0.0)

fputs -- 写入到文件指针处

描述

int fputs (int fp, string str [, int length])

fputs()fwrite()的别名,它们是全部一样的。注意,length 参数是可选的,如果不指定整个字符串都将被写入。


#include "config_parser.h" #include <stdio.h> #include <stdlib.h> #include <string.h> Config *config_new() { Config *config = (Config *)malloc(sizeof(Config)); if (!config) return NULL; config->sections = NULL; return config; } void free_lines(ConfigLine *head) { while (head) { ConfigLine *tmp = head; head = head->next; free(tmp); } } void config_free(Config *config) { ConfigSection *sec = config->sections; while (sec) { ConfigSection *tmp_sec = sec; sec = sec->next; free_lines(tmp_sec->lines); free(tmp_sec); } free(config); } void add_line(ConfigLine **head, const char *line) { ConfigLine *new_line = (ConfigLine *)malloc(sizeof(ConfigLine)); if (!new_line) { fprintf(stderr, "Memory allocation failed in add_line\n"); exit(EXIT_FAILURE); // 或者返回错误码,根据你的需求 } strncpy(new_line->line, line, sizeof(new_line->line) - 1); new_line->line[sizeof(new_line->line) - 1] = '\0'; new_line->next = *head; *head = new_line; } ConfigSection *find_section(Config *config, const char *section_type, const char *key) { if (!config || !section_type) return NULL; ConfigSection *sec = config->sections; while (sec) { if (sec->type && strcmp(sec->type, section_type) == 0) { if (key == NULL && sec->name == NULL) { return sec; } else if (key && sec->name && strcmp(sec->name, key) == 0) { return sec; } } sec = sec->next; } return NULL; } ConfigSection *create_section(const char *section, const char *key) { ConfigSection *new_sec = (ConfigSection *)malloc(sizeof(ConfigSection)); if (!new_sec) { fprintf(stderr, "Memory allocation failed in create_section\n"); return NULL; } strncpy(new_sec->name, section, sizeof(new_sec->name) - 1); new_sec->name[sizeof(new_sec->name) - 1] = '\0'; if (key) { strncpy(new_sec->key, key, sizeof(new_sec->key) - 1); } else { new_sec->key[0] = '\0'; } new_sec->key[sizeof(new_sec->key) - 1] = '\0'; new_sec->lines = NULL; new_sec->next = NULL; return new_sec; } int config_add_section(Config *config, const char *section, const char *key) { if (find_section(config, section, key)) { return -1; // 已存在 } ConfigSection *new_sec = create_section(section, key); new_sec->next = config->sections; config->sections = new_sec; return 0; } int config_read(Config *config, const char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) return -1; ConfigSection *current_sec = NULL; char line[512]; while (fgets(line, sizeof(line), fp)) { char key[64] = {0}; char val[128] = {0}; if (sscanf(line, "%63[^=]=%127[^\n]", key, val) == 2 && strcmp(key, "bss") == 0) { ConfigSection *new_sec = create_section("BSS", val); if (!new_sec) { fclose(fp); return -1; } new_sec->next = config->sections; config->sections = new_sec; current_sec = new_sec; } else { if (!current_sec) { if (!config->sections) { config->sections = create_section("General", NULL); if (!config->sections) { fclose(fp); return -1; } } current_sec = config->sections; } add_line(&current_sec->lines, line); } } fclose(fp); return 0; } int config_write(Config *config, const char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) return -1; for (ConfigSection *sec = config->sections; sec; sec = sec->next) { for (ConfigLine *line = sec->lines; line; line = line->next) { fputs(line->line, fp); } } fclose(fp); return 0; } int config_get_value(Config *config, const char *section, const char *key, const char *name, char *value, size_t size) { ConfigSection *sec = find_section(config, section, key); if (!sec) return -1; for (ConfigLine *line = sec->lines; line; line = line->next) { char k[128], v[128]; if (sscanf(line->line, "%127[^=]=%127[^\n]", k, v) == 2) { if (strcmp(k, name) == 0) { strncpy(value, v, size - 1); value[size - 1] = '\0'; return 0; } } } return -1; } int config_set_value(Config *config, const char *section, const char *key, const char *name, const char *value) { ConfigSection *sec = find_section(config, section, key); if (!sec) return -1; for (ConfigLine *line = sec->lines; line; line = line->next) { char k[128], v[128]; if (sscanf(line->line, "%127[^=]=%127[^\n]", k, v) == 2) { if (strcmp(k, name) == 0) { snprintf(line->line, sizeof(line->line), "%s=%s\n", name, value); return 0; } } } char new_line[512]; snprintf(new_line, sizeof(new_line), "%s=%s\n", name, value); add_line(&sec->lines, new_line); return 0; } int config_remove_section(Config *config, const char *section, const char *key) { ConfigSection **prev = &config->sections; ConfigSection *curr = config->sections; while (curr) { if (strcmp(curr->name, section) == 0 && strcmp(curr->key, key) == 0) { *prev = curr->next; free_lines(curr->lines); free(curr); return 0; } prev = &curr->next; curr = curr->next; } return -1; } int config_remove_key(Config *config, const char *section, const char *key, const char *name) { ConfigSection *sec = find_section(config, section, key); if (!sec) return -1; ConfigLine **prev = &sec->lines; ConfigLine *curr = sec->lines; while (curr) { char k[128], v[128]; if (sscanf(curr->line, "%127[^=]=%127[^\n]", k, v) == 2 && strcmp(k, name) == 0) { *prev = curr->next; free(curr); return 0; } prev = &curr->next; curr = curr->next; } return -1; } int config_read_wpa(Config *config, const char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) return -1; ConfigSection *current_sec = NULL; char line[512]; while (fgets(line, sizeof(line), fp)) { char sec_name[64], key[128]; if (sscanf(line, "%63[^=]={%127[^}]", sec_name, key) == 2) { ConfigSection *new_sec = create_section(sec_name, key); new_sec->next = config->sections; config->sections = new_sec; current_sec = new_sec; } else if (strncmp(line, "}", 1) == 0) { continue; } else if (current_sec) { add_line(&current_sec->lines, line); } } fclose(fp); return 0; } int config_write_wpa(Config *config, const char *filename) { FILE *fp = fopen(filename, "w"); if (!fp) return -1; for (ConfigSection *sec = config->sections; sec; sec = sec->next) { if (strlen(sec->key) > 0) { fprintf(fp, "%s={%s\n", sec->name, sec->key); } for (ConfigLine *line = sec->lines; line; line = line->next) { fputs(line->line, fp); } if (strlen(sec->key) > 0) { fputs("}\n", fp); } } fclose(fp); return 0; } 在此基础上完成修改并给出完整代码
最新发布
08-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值