一个读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
- 一个读text文本文件和解析文本的例子(C语言)
- C语言文本文件解析
- C语言二进制文件和文本文件的区别
- 一个例子说明C语言和Java的区别
- 编写一个C程序将一个文本的字符计算将字符个数输出到另一个文本文件(图)
- 【C语言编程】统计一个文本文件的行数
- c语言,读取一个内容长度未知的文本文件。
- C语言混乱代码:加密文本的过滤器(Binary to encrypted text filter)
- 二进制文件和文本文件的例子
- 090911c语言笔记(一个字符串输出的例子)
- C语言字符串处理的一个例子
- c语言文件读写的一个例子
- C语言实现POST的一个例子
- C语言中二进制文件和文本文件的区别
- VC++编程打开一个文本文件的例子
- java生成和读取文本文件(text)
- C语言学习-数据类型本质(表示一个内存格子的长度和解析方法)
- C语言中文本文件 和 二进制文件 流的区别(二)
- 拓扑排序【模板】
- C++中malloc/free与new/delete的区别及内存分配失败错误处理
- vi操作整理
- c#多态的重要实现-接口
- Sphinx 在 windows 上的安装并用JAVA调用
- 一个读text文本文件和解析文本的例子(C语言)
- Bash脚本 Vlc Android平台下载和编译
- 计划以mini2440学习Linux。
- 在myeclipse中如何设置项目编码方式
- 二分查找的探讨
- android刷机包目录结构
- mpi 程序 debug
- nyoj 数论 541最强DE 战斗力
- c#多态