本博客中有用verilog处理文件数据的代码,本博文采用C 处理文件中的数据。
有时候要生成一个mif文件—— altera memory initial file。本次工程中我得到的是一个大型的数据矩阵一样的东西,我需要慢慢靠近把它处理成mif格式。
第一个程序,把文件读进来是这么干的
//date :2016/07/09
//designer:cobbpeng
//version :00
//function: #include "stdio.h"
//#define METHOD1 void main ()
{
unsigned int b ;
int i ;
FILE *fp0 = fopen("test.txt","r+") ;
#ifdef METHOD1
while (!feof (fp0))
{
fscanf (fp0,"%x",&b);
#else
while (fscanf (fp0,"%x",&b)!=EOF)
#endif
printf ("%x\n",b);
#ifdef METHOD1
}
#endif
fclose (fp0) ;
}
文件读进来了,需要把它格式转换成一行一个的格式,继续修改程序
//date :2016/07/09
//designer:cobbpeng
//version :01
//***************************************************************************
//function:read the file finger.txt. It show as a array 112*96 in file ,
// I musk show the data alone at each line #include "stdio.h"
#define TEST int main ()
{
unsigned int in_data = ;
#ifdef TEST
unsigned int i = ;
#endif
//open file one for read and one for write
FILE *fp0 = fopen("finger.txt","r") ;
FILE *fp1 = fopen("format.txt","w") ;
//********************************************************
if ((fp0==NULL)&&(fp1 == NULL))
printf ("open file failed !!! \n");
else
{
printf ("open file success !!! \n");
//read data finish until being end of the file
while (fscanf (fp0,"%x",&in_data)!=EOF)
{
fprintf(fp1,"%.2x\n",in_data);
#ifdef TEST
if(i<) printf ("%.2x\n",in_data) ;
i++ ;
//break ;
#endif
}
}
fclose (fp0) ;
fclose (fp1) ;
}
上面的代码只是转换了一个格式对吧,要转换到mif格式还差点。所以有了以下代码
//date :2016/07/09
//designer:cobbpeng
//version :02
//***************************************************************************
//function:read the file finger.txt. It show as a array 112*96 in file ,
// I musk show the data alone at each line #include "stdio.h"
//#define TEST
#define METHOD1 int main ()
{
unsigned int in_data = ;
unsigned int byte_num = ;
char w_num = ;
char h_num = ;
char str_mif[] ;
char *s_s; #ifdef TEST
unsigned int i = ;
#endif
//open file
FILE *fp0 = fopen("finger.txt","r") ;
FILE *fp1 = fopen("format.txt","w") ;
FILE *fp2 = fopen("musk_mem.mif","r") ;
//********************************************************
if ((fp0==NULL)&&(fp1 == NULL)&&(fp2 == NULL))
printf ("open file failed !!! \n");
else
{
printf ("open file success !!! \n");
//read data finish until being end of the file
while (fscanf (fp0,"%x",&in_data)!=EOF)
{
byte_num++;
fprintf(fp1,"%.2x,\n",in_data);
w_num++;
#ifdef TEST
if(i<) printf ("%.2x\n",in_data) ;
i++ ;
//break ;
#endif
}
printf ("data num = %ld",byte_num);
//***************************************************************
#ifdef METHOD1
while (fscanf (fp2,"%c",&str_mif[])!=EOF)
{
#else
while(!feof(fp2))
{
str_mif[]=fgetc(fp2) ;
#endif
//printf ("%c\n",str_mif[0]); if((str_mif[]=='N')&&(str_mif[]=='I')&&(str_mif[]=='G')&&(str_mif[]=='E')&&(str_mif[]=='B'))
{
printf ("\nfind string\n");
break ;
}
else
{
str_mif[]=str_mif[] ;
str_mif[]=str_mif[] ;
str_mif[]=str_mif[] ;
str_mif[]=str_mif[] ;
}
} }
fclose (fp0) ;
fclose (fp1) ;
fclose (fp2) ;
}
上面的程序也只是去寻找一个节点:以BEGIN为结尾的地方。—— 因为mif文件中begin后面跟随的是数据。并没有做其他的东西。—— 用C语言去追踪文件中的特定字符还真是有些麻烦。
当我做到这里的时候发现
把数据导入excel,再在excel中复制数据到Q中开启的mif文件中 —— 几个快捷键就搞定了。我去,让我凑了一整天的C 代码。
猜到了开头,却没有猜到结尾。
欢迎加入: FPGA广东交流群:162664354
FPGA开发者联盟: 485678884