c语⾔text的作⽤,⼀个读text⽂本⽂件和解析⽂本的例⼦(C语
⾔)
⼀个读text⽂本⽂件和解析⽂本的例⼦。
(1)引⼊头⽂件const的作用
#include
#include
#include (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);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。