⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doconfig.c

📁 是自己开发的程序
💻 C
字号:
/************************************************************ ÎļþÃû: prefile.c ¹¦  ÄÜ: ¶Á/дÅäÖÃÎļþÄÚÈÝ ÈÕ  ÆÚ: 2005.3.2 ÐÞ¸ÄÈË: ÁõÀ´²¨ ×¢  ½â£º¶Á»òдÅäÖÃÎļþµÄÄÚÈÝ£¬ÅäÖÃÎļþ¸ñʽÈçÏ£º		 1¡¢±êÌ⣺[Title]		 2¡¢Ïî:   Item=		 3¡¢ÄÚÈÝ: content		 ±êÌâµ¥¶ÀÒ»ÐÐ, ÏîºÍÄÚÈÝÔÚÒ»ÐУ¬ Óá°=¡±·Ö¸ô;		 ±êÌâ²»¿ÉÒÔÖØ¸´;		 ÿһÐÐÒÔ»»Ðзû½áÊø,×îºóÒ»ÐпÉÒÔ²»¼Ó»»Ðзû;		 ÿÐÐ×î¶à254¸ö×Ö·û¡£*************************************************************/#include "doconfig.h"#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#define MAXLINE	255struct record{	char	title[MAXLINE];	char	item[MAXLINE];	char	content[MAXLINE];	struct record *first;	struct record *next;};// È¡±êÌâvoid GetTitle(char *str, char *title){	char strtemp[MAXLINE];	char *p, *q;	strcpy(strtemp, str);	strtemp[MAXLINE - 1] = 0;	p = strtemp;	q = p;	while (*p != ']' && *p != 0)		p++;	*p = '\0';	while (*(q++) != '[' && *q != 0);	strcpy(title, q);	return;}// È¡Ïîvoid GetItem(char *str, char *item){	char strtemp[MAXLINE];	char *p, *q;	strcpy(strtemp, str);	strtemp[MAXLINE - 1] = 0;	p = strtemp;	while (*p == ' ')		p++;	q = p;	while (*q != '=' && *q != ' ' && *q != 0)		q++;	*q = 0;	strcpy(item, p);	return;}// È¡ÄÚÈÝvoid GetContent(char *str, char *content){	char strtemp[MAXLINE];	char *p, *q;	strcpy(strtemp, str);	strtemp[MAXLINE - 1] = 0;	p = strtemp;	while (*(p++) != '=' && *p != 0);	q = p;	while (*q != '\n' && *q != 0)		q++;	*q = 0;	strcpy(content, p);	return;}// ´ÓÅäÖÃÎļþÖжÁ³ö×Ö·û´®ÄÚÈÝ// ·µ»ØÖµ: ³É¹¦: ·µ»Ø×Ö·û´®µÄ³¤¶È    ʧ°Ü: -1    ·µ»Ø0ÊÇûÓÐÕÒµ½int ReadString(char *filename, char *title, char *item, char *content, char *defaultstr){	FILE	*fp;	char	linebuf[MAXLINE];	int		isBegin = 0;	char	strtemp[MAXLINE];	memset(linebuf, 0, MAXLINE);	memset(content, 0, strlen(content));	memset(strtemp, 0, strlen(strtemp));	// ´ò¿ªÎļþ£¬³ö´í·µ»Ø	if ((fp = fopen(filename, "r")) == NULL)		return -1;	fseek(fp, 0L, SEEK_SET);	while (fgets(linebuf, MAXLINE, fp) != NULL)	{		linebuf[MAXLINE - 1] = '\0';		if (isBegin == 1)		{			// Èç¹û±êÌâ»»ÁË£¬ÔòÍ˳öÑ­»·£¬ËµÃ÷ûÕÒµ½			if (strstr(linebuf, "[") != NULL && strstr(linebuf, "]") != NULL)				break;			else			{				// È¡Ïî				GetItem(linebuf, strtemp);				if (strcmp(strtemp, item) == 0)				{					GetContent(linebuf, content);					fclose(fp);					return strlen(content);				}			}		}		// ÕÒµ½±êÌâºó£¬×ö±êÖ¾		if (strstr(linebuf, "[") != NULL && strstr(linebuf, title) != NULL && strstr(linebuf, "]") != NULL)			isBegin = 1;		memset(linebuf, 0, sizeof(linebuf));	}	fclose(fp);	strcpy(content, defaultstr);	return 0;}// ¶Á±êÌâ,// ·µ»ØÖµ: 0, ³É¹¦; -1, ʧ°Üint ReadTitle(char *filename, char title[20][30]){	FILE	*fp;	char	buf[255];	char	session[255];	short int	i = 0;	memset(buf,0,255);	memset(title, 0, sizeof(title));	memset(session, 0, sizeof(session));	// ´ò¿ªÎļþ£¬³ö´í·µ»Ø	if ((fp = fopen(filename, "r")) == NULL)		return -1;	fseek(fp, 0L, SEEK_SET);	i = 0;	while (fgets(buf, 255, fp) != NULL)	{		if (strstr(buf, "[") != NULL && strstr(buf, "]") != NULL)		{			GetTitle(buf, session);			strcpy(title[i], session);			i++;		}	}	return 0;}// ´ÓÅäÖÃÎļþÖжÁ³ö×Ö·û´®ÄÚÈÝ, ²¢×ª»»³ÉÕûÐÍ·µ»Ø// ·µ»ØÖµ: ³É¹¦: ·µ»Ø×Ö·û´®µÄ³¤¶È    ʧ°Ü: -1    ·µ»Ø0ÊÇûÓÐÕÒµ½int ReadInt(char *filename, char *title, char *item, int defaultvalue){	FILE	*fp;	char	linebuf[MAXLINE];	int		isBegin = 0;	char	strtemp[MAXLINE];	char	content[MAXLINE];	memset(linebuf, 0, MAXLINE);	memset(content, 0, strlen(content));	memset(strtemp, 0, strlen(strtemp));	// ´ò¿ªÎļþ£¬³ö´í·µ»Ø	if ((fp = fopen(filename, "r")) == NULL)		return defaultvalue;	fseek(fp, 0L, SEEK_SET);	while (fgets(linebuf, MAXLINE, fp) != NULL)	{		linebuf[MAXLINE - 1] = '\0';		if (isBegin == 1)		{			// Èç¹û±êÌâ»»ÁË£¬ÔòÍ˳öÑ­»·£¬ËµÃ÷ûÕÒµ½			if (strstr(linebuf, "[") != NULL && strstr(linebuf, "]") != NULL)				break;			else			{				// È¡Ïî				GetItem(linebuf, strtemp);				if (strcmp(strtemp, item) == 0)				{					GetContent(linebuf, content);					fclose(fp);					return atoi(content);				}			}		}		// ÕÒµ½±êÌâºó£¬×ö±êÖ¾		if (strstr(linebuf, "[") != NULL && strstr(linebuf, title) != NULL && strstr(linebuf, "]") != NULL)			isBegin = 1;		memset(linebuf, 0, sizeof(linebuf));	}	fclose(fp);	return defaultvalue;}// Ð޸ġ¢Ìí¼ÓÅäÖÃÎļþ// ·µ»ØÖµ£º0, ³É¹¦  -1, ʧ°Üint WriteString(char *filename, char *session, char *instr, char *repairstr){	FILE *fp = NULL;	struct record *head = NULL, *p = NULL, *pnew = NULL;	char	strtemp[MAXLINE] = "\0";	char	title[MAXLINE];	int	isNoFind = 0;	// open file	if (access(filename, F_OK) == -1)	{		if ((fp = fopen(filename, "wb+")) == NULL)		{			printf("open file %s error!\n", filename);			return -1;		}		fprintf(fp, "[%s]\n", session);		fprintf(fp, "%s=%s\n", instr, repairstr);		fclose(fp);		return 0;	}	else	{		if ((fp = fopen(filename, "r+")) == NULL)		{			printf("open file %s error!\n", filename);			return -1;		}	}	head = (struct record*)malloc(sizeof(struct record));	bzero(head->title, sizeof(head->title));	bzero(head->item, sizeof(head->item));	bzero(head->content, sizeof(head->content));	head->first = NULL;	head->next = NULL;	// read line data from file.	p = head;	while (!feof(fp))	{		if (fgets(strtemp, MAXLINE, fp) == NULL)			break;		if (strstr(strtemp, "[") != NULL && strstr(strtemp, "]") != NULL)		{			pnew = (struct record*)malloc(sizeof(struct record));			GetTitle(strtemp, title);			strcpy(pnew->title, title);			bzero(pnew->item, sizeof(pnew->item));			bzero(pnew->content, sizeof(pnew->content));		}		else		{			if (strstr(strtemp, "=") != NULL)			{				pnew = (struct record*)malloc(sizeof(struct record));				strcpy(pnew->title, title);				GetItem(strtemp, pnew->item);				GetContent(strtemp, pnew->content);			}			else				continue;		}		pnew->first = p;		pnew->next = NULL;		p->next = pnew;		p = p->next;		bzero(strtemp, sizeof(strtemp));	}	fclose(fp);	// insert or modify item data.	p = head;	isNoFind = 0;	while (p->next != NULL)	{		p = p->next;		// modify		if (strcmp(p->title, session) == 0)		{			if (strcmp(p->item, instr) == 0)			{				strcpy(p->content, repairstr);				break;			}			// decide if or not add one item			if (p->next != NULL && strcmp(p->title, p->next->title) != 0 || p->next == NULL)			{					pnew = (struct record*)malloc(sizeof(struct record));					strcpy(pnew->title, p->title);					strcpy(pnew->item, instr);					strcpy(pnew->content, repairstr);					pnew->first = p;					pnew->next = p->next;					p->next = pnew;					break;			}		}		if (p->next == NULL)			isNoFind = 1;	}	// no find or file is empty, append it	if (isNoFind)	{		// create title.		pnew = (struct record*)malloc(sizeof(struct record));		strcpy(pnew->title, session);		bzero(pnew->item, sizeof(pnew->item));		bzero(pnew->content, sizeof(pnew->content));		pnew->first = p;		pnew->next = NULL;		p->next = pnew;		// create item and content		p = p->next;		pnew = (struct record*)malloc(sizeof(struct record));		strcpy(pnew->title, session);		strcpy(pnew->item, instr);		strcpy(pnew->content, repairstr);		pnew->first = p;		pnew->next = NULL;		p->next = pnew;	}	// write line data to file.	if ((fp = fopen(filename, "w+")) != NULL)	{		p = head;		while (p->next != NULL)		{			p = p->next;			if (strlen(p->item) == 0)				fprintf(fp, "[%s]\n", p->title);			else				fprintf(fp, "%s=%s\n", p->item, p->content);		}		fclose(fp);	}	else		printf("fopen %s error when write data!\n", filename);	p = head;	while (p->next != NULL)	{		p = p->next;		free(p->first);		p->first = NULL;	}	free(p);	p = NULL;	return 0;}/*void writedata(void){	char	strtemp[20];	long int num = 0;	long int num2 = 0;	sleep(2);	num = ReadInt("writedata.txt", "1234", "LotteryNum", 0);	sprintf(strtemp, "%d", ++num);	WriteString("writedata.txt", "1234", "LotteryNum", strtemp);	sleep(1);	bzero(strtemp, sizeof(strtemp));	num2 = ReadInt("writedata.txt", "1234", "PrintNum", 0);	sprintf(strtemp, "%d", ++num2);	WriteString("writedata.txt", "1234", "PrintNum", strtemp);	printf("num is %d, num2 is %d\n", num, num2);	return;}main(){	long int i = 0;	int	child_pid;	for (i = 0; i < 1000; i++)	{		switch (child_pid = fork())		{			case 0:				writedata();				exit(0);			case -1:				printf("create thread error!\n");				break;			defalut:				close(child_pid);				break;		}	}}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -