加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

SV中文件操作的那些函数

(2014-07-31 16:46:50)
标签:

文件操作函数

分类: 杂谈

1,函数原型:char * fgets (char *buf, int bufsize, FILE *stream); 其中*buf,用来储存所得数据的地址。bufsize, 指明储存数据的大小。 *stream将要读取的文件流。

      函数从文件结构体stream中读取数据,每次读取一行。读取的数据保存在buf指向的字符数组中。每次最多读取bufsize-1个字符,如果文件中的该行不足bufsize-1个字符,则读完该行结束。

      函数成功将返回buf,失败或者读到文件结尾返回NULL,所以不能通过fgets的返回值来判断操作是否正确。可以通过eof(end of file)来判断是否读到文件结尾。

2,函数原型:int sscanf (const char *buffer, const char *format, [argument])。 buffer表示储存的数据,format表示输出数据的格式控制字符串,argument表示接收数据的参数。

      sscanf从buffer中读出数据,按着format的格式将数据写入argument中。sscanf与scanf都是用于输入的,只是后者是以键盘(stdin)为输入源的,前者是以固定字符串为输入源的。

      函数成功则返回参数的数目,失败则返回-1。

3,fflush(stdin):功能清空输入缓冲区,来确保不影响后面数据的读取。

4,SV中的两个命令行输入的调试函数。$test$plusargs,原文解释为:If the prefix of one of the supplied plusargs matches all characters in the provided string, a non-zero integer is returned. If no plusarg from the command line matches the string provided, the integer value zero (0) is returned.

eg: initial  begin

                     if ($test$pluargs ("HELLO"))

                                       $display ("Hello  argument  found.")

当命令行输入中没有HELLO时,display不会显示。有HELLO时,display执行。字符串也可以来自于其他脚本文件中,如ini等。These arguments are visually distinguished from other simulator arguments by the starting with the plus (+) character.

       $value$plusargs(user_string,variable).

       The user_string shall be of the form "plusarg_string format_string",其中%d decimal conversion

%o octal conversion  %h hexadecimal  conversion  %b  binary  conversion 

%e real exponential conversion  %s string   %f real  decimal  conversion 

eg:     +FINISH=10000  

integer  stop_clock;

      if($value$plusargs ("FINISH=%d", stop_clock))

             begin   repeat (stop_clock)   @ (posedge clk);     $finish   end

module 在10000个时钟后结束。

5,函数原型:¥sformat (output_reg, format_string, list_of_arguments);将arguments的值按着format的格式输入到output_reg中。

Eg: for (int i =0; i<2;i++)

              begin

                      $ sformat (instance_name, "slave[%d]",i);

              end

结果为:slave[0],  slave[1]。

6,函数原型:int  atoi (const  char  *nptr)。把字符串转换成整形。 ASCII to integer。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有