📄 filesearch.c
字号:
/*
*******************************************************************************
* ansi c source code
* file name:
* FileSearch.c
* abstract:
* provide functions to search a character or string in a file, as well as
* read paramaters from a file
* reference:
*
* author:
* Wang Jiaheng 2004-09-09
* revision history:
* Wang Jiaheng 2004-08-09 original version
* Wang Jiaheng 2005-05-14 eliminate a bug
*******************************************************************************
*/
/*
*******************************************************************************
* include files
*******************************************************************************
*/
#include <stdio.h>
#include <string.h>
/*
*******************************************************************************
* function definition
*******************************************************************************
*/
/*
*******************************************************************************
* description:
* find a character in the file
* input:
* pf: pointer to the file
* target: the character to be searched
* output:
* if find, return 1; otherwise retrun 0.
* function reference :
* author :
* Wang Jiaheng, 2004-09-09 created
*******************************************************************************
*/
int FindChar(FILE *pf, char target)
{
char ch;
do
{
ch = fgetc(pf);
} while( (ch != target) && (!feof(pf)) );
if ( ch == target ) return 1;
else return 0;
}
/*
*******************************************************************************
* description: find a string in the file
* inputs:
* pf: pointer to the file
* p_str: pointer to the string to be searched
* output:
* if find, return 1; otherwise retrun 0.
* function reference:
* author:
* Wang Jiaheng, 2004-09-09 created
*******************************************************************************
*/
int FindString(FILE *pf, char *p_str)
{
unsigned int length; /* length of the string */
unsigned int i;
long fpos; /* position in the file */
char find; /* indicator */
length = strlen(p_str);
if ( length == 0 ) return 0;
if ( !FindChar(pf, p_str[0]) ) return 0;
fpos = ftell(pf);
find = 0;
while ( (!find) && (!feof(pf)) )
{
find = 1;
for (i = 1; i < length; i++)
{
if ( fgetc(pf) != p_str[i] )
{
find = 0;
break;
}
}
if ( find ) break;
fseek(pf, fpos+1, 0);
if ( !FindChar(pf, p_str[0]) ) return 0;
fpos = ftell(pf);
}
return 1;
}
/*
*******************************************************************************
* description:
* change the current position to a new line
* input:
* pf: pointer to the file
* output:
* if succeed, return 1; otherwise retrun 0.
* function reference:
* author:
* Wang Jiaheng, 2004-09-09 created
*******************************************************************************
*/
int ChangeLine(FILE *pf)
{
if ( !FindChar(pf, 0x0a) ) return 0;
return 1;
}
/*
*******************************************************************************
* description:
* read parameters from a specified file. The input file must be such a
* pattern below:
* -- xxxxxxx(xx could be annotations)
* parameter1
* -- xxxxxxx
* parameter2
* ......
* input:
* file_name: name of file
* output:
* *p_num: number of parameters read from the file
* para[]: array storing the parameters read from the file
* function reference:
* author:
* Wang Jiaheng, 2004-09-09 created
* Wang Jiaheng, 2005-05-14 revised
*******************************************************************************
*/
int ReadFile(char file_name[], int *p_num, double para[])
{
FILE *pf;
pf = fopen(file_name, "r");
if ( pf == NULL )
{
return 0;
}
*p_num = 0;
while ( FindString(pf, "--") )
{
if ( !ChangeLine(pf) ) break;
fscanf(pf, "%lf", ¶[(*p_num)]);
(*p_num)++;
}
fclose(pf);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -