很多软件都有配置文件,软件代码需要从配置文件中获取信息. 在看JM8.6解码器时,需要解析配置文件,以便从配置文件decoder.cfg中获取信息. 当然配置文件中有“注释”,那么,如何滤掉这些“注释”呢?

      decoder.cfg文件中的内容为:

 

test.264                 ........H.264 coded bitstream   
test_dec.yuv             ........Output file, YUV 4:2:0 format    
test_rec.yuv             ........Ref sequence (for SNR)   
10                       ........Decoded Picture Buffer size   
0                        ........NAL mode (0=Annex B, 1: RTP packets)   
0                        ........SNR computation offset   
1                        ........Poc Scale (1 or 2)   
500000                   ........Rate_Decoder   
104000                   ........B_decoder  
73000                    ........F_decoder  
leakybucketparam.cfg     ........LeakyBucket Params     

This is a file containing input parameters to the JVT H.264/AVC decoder.
The text line following each parameter is discarded by the decoder.

 

       那么,怎样从配置文件中读出有用信息呢?代码如下:

#include<stdio.h>

int main()
{
	char inputFile[20];
	char outputFile[20];
	char refFile[20];
	int bufferSize;

	FILE *fp = fopen("decoder.cfg", "r");

	fscanf(fp, "%s", inputFile);
	fscanf(fp,"%*[^\n]");

	fscanf(fp, "%s", outputFile);
	fscanf(fp,"%*[^\n]");

	fscanf(fp, "%s", refFile);
	fscanf(fp,"%*[^\n]");

	// 千万别忘了bufferSize前面的&
	fscanf(fp, "%d", &bufferSize);
	fscanf(fp,"%*[^\n]");

	fclose(fp);

	printf("%s\n", inputFile);
	printf("%s\n", outputFile);
	printf("%s\n", refFile);
	printf("%d\n", bufferSize);

	return 0;
}


      结果为:

test.264
test_dec.yuv
test_rec.yuv
10


本文转载:CSDN博客