zoukankan      html  css  js  c++  java
  • FPGA工程中用C语言对文件进行处理_生成mif文件

      本博客中有用verilog处理文件数据的代码,本博文采用C 处理文件中的数据。

      有时候要生成一个mif文件—— altera memory  initial file。本次工程中我得到的是一个大型的数据矩阵一样的东西,我需要慢慢靠近把它处理成mif格式。

    第一个程序,把文件读进来是这么干的

     1 //date    :2016/07/09
     2 //designer:cobbpeng
     3 //version :00
     4 //function:
     5 
     6 #include "stdio.h"
     7 //#define METHOD1
     8 
     9 void main ()
    10 {
    11     unsigned int b ; 
    12     int i ; 
    13     FILE  *fp0 = fopen("test.txt","r+") ; 
    14 #ifdef METHOD1
    15     while (!feof (fp0)) 
    16     {
    17         fscanf (fp0,"%x",&b); 
    18 #else 
    19         while (fscanf (fp0,"%x",&b)!=EOF) 
    20 #endif
    21         printf ("%x
    ",b); 
    22 #ifdef METHOD1
    23     }
    24 #endif 
    25     fclose (fp0) ; 
    26 }

    文件读进来了,需要把它格式转换成一行一个的格式,继续修改程序

     1 //date    :2016/07/09
     2 //designer:cobbpeng
     3 //version :01
     4 //***************************************************************************
     5 //function:read the file finger.txt. It show as a array 112*96 in file ,
     6 //            I musk show the data alone at each line
     7 
     8 #include "stdio.h"
     9 #define TEST
    10 
    11 int main  ()
    12 {
    13     unsigned int in_data = 0 ; 
    14 #ifdef TEST
    15     unsigned int i = 0 ; 
    16 #endif
    17 //open file one for read  and one for write     
    18     FILE  *fp0 = fopen("finger.txt","r") ; 
    19     FILE  *fp1 = fopen("format.txt","w") ;
    20 //********************************************************
    21     if ((fp0==NULL)&&(fp1 == NULL)) 
    22         printf ("open file  failed !!! 
    ");
    23     else 
    24     {
    25         printf ("open file success !!! 
    ");
    26 //read data finish until being end of the file 
    27         while (fscanf (fp0,"%x",&in_data)!=EOF) 
    28         {
    29             fprintf(fp1,"%.2x
    ",in_data);
    30 #ifdef TEST
    31             if(i<115) printf ("%.2x
    ",in_data) ; 
    32             i++ ; 
    33             //break ;     
    34 #endif
    35         }  
    36     }
    37     fclose (fp0) ; 
    38     fclose (fp1) ;     
    39 }

     上面的代码只是转换了一个格式对吧,要转换到mif格式还差点。所以有了以下代码

     1 //date    :2016/07/09
     2 //designer:cobbpeng
     3 //version :02
     4 //***************************************************************************
     5 //function:read the file finger.txt. It show as a array 112*96 in file ,
     6 //            I musk show the data alone at each line
     7 
     8 #include "stdio.h"
     9 //#define TEST
    10 #define METHOD1
    11 
    12 int main  ()
    13 {
    14     unsigned int in_data = 0 ; 
    15     unsigned int byte_num =0 ; 
    16     char w_num =0 ; 
    17     char h_num = 0 ;
    18     char str_mif[5] ; 
    19     char *s_s;
    20     
    21 #ifdef TEST
    22     unsigned int i = 0 ; 
    23 #endif
    24 //open file 
    25     FILE  *fp0 = fopen("finger.txt","r") ; 
    26     FILE  *fp1 = fopen("format.txt","w") ;
    27     FILE  *fp2 = fopen("musk_mem.mif","r") ;
    28 //********************************************************
    29     if ((fp0==NULL)&&(fp1 == NULL)&&(fp2 == NULL)) 
    30         printf ("open file  failed !!! 
    ");
    31     else 
    32     {
    33         printf ("open file success !!! 
    ");
    34 //read data finish until being end of the file 
    35         while (fscanf (fp0,"%x",&in_data)!=EOF) 
    36         {
    37             byte_num++; 
    38             fprintf(fp1,"%.2x,
    ",in_data);
    39             w_num++;         
    40 #ifdef TEST
    41             if(i<115) printf ("%.2x
    ",in_data) ; 
    42             i++ ; 
    43             //break ;     
    44 #endif
    45         }
    46         printf ("data num = %ld",byte_num);
    47 //***************************************************************    
    48 #ifdef METHOD1    
    49     while (fscanf (fp2,"%c",&str_mif[0])!=EOF) 
    50         {
    51 #else 
    52     while(!feof(fp2))
    53         {
    54             str_mif[0]=fgetc(fp2) ;
    55 #endif 
    56             //printf ("%c
    ",str_mif[0]);
    57 
    58             if((str_mif[0]=='N')&&(str_mif[1]=='I')&&(str_mif[2]=='G')&&(str_mif[3]=='E')&&(str_mif[4]=='B'))
    59             {
    60                 printf ("
    find string
    ");
    61                 break ; 
    62             }
    63             else 
    64             {
    65                 str_mif[4]=str_mif[3] ;
    66                 str_mif[3]=str_mif[2] ;
    67                 str_mif[2]=str_mif[1] ;
    68                 str_mif[1]=str_mif[0] ;
    69             }
    70         }
    71         
    72     }
    73     fclose (fp0) ; 
    74     fclose (fp1) ;     
    75     fclose (fp2) ; 
    76 }

    上面的程序也只是去寻找一个节点:以BEGIN为结尾的地方。—— 因为mif文件中begin后面跟随的是数据。并没有做其他的东西。—— 用C语言去追踪文件中的特定字符还真是有些麻烦。

    当我做到这里的时候发现

    把数据导入excel,再在excel中复制数据到Q中开启的mif文件中 —— 几个快捷键就搞定了。我去,让我凑了一整天的C 代码。

    猜到了开头,却没有猜到结尾。

    欢迎加入: FPGA广东交流群:162664354

          FPGA开发者联盟: 485678884

  • 相关阅读:
    uva10256
    uva11168
    zoj2318
    hdu6121
    hdu6127
    bzoj3957: [WF2011]To Add or to Multiply
    bzoj4377: [POI2015]Kurs szybkiego czytania
    bzoj3137: [Baltic2013]tracks
    bzoj4069: [Apio2015]巴厘岛的雕塑
    bzoj4169: Lmc的游戏
  • 原文地址:https://www.cnblogs.com/sepeng/p/5740079.html
Copyright © 2011-2022 走看看