一个读text文本文件和解析文本的例子(C语言)

一个读text文本文件和解析文本的例子。

(1)引入头文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
(2)函数int initConfigFile(const char * pFileName)

/**
 * initConfigFile()
 * return:
 *   1 create file success
 *   0 create file failed
 */
int initConfigFile(const char * pFileName)
{
	int rval = 0;

	FILE *pFile;
	char buffer[1024] = {0};
	int strLen = 0;
	int bytes = 0;

	if ((pFile = fopen(pFileName, "wb")) == NULL) {
		printf("File cannot be created\n");
		return rval;
	}

	sprintf(buffer, "GPRMC;204522/00;A;2233.94321;N;11402.42498;E");
	strLen = strlen(buffer);
	bytes = fwrite(buffer, 1, strLen, pFile);
	if (bytes == strLen) {
		printf("Initial Configuration File success!\n");
		rval = 1;
	}

	fclose(pFile);
	return rval;
}
(3)函数int parseConfigFile(const char * pFileName)

/**
 * parseConfigFile()
 * return:
 *   1 parse file success
 *   0 parse file failed
 */
int parseConfigFile(const char * pFileName)
{
	int rval = 0;
	
	FILE *pFile;
	long file_size = 0;
	char *buffer = NULL;
	long bytes = 0;
	long i = 0;
	long numberCRLF = 0;
	
	int semiNum = 0;	// Number of semicolon
	int slashNum = 0;	// Number of slash
	
	long strLen = 0;
	char *array = NULL;
	char *pos1 = NULL;
	char *pos2 = NULL;
	
	//
	if ((pFile = fopen(pFileName, "rb")) == NULL) {
		printf("File cannot be opened\n");
		return rval;
	}
	
	fseek(pFile, 0, SEEK_END);
	file_size = ftell(pFile);
	rewind(pFile);
	printf("file_size: %d\n", file_size);
	
	buffer = (char*)malloc(sizeof(char) * (file_size + 1 + 1));	// +1 For strcat(buffer, ";"); later.
	if (!buffer) {
		printf("Insufficient memory available\n" );
		fclose(pFile);
		return rval;
	}
	memset(buffer, 0, (file_size + 1 + 1));	// +1 For strcat(buffer, ";"); later.
	
	bytes = fread(buffer, sizeof(char), file_size, pFile);
	printf("read char: %d\n", bytes);
	buffer[file_size] = '\0';
	if (file_size == bytes) {
		printf("read:\n%s\n", buffer);
	} else {
		printf("read error\n");
		fclose(pFile);
		free(buffer);
		return rval;
	}
	
	fclose(pFile);
	
	//
	// 1. Check the numbers of semicolon and slash
	strLen = strlen(buffer);
	for (i = 0; i < strLen; i ++) {
		if (';' == buffer[i]) {
			semiNum++;
		} else if ('/' == buffer[i]) {
			slashNum++;
		}
	}
	
	printf("semicolon: %d, slash: %d\n", semiNum, slashNum);
	
	if ((semiNum < 6) || (slashNum < 1))
	{
		printf("Configuration file invalid!\n");
		rval = initConfigFile(pFileName);
		return rval;
	} 
	
	//
	// 2. Delete CRLF, beginning of string
	strLen = strlen(buffer);
	array = (char *)malloc(sizeof(char) * (strLen + 1));
	if (!buffer) {
		printf("Insufficient memory available\n" );
		fclose(pFile);
		return rval;
	}
	memset(array, 0, (strLen + 1));
	pos2 = buffer;
	for (i = 0; i < strLen; i++) {
		if (('\r' == buffer[i]) || ('\n' == buffer[i])) {
			pos2++;
		} else {
			break;
		}
	}
	
	strcpy(array, pos2);
	strcpy(buffer, array);
	printf("Delete CRLF, beginning of string:\n%s\n", buffer);
	
	//
	// 3. Delete CRLF, end of string
	strLen = strlen(buffer);
	free(array);
	array = (char *)malloc(sizeof(char) * (strLen + 1));
	if (!buffer) {
		printf("Insufficient memory available\n" );
		fclose(pFile);
		return rval;
	}
	memset(array, 0, (strLen + 1));
	numberCRLF = 0;
	for (i = (strLen - 1); i >= 0; i--) {
		if (('\r' != buffer[i]) && ('\n' != buffer[i])) {
			break;
		} else {
			numberCRLF++;
		}
	}
	
	strncpy(array, buffer, (strLen - numberCRLF));
	strcpy(buffer, array);
	printf("Delete CRLF, end of string:\n%s\n", buffer);
	
	//
	// 4. Remove spaces at the beginning of the string
	strLen = strlen(buffer);
	free(array);
	array = (char *)malloc(sizeof(char) * (strLen + 1));
	if (!buffer) {
		printf("Insufficient memory available\n" );
		fclose(pFile);
		return rval;
	}
	memset(array, 0, (strLen + 1));
	pos2 = buffer;
	for (i = 0; i < strLen; i++) {
		if (' ' == buffer[i]) {
			pos2++;
		} else {
			break;
		}
	}
	
	strcpy(array, pos2);
	strcpy(buffer, array);
	printf("Remove spaces at the beginning of the string:\n%s\n", buffer);
	
	//
	// 5. parse the string
	strcat(buffer, ";");
	strLen = strlen(buffer);
	free(array);
	array = (char *)malloc(sizeof(char) * (strLen + 1));
	if (!buffer) {
		printf("Insufficient memory available\n" );
		fclose(pFile);
		return rval;
	}
	memset(array, 0, (strLen + 1));
	pos1 = NULL;
	pos2 = buffer;
	pos1 = strstr(pos2, ";");
	while (NULL != pos1) {
		strncpy(array, pos2, (pos1 - pos2));
		array[(pos1 - pos2)] = '\0';
		printf("array: %s\n", array);
		pos2 = pos1 + 1;
		pos1 = strstr(pos2, ";");
	}
	
	free(array);
	free(buffer);
	
	rval = 1;
	return rval;
}
(4)main函数

int main(int argc, char *argv[])
{
	int result =0;


	result = parseConfigFile("config.ini");


	if (result) {
		printf("Parse File success!\n");
	} else {
		printf("Parse File failed!\n");
	}


	return 0;
}






  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值