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

来源:互联网 发布:linux vi 不保存 编辑:程序博客网 时间:2024/06/05 11:45

一个读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 semicolonint slashNum = 0;// Number of slashlong 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 slashstrLen = 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 stringstrLen = 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 stringstrLen = 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 stringstrLen = 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 stringstrcat(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 0