搜档网
当前位置:搜档网 › verilog读写文件

verilog读写文件

verilog读写文件
verilog读写文件

系统函数$fopen用于打开一个文件,并还回一个整数指针.然后,$fdisplay就可以使用这个文件指针在文件中写入信息,写完后,则可以使用$fclose系统关闭这个文件

例如:

integer write_out_file;//定义一个文件指针

integer write_out_file=$fopen("write_out_file.txt");

$fdisplay(write_out_file,"@%h\n%h",addr,data);

$fclose("write_out_file");

以上语法是将addr,data分别显示在”@%h\n%h”中的2个%h的位置,并写入write_out_file文件指针所指向的write_out_file.txt中.

从文件中读取数据,可以用$readmemb $readmemh 从文件中读入数据,该文件格式是一定的.

reg〔7:0〕data〔47:0〕;

$readmemh("file_name.txt",data );

就是将file_name.txt中的数据读入到data数组中,然后就可以使用这些数据了.

还有一种方式可以把指定的数据放入指定的存储器地址单元内,就是在存放数据的文本文件内,给相应的数据规定其内存地址,形式如下:

@address_in_hexadecimal data

@2f20

两个系统任务可以在仿真的任何时刻被执行使用,其使用格式共有以下六种:

1) $readmemb("<数据文件名>",<存贮器名>);

2) $readmemb("<数据文件名>",<存贮器名>,<起始地址>);

3) $readmemb("<数据文件名>",<存贮器名>,<起始地址>,<结束地址>);

4) $readmemh("<数据文件名>",<存贮器名>);

5) $readmemh("<数据文件名>",<存贮器名>,<起始地址>);

6) $readmemh("<数据文件名>",<存贮器名>,<起始地址>,<结束地址>);

在这两个系统任务中,被读取的数据文件的内容只能包含:空白位置(空格,换行,制表格(tab)和form-feeds),注释行(//形式的和/*...*/形式的都允许),二进制或十六进制的数字。数字中不能包含位宽说明和格式说明,对于$readmemb系统任务,每个数字必须是二进制数字,对于$readmemh系统任务,每个数字必须是十六进制数字。数字中不定值x或X,高阻值z或Z,和下划线(_)的使用方法及代表的意义与一般V erilog HDL程序中的用法及意义是一样的。另外数字必须用空白位置或注释行来分隔开。

在下面的讨论中,地址一词指对存贮器(memory)建模的数组的寻址指针。当数据文件被读取时,每一个被读取的数字都被存放到地址连续的存贮器单元中去。存贮器单元的存放地址范围由系统任务声明语句中的起始地址和结束地址来说明,每个数据的存放地址在数据文件中进行说明。当地址出现在数据文件中,其格式为字符“@”后跟上十六进制数。如:

@hh...h

对于这个十六进制的地址数中,允许大写和小写的数字。在字符“@”和数字之间不允许存在空白位置。可以在数据文件里出现多个地址。当系统任务遇到一个地址说明时,系统任

务将该地址后的数据存放到存贮器中相应的地址单元中去。

对于上面六种系统任务格式,需补充说明以下五点:

1) 如果系统任务声明语句中和数据文件里都没有进行地址说明,则缺省的存放起始地址为该存贮器定义语句中的起始地址。数据文件里的数据被连续存放到该存贮器中,直到该存贮器单元存满为止或数据文件里的数据存完。

2) 如果系统任务中说明了存放的起始地址,没有说明存放的结束地址,则数据从起始地址开始存放,存放到该存贮器定义语句中的结束地址为止。

3) 如果在系统任务声明语句中,起始地址和结束地址都进行了说明,则数据文件里的数据按该起始地址开始存放到存贮器单元中,直到该结束地址,而不考虑该存贮器的定义语句中的起始地址和结束地址。

4) 如果地址信息在系统任务和数据文件里都进行了说明,那么数据文件里的地址必须在系统任务中地址参数声明的范围之内。否则将提示错误信息,并且装载数据到存贮器中的操作被中断。

5) 如果数据文件里的数据个数和系统任务中起始地址及结束地址暗示的数据个数不同的话,也要提示错误信息。

下面举例说明:

先定义一个有256个地址的字节存贮器mem:

reg[7:0] mem[1:256];

下面给出的系统任务以各自不同的方式装载数据到存贮器mem中。

initial $readmemh("mem.data",mem);

initial $readmemh("mem.data",mem,16);

initial $readmemh("mem.data",mem,128,1);

第一条语句在仿真时刻为0时,将装载数据到以地址是1的存贮器单元为起始存放单元的存贮器中去。第二条语句将装载数据到以单元地址是16的存贮器单元为起始存放单元的存贮器中去,一直到地址是256的单元为止。第三条语句将从地址是128的单元开始装载数据,一直到地址为1的单元。在第三种情况中,当装载完毕,系统要检查在数据文件里是否有128个数据,如果没有,系统将提示错误信息。

”引用参考”

1.打开文件

integer file_id;

file_id = fopen("file_path/file_name");

2.写入文件

//$fmonitor只要有变化就一直记录

$fmonitor(file_id, "%format_char", parameter);

eg:$fmonitor(file_id, "%m: %t in1=%d o1=%h", $time, in1, o1);

//$fwrite需要触发条件才记录

$fwrite(file_id, "%format_char", parameter);

//$fdisplay需要触发条件才记录

$fdisplay(file_id, "%format_char", parameter);

$fstrobe();

3.读取文件

integer file_id;

file_id = $fread("file_path/file_name", "r");

4.关闭文件

$fclose(fjile_id);

5.由文件设定存储器初值

$readmemh("file_name", memory_name"); //初始化数据为十六进制

$readmemb("file_name", memory_name"); //初始化数据为二进制

转:

https://www.sodocs.net/doc/8f1247380.html,/logs/19569780.htmlVerilog

提供了丰富的系统函数,这为T estbench的编写提供了方便。尤其是IEEE1364-2005,其系统级建模的能力更强。

以前我一般常用到的系统函数只有几个:$readmemb,$readmemh,$display,$fmonitor,$fwrite,$fopen,$fclose等。通常需要对文件作预处理,才能用于T estbench读取。今天又尝试了几个其他的文件输入输出函数,不需要对文件进行预处理,直接使用需要的文件,只对需要的部分进行读取。

$fseek,文件定位,可以从任意点对文件进行操作;

$fscanf,对文件一行进行读写。

下面是一些常见的应用:

1、读写文件

`timescale 1 ns/1 ns

module FileIO_tb;

integer fp_r, fp_w, cnt;

reg [7:0] reg1, reg2, reg3;

initial begin

fp_r = $fopen("data_in.txt", "r");

fp_w = $fopen("data_out.txt", "w");

while(!$feof(fp_r)) begin

cnt = $fscanf(fp_r, "%d %d %d", reg1, reg2, reg3);

$display("%d %d %d", reg1, reg2, reg3);

$fwrite(fp_w, "%d %d %d\n", reg3, reg2, reg1);

end

$fclose(fp_r);

$fclose(fp_w);

end

2、

integer file, char;

reg eof;

initial begin

file = $fopenr("myfile.txt");

eof = 0;

while (eof == 0) begin

char = $fgetc(file);

eof = $feof (file);

$display ("%s", char);

end

end

3、文件处理定位

`define SEEK_SET 0

`define SEEK_CUR 1

`define SEEK_END 2

integer file, offset, position, r;

r = $fseek(file, 0, `SEEK_SET); /* Beginning */

r = $fseek(file, 0, `SEEK_CUR); /* No effect */

r = $fseek(file, 0, `SEEK_END); /* End of file */

r = $fseek(file, position, `SEEK_SET); /* Previous loc */ 4、

integer r, file, start, count;

reg [15:0] mem[0:10], r16;

r = $fread(file, mem[0], start, count);

r = $fread(file, r16);

5、

integer file, position;

position = $ftell(file);

6、

integer file, r, a, b;

reg [80*8:1] string;

file = $fopenw("output.log");

r = $sformat(string, "Formatted %d %x", a, b);

r = $sprintf(string, "Formatted %d %x", a, b);

r = $fprintf(file, "Formatted %d %x", a, b);

7、

integer file, r;

file = $fopenw("output.log");

r = $fflush(file);

8、

// This is a pattern file - read_pattern.pat

// time bin dec hex

20.0: 010 20 020

50.02: 111 5 FFF

62.345: 100 4 DEADBEEF

75.789: XXX 2 ZzZzZzZz

`timescale 1ns / 10 ps

`define EOF 32'hFFFF_FFFF

`define NULL 0

`define MAX_LINE_LENGTH 1000

module read_pattern;

integer file, c, r;

reg [3:0] bin;

reg [31:0] dec, hex;

real real_time;

reg [8*`MAX_LINE_LENGTH:0] line; /* Line of text read from file */

initial

begin : file_block

$timeformat(-9, 3, "ns", 6);

$display("time bin decimal hex");

file = $fopenr("read_pattern.pat");

if (file == `NULL) // If error opening file

disable file_block; // Just quit

c = $fgetc(file);

while (c != `EOF)

begin

/* Check the first character for comment */

if (c == "/")

r = $fgets(line, `MAX_LINE_LENGTH, file);

else

begin

// Push the character back to the file then read the next time

r = $ungetc(c, file);

r = $fscanf(file," %f:\n", real_time);

// Wait until the absolute time in the file, then read stimulus

if ($realtime > real_time)

$display("Error - absolute time in file is out of order - %t",

real_time);

else

#(real_time - $realtime)

r = $fscanf(file," %b %d %h\n",bin,dec,hex);

c = $fgetc(file);

end // while not EOF

r = $fcloser(file);

end // initial

// Display changes to the signals

always @(bin or dec or hex)

$display("%t %b %d %h", $realtime, bin, dec, hex);

endmodule // read_pattern

9、自动比较输出结果

`define EOF 32'hFFFF_FFFF

`define NULL 0

`define MAX_LINE_LENGTH 1000

module compare;

integer file, r;

reg a, b, expect, clock;

wire out;

reg [`MAX_LINE_LENGTH*8:1];

parameter cycle = 20;

initial

begin : file_block

$display("Time Stim Expect Output");

clock = 0;

file = $fopenr("compare.pat");

if (file == `NULL)

disable file_block;

r = $fgets(line, MAX_LINE_LENGTH, file); // Skip comments r = $fgets(line, MAX_LINE_LENGTH, file);

while (!$feof(file))

begin

// Wait until rising clock, read stimulus

@(posedge clock)

r = $fscanf(file, " %b %b %b\n", a, b, expect);

// Wait just before the end of cycle to do compare

#(cycle - 1)

$display("%d %b %b %b %b", $stime, a, b, expect, out);

$strobe_compare(expect, out);

end // while not EOF

r = $fcloser(file);

$stop;

end // initial

always #(cycle / 2) clock = !clock; // Clock generator

and #4 (out, a, b); // Circuit under test

endmodule // compare

10、从文件中读数据到mem(这个好像一般人用的最多了)

`define EOF 32'HFFFF_FFFF

`define MEM_SIZE 200_000

module load_mem;

integer file, i;

reg [7:0] mem[0:`MEM_SIZE];

reg [80*8:1] file_name;

initial

begin

file_name = "data.bin";

file = $fopenr(file_name);

i = $fread(file, mem[0]);

$display("Loaded %0d entries \n", i);

i = $fcloser(file);

$stop;

end endmodule // load_mem

Introduction

This describes how you can read and write files in a Verilog model using a set of user functions, based on the C stdio package. With these functions you can perform file input and output directly in Verilog models without having to learn C or the PLI. This code works with VCS, MTI, V erilog-XL, and NC-Verilog (see $fread for one restriction).

Note that Synopsys' VCS 6.1, NC-V erilog 3.3, and MTI's ModelSim 5.5 offer native support for the IEEE-1364 2001 standard. Verilog-XL does not support these tasks except through this PLI application.

Overview

This application note describes how your V erilog model or testbench can

read text and binary files to load memories, apply stimulus, and control simulation. Files can also be written. The format of the file I/O functions

is based on the C stdio routines, such as fopen, fgetc, fprintf, and fscanf.

The V erilog language has a rich set of system functions to write files ($fdisplay, $fwrite, etc.) but only reads files with a single, fixed format ($readmem). In the past if you wanted to read a file that was not in $readmem format, you would have to learn the Programming Language Interface (PLI)

and the C language, write C code to read the file and pass values into

Verilog, then debug the combined C and V erilog code. In addition, the Verilog

is limited to 32 open files at a time.

However, using the new file I/O system functions you can perform your

file I/O directly from V erilog. Y ou can write V erilog HDL to:

?

read stimulus files to apply patterns to the inputs of a model ?

read a file of expected values for comparison with your model ?

read a script of commands to drive a simulation

?

read either ASCII or binary files into Verilog registers and memories ?

have hundreds of log files open simultaneously (though they are written

to one at a time)

Code for all the examples in this file is included in the examples directory

for the file I/O functions.

Note that these system tasks behave the same as the equivalent stdio routines. For example, $fscanf will skip over white-space, including blank lines,

just like fscanf(). Y ou can prototype code in C then convert it to V erilog.

Differences between fileio and IEEE-1364 Verilog-2001

(V2K) standard

The following list describes the differences between my file I/O package (fileio) and the IEEE-1364 Verilog-2001 standard (V2K).

1) In fileio $fopen has read, write, append variants:

file = $fopenr("filename");

file = $fopenw("filename");

file = $fopena("filename");

In V2K, there is a single $fopen for both multi-channel descriptors (MCD) and file descriptors (FD). Whether an FD or MCD is produced

is indicated by the presence of a mode string added to $fopen in V2K:

file = $fopen("filename", "w");

// FD

file = $fopen("filename");

// MCD

Fileio supports the V2K $fopen format under a package compilation switch but that then blocks any use of MCDs since it hides the builtin $fopen.

2) Fileio $fclose has read and write variants that return a status:

r = $fcloser(file);

r = $fclosew(file);

In V2K, there is a single $fclose for both MCDs and FDs. It does

not return a status. Errors can be determined by using $ferror.

Fileio supports the V2K $fclose format under a package compilation switch but that then blocks any use of MCDs since it hides the builtin $fclose.

3) Fileio $getchar is not directly supported in Verilog-2001.

The operation can be done by using $fgetc('h8000_0000) which makes use of the reserved FD for stdin.

4) Fileio defines $fgets as:

r = $fgets(string, n, file);

V2K does not support a maximum count "n" of characters to read. Input

in V2K always terminates at end of line and then string assignment to the target is done.

Fileio's $gets is not directly supported in Verilog 2001. The

operation can be done by using:

$fgets(string, 'h8000_0000);

that makes use of the reserved FD for stdin.

5) Fileio $scanf is not directly supported in V erilog 2001. The operation can be done by using:

$fscanf('h8000_0000, format, args);

which makes use of the reserved FD for stdin.

6) Fileio does not support ? as an alias for X; V2K does.

7) Fileio does not support reading X or Z for %d format specification;

V2K does.

8) Fileio supports %f, but not the synonyms %e and %g. V2K supports

all three. Fileio does not support %f on in $sscanf; V2K supports

all specifiers in $sscanf.

9) Fileio does not support %u, %z, %v, %t, or %m input format specifiers;

V2K supports all of them.

10) Fileio supports special character input handling for \ (i.e. \\,

\oNNN); V2K does not support this (not in LRM).

11) Fileio requires that $fread on a memory use "mem[0]" as the memory referend. V2K requires "mem" since "mem[0]" will be taken as a register read.

12) Fileio defines $sprintf and $fprintf which are not defined in V2K.

V2K defines the $swrite family of tasks for string ouput and allows both MCDs and FDs in the $fwrite, $fdisplay, $fmonitor, and $fstrobe families

of tasks. V2K supports $sformat where the difference in $sformat

and $swrite is in the management of format specification strings.

Fileio requires a single format string in $sprintf, etc; V2K follows the normal Verilog convention of treating any constant strings as format specifiers for $swrite. In V2K, all output format specifications are consistent

and produce the same result independent of whether the target is a string, file, or standard output.

13) Fileio $ferror only returns a status. V2k $ferror takes a

second parameter and stores the error string in that register. Additionally,

V2K $ferror accepts a file descriptor with the value 0 and simple produces the most recent system error status.

14) Fileio requires an argument to $fflush; V2K permits a parameterless

call and flushes all files (including MCD files) in that case. V2K

$fflush supports either MCDs or FDs.

15) V2K supports $rewind which Fileio does not.

16) Fileio supports $fputc which V2K does not.

17) Fileio supports $feof which V2K does not. Some functions

such as $fgetc return EOF (-1) but this is not the same.

File Input Functions

The file I/O system functions and tasks are based on the C stdio routines.

For more information on the stdio routines, consult a C manual. The major

differences between these system tasks and C are caused by the lack of

a pointer variable in the Verilog language. Strings in V erilog are stored

in registers, with 8 bits needed to store a single character.

OPEN A FILE

integer file;

file = $fopenr("filename");

file = $fopenw("filename");

file = $fopena("filename");

The function $fopenr opens an existing file for reading. $fopenw opens

a new file for writing, and $fopena opens a new file for writing where

any data will be appended to the end of the file. The file name can be

either a quoted string or a reg holding the file name. If the file was

successfully opened, it returns an integer containing the file number (1..MAX_FILES) or NULL (0) if there was an error. Note that these functions are not the

same as the built-in system function $fopen which opens a file for writing

by $fdisplay. The files are opened in C with 'rb', 'wb', and 'ab' which

allows reading and writing binary data on the PC. The 'b' is ignored on

Unix.

CLOSE A FILE

integer file, r;

r = $fcloser(file);

r = $fclosew(file);

The function $fcloser closes a file for input. $fclosew closes a file for

output. It returns EOF if there was an error, otherwise 0. Note that these

are not the same as $fclose which closes files for writing.

TEST FOR END OF FILE

integer file;

reg eof;

eof = $feof(file);

The function $feof tests for end of file. If an end-of-file has been reached

while reading from the file, a non-zero value is returned; otherwise, a

0 is returned.

RETURN FILE ST ATUS

integer file;

reg error;

error = $ferror(file);

The function $ferror returns the error status of a file. If an error

has occurred while reading from a file, $ferror returns a non-zero value, else 0. The error value is returned once, then reset to 0.

READ A SINGLE CHARACTER

integer file, char;

char = $fgetc(file);

char = $getc();

The function $fgetc reads a single character from the specified file and returns it. If the end-of-file is reached, $fgetc returns EOF. Y ou should use a 32-bit register to hold the result from $fgetc to tell the difference between the character with the value 255 and EOF. $getc reads from stdin.

PUSH BACK A CHARACTER

integer file;

reg [7:0] char, r;

r = $ungetc(char, file);

The function $ungetc pushes the character back into the file stream. That character will be the next read by $fgetc. It returns the character if

it was successfully pushed back or EOF if it fails.

Note that since there is no $ungetc for stdin in C, there will not be

one in the file I/O package.

WRITE A SINGLE CHARACTER

integer stream, r, char;

r = $fputc(stream, char);

The function $fputc writes a single character to the specified

file. It returns EOF if there was an error, 0 otherwise.

READ A STRING

integer file, n, r;

reg [n*8-1:0] string;

r = $fgets(string, n, file);

r = $gets(string);

The function $fgets reads a string from the file. Characters are read

from the file into string until a newline is seen, end-of-file is reached,

or n-1 characters have been read. If the end-of-file is encountered, $fgets

returns a 0 and string is unchanged; otherwise, $fgets returns a 1. $gets

reads from stdin.

The function $gets is no longer supported by default in fileio v3.4.

If you want to use it, you must compile fileio.c with -DGETS. This is

because some C compilers will give an error message when compiling

fileio.c:

fileio.o: In function `fileio_gets_call`:

fileio.o: the gets function is dangerous and should not be used

Y ou can either ignore this message, or stop using -DGETS to remove the

gets function call from fileio.c.

READ FORMATTED TEXT

integer file, count;

count = $fscanf(file, format, args);

count = $sscanf(string, format, args);

count = $scanf(format, args);

The function $fscanf parses formatted text from the file according

to the format and writes the results to args. $sscanf parses formatted

text from a string. $scanf parses formated text from stdin. See a C reference manual for detailed information on fscanf, plus examples later in this

note.

The format can be either a string constant or a reg. It can contain:

?

Whitespace characters such as space, tab (\t), or newline (\n). One

or more whitespace characters are treated as a single character, and can

match zero or more whitespace characters from the input.

?

Conversion specifications which start with a %. Next is an optional

*, which suppresses assignment. Then is an optional field width in decimal.

Lastly is the operator character as follows:

?

b -- Binary values 0, 1, X, x, Z, z, _

?

d -- Decimal values 0-9, _, no X, x, Z, or z. Not

e that negative numbers

are NOT supported because of a V erilog language limitation.

?

o -- Octal values 0-7, _, X, x, Z, z

?

h or x -- Hexadecimal values, 0-9, A-F, a-f, _, X, x, Z, z

?

c -- A single character

?

f -- A floatin

g point number, no _, X, x, Z, or z

?

s -- A string

?

% -- The percent character

?

Other characters which must match the characters read from the file.

Special characters are \" for the " character, \\ for the \ character,

\oNNN is a single character whose ASCII value is specified by the octal

number NNN, and %% for the character %.

The args is an optional list of registers to be assigned by $fscanf,

$sscanf, and $scanf. There must be a register for each conversion operator (except those with %*). Bit subscripts are ignored.

Formatting & padding is closer to V erilog than C. For example,

%x of 16'h24 is '0024', not '24', and %0x returns '24', not '0024'.

$fscanf, $sscanf, and $scanf return the number of successful assignments performed. If you do not want a return value from these routines, compile fileio.c (and veriuser.c for Cadence users) with -Dscanf_task. VCS users should switch from fileio.tab to fileio_task.tab

FIND THE FILE POSITION

integer file, position;

position = $ftell(file);

The function $ftell returns the position in the file for use by $fseek.

If there is an error, it returns a -1.

POSITION A FILE

`define SEEK_SET 0

`define SEEK_CUR 1

`define SEEK_END 2

integer file, offset, position, r;

r = $fseek(file, 0, `SEEK_SET); /* Beginning */

r = $fseek(file, 0, `SEEK_CUR); /* No effect */

r = $fseek(file, 0, `SEEK_END); /* End of file */

r = $fseek(file, position, `SEEK_SET); /* Previous loc */

The function $fseek allows random access in a file. Y ou can position

at the beginning or end of a file, or use the position returned

from $ftell.

READ BINARY DATA

integer r, file, start, count;

reg [15:0] mem[0:10], r16;

r = $fread(file, mem[0], start, count);

r = $fread(file, r16);

The function $fread reads a binary file into a Verilog memory. The

first argument is either a register or a memory name, which must have a subscript, though the value of the subscript is ignored. start and count

are optional.

By default $fread will store data in the first data location through

the final location. For the memory up[10:20], the first location loaded

would be up[10], then up[11]. For down[20:10], the first location would

be down[10], then down[11].

start and count are ignored if $fread storing data in a reg instead

of a memory. No warning is printed if the file contains more data than

will fit in the memory.

start is the word offset from the lowest element in the memory.

For start = 2 and the memory up[10:20], the first data would be loaded

at up[12]. For the memory down[20:10] , the first location loaded would

be down[12], then down[13].

$fread returns the number of elements read from the file, If $fread terminates early because of an error, it will return the number of elements successfully read. $feof can be used to determine whether the end-of-file

was reached during $fread.

The data in the file is broken into bytes according to the width

of the memory. An 8-bit wide memory takes one byte per location, while

a 9-bit wide memory takes 2 bytes. Care should be taken when using memories with widths not evenly divisible by 8 as there may be gaps in the data

in the memory vs. data in the file.

The $fread system task only works with NC-Verilog if you use the

-MEMPACK switch as in:

ncverilog +ncvlogargs+-NOMEMPACK foo.v

WRITING A FORMATTED STRING

integer file, r, a, b;

reg [80*8:1] string;

file = $fopenw("output.log");

r = $sformat(string, "Formatted %d %x", a, b);

r = $sprintf(string, "Formatted %d %x", a, b);

r = $fprintf(file, "Formatted %d %x", a, b);

The functions $sformat and $sprintf writes a formatted string into

a Verilog register. The two functions are identical. $fprintf writes

a formatted string to a file. It has most, but not the formatting

capabilites of C stdio package. The first argument is a register

to receive the formatted data, and the second is a format string.

Additional arguments may be included.

The supported formats include b, c, d, e, f, g, h, m, o, r, s, and

x. %t for printing formatted time is NOT supported yet.

FLUSHING THE FILE STREAM

integer file, r;

file = $fopenw("output.log");

r = $fflush(file);

The function $fflush(stream) causes any buffered data waiting to be

written for the named stream to be written to that file. If the stream

is 0, all files open for writing are flushed.

Restrictions and Caveats

Y ou should be aware of following restrictions in using these V erilog functions vs. the stdio functions in C, which are imposed by the V erilog language :

?

Because these are V erilog system functions, you must always use the

return value as in:

r = $fscanf(...)

?

Verilog does not allow assignments inside a conditional. Thus the C

code fragment:

while (c=fgetc(stream) != EOF) {

}

turns into the V erilog code:

c = $fgetc(file);

while (c !== `EOF)

begin

c = $fgetc(file);

end

?

$fgets and $gets return only a single bit, 0 = error, 1 = no error,

unlike fgets / gets in C, which return a string pointer.

?

$fread is very different from fread in C. The order of arguments is

different, and the arguments are oriented towards writing to a V erilog

memory instead of a C character string. See page 5 for more info.

?

$fscanf can not be used to read binary files, or any files with null

characters. $fprintf can not be used to write binary files with null characters.

Because of the string processing that $fscanf uses, a null in the middle

of an ASCII field will prematurely terminate the field.

In addition, $save / $restart are loosely supported with VCS on SunOS.

In a test, $feof never returned EOF when running from a save file, but

$fgetc did. On other simulators and hardware platforms you can mimic these functions using $ftell and $fseek to find the position in a file and later

jump to that location.

The maximum number of files (MAX_FILES) is set in the C code to

12. The maximum string size is 1000 characters. There is no known limit

to the number of conversion operators in $fscanf or $sscanf.

Reading pattern files

This first example shows how to read input stimulus from a text file.

This is the pattern file - read_pattern.pat , included in the examples directory:

// This is a pattern file

// time bin dec hex

10: 001 1 1

20.0: 010 20 020

50.02: 111 5 FFF

62.345: 100 4 DEADBEEF

75.789: XXX 2 ZzZzZzZz

Note that the binary and hexadecimal values have X and Z values, but these are not allowed in the decimal values. Y ou can use white space when formatting your file to make it more readable. Lastly, any line beginning with a / is treated as a comment.

The module read_pattern.v reads the time for the next pattern from

an ASCII file. It then waits until the absolute time specified in the input file, and reads the new values for the input signals (bin, dec, hex). The time in the file is a real value and, when used in a delay, is rounded according to the timescale directive. Thus the time 75.789 is rounded to 75.79 ns.

`timescale 1ns / 10 ps

`define EOF 32'hFFFF_FFFF

`define NULL 0

`define MAX_LINE_LENGTH 1000

module read_pattern;

integer file, c, r;

reg [3:0] bin;

reg [31:0] dec, hex;

real real_time;

reg [8*`MAX_LINE_LENGTH:0] line; /* Line of text read from file */

initial

begin : file_block

$timeformat(-9, 3, "ns", 6);

$display("time bin decimal hex");

file = $fopenr("read_pattern.pat");

if (file == `NULL) // If error opening file

disable file_block; // Just quit

c = $fgetc(file);

while (c != `EOF)

begin

/* Check the first character for comment */

if (c == "/")

r = $fgets(line, `MAX_LINE_LENGTH, file);

else

begin

// Push the character back to the file then read the next time

r = $ungetc(c, file);

r = $fscanf(file," %f:\n", real_time);

// Wait until the absolute time in the file, then read stimulus

if ($realtime > real_time)

$display("Error - absolute time in file is out of order - %t", real_time);

else

#(real_time - $realtime)

r = $fscanf(file," %b %d %h\n",bin,dec,hex);

end // if c else

c = $fgetc(file);

end // while not EOF

r = $fcloser(file);

end // initial

// Display changes to the signals

always @(bin or dec or hex)

$display("%t %b %d %h", $realtime, bin, dec, hex); endmodule // read_pattern

Comparing outputs with expected results

The following model, compare.v, reads a file containing both stimulus and expected results. The input signals are toggled at the beginning of a clock cycle and the output is compared just before the end of the cycle.

`define EOF 32'hFFFF_FFFF

`define NULL 0

`define MAX_LINE_LENGTH 1000

module compare;

integer file, r;

reg a, b, expect, clock;

wire out;

reg [`MAX_LINE_LENGTH*8:1];

parameter cycle = 20;

initial

begin : file_block

$display("Time Stim Expect Output");

clock = 0;

file = $fopenr("compare.pat");

if (file == `NULL)

disable file_block;

r = $fgets(line, MAX_LINE_LENGTH, file); // Skip comments

r = $fgets(line, MAX_LINE_LENGTH, file);

while (!$feof(file))

begin

// Wait until rising clock, read stimulus

@(posedge clock)

r = $fscanf(file, " %b %b %b\n", a, b, expect);

// Wait just before the end of cycle to do compare

#(cycle - 1)

$display("%d %b %b %b %b", $stime, a, b, expect, out);

$strobe_compare(expect, out);

end // while not EOF

r = $fcloser(file);

$stop;

end // initial

always #(cycle / 2) clock = !clock; // Clock generator

and #4 (out, a, b); // Circuit under test

endmodule // compare

Reading script files

Sometimes a detailed simulation model for a device is not available, such as a microprocessor. As a substitute, you can write a bus-functional model which reads a script of bus transactions and performs these actions. The following, script.v, reads a file with commands plus data values.

软件测试基本点(参考文件资料资料资料)

一、功能测试 1、对话框测试输入进行测试。包括日文字符、英文字符、数字字符、特殊字符、及几种字符的组合。 2、对界面可操作按钮进行测试。包括【新增(N)】【保存(S)】【修改(M)】【查询(A)】【打印(P)】【退出(X)】。同时需要对鼠标右键的菜单进行测试。 3、数据保存测试。将1 和2 进行组合。 4、必要条件控制测试。在做了3 时将必要条件(如:a、编号、姓名不可为空b、编号、姓名不可重复)控制测试联合起来。 二、图形界面测试 1.窗体是否能够基于相关的输入或菜单命令适当的打开 2.窗体是否能够改变大小、移动和滚动 3.窗体的数据是否能够利用鼠标、功能键、方向箭头和键盘操作 4.当窗体被覆盖并重新调用后,窗体是否能够正确再生 5.窗体相关的功能是否可以操作 6.是否显示相关的下拉菜单、工具条、滚动条、对话框、按钮、图标和其他控制,既能正确显示又能调用 7.显示多窗体时,窗体名称是否能够正确表示 8.活动窗体是否能够被反显加亮 9.多用户联机时所有窗体是否能够实时更新 10.鼠标无规则点击时是否会产生无法预料的结果 11.窗体声音及提示是否符合既定编程规则 12.窗体是否能够被关闭 13.窗体控件的大小、对齐方向、颜色、背景等属性的设置值是否和程序设计规约相一致 14.窗体控件布局是否合理、美观 15.窗体控件 TAB 顺序是否从左到右,从上到下 16.窗体焦点是否按照编程规范落在既定的控件上 17.窗体画面文字(全、半角、格式、拼写)是否正确 18.鼠标有多个形状时是否能够被窗体识别(如漏斗状时窗体不接受输入)

三、功能测试就是对产品的各功能进行验证,根据功能测试用例,逐项测试,检查产品是否达到用户要求的功能。常用的测试方法如下: 1.页面链接检查:每一个链接是否都有对应的页面,并且页面之间切换正确。 2.相关性检查:删除/增加一项会不会对其他项产生影响,如果产生影响,这些影响是否都正确。 3.检查按钮的功能是否正确:如update, cancel, delete, save 等功能是否正确。 4.字符串长度检查: 输入超出需求所说明的字符串长度的内容, 看系统是否检查字符串长度,会不会出错. 5.字符类型检查: 在应该输入指定类型的内容的地方输入其他类型的内容(如在应该输入整型的地方输入其他字符类型),看系统是否检查字符类型,会否报错. 6.标点符号检查: 输入内容包括各种标点符号,特别是空格,各种引号,回车键.看系统处理是否正确. 7.日文字符处理: 在可以输入日文的系统输入日文,看会否出现乱码或出错. 8.检查带出信息的完整性: 在查看信息和update 信息时,查看所填写的信息是不 是全部带出.,带出信息和添加的是否一致 9.信息重复: 在一些需要命名,且名字应该唯一的信息输入重复的名字或ID,看系 统有没有处理,会否报错,重名包括是否区分大小写,以及在输入内容的前后输入空格,系统是否作出正确处理. 10.检查删除功能:在一些可以一次删除多个信息的地方,不选择任何信息,按”delete”,看系统如何处理,会否出错;然后选择一个和多个信息,进行删除,看是否正确处理. 11.检查添加和修改是否一致: 检查添加和修改信息的要求是否一致,例如添加要求必填的项,修改也应该必填;添加规定为整型的项,修改也必须为整型. 12.检查修改重名:修改时把不能重名的项改为已存在的内容,看会否处理,报错.同时,也要注意,会不会报和自己重名的错. 13.重复提交表单:一条已经成功提交的纪录,back 后再提交,看看系统是否做了处理。 14.检查多次使用back 键的情况: 在有back 的地方,back,回到原来页面,再back,重复多次,看会否出错.

C语言读写文件操作

C语言读写文件操作 #include #include #include FILE *stream;//, *stream2; FILE *stream2; void main( void ) { int numclosed; char *list; list="这个程序由czw编写"; //试图打开文件data.txt,如果该文件不存在,则自动创建 if( (stream= fopen( "data.txt", "r" )) == NULL ) { printf( "试图打开'data.txt'\n" ); printf( "'data.txt'不存在\n" ); printf( "'data.txt'被创建\n" ); } else printf( "'data.txt'被打开\n" ); //以写入方式打开 if( (stream2 = fopen( "data.txt", "w+" )) == NULL ) printf( "'data.txt'不存在\n" ); else { printf( "'data.txt'成功被打开\n" ); fwrite(list,strlen(list),30,stream2); printf("写入数据成功\n"); } //如果文件data.txt存在,就会打开成功,则stream!=NULL,这时就关闭stream if (stream!=NULL) if( fclose( stream) ) printf( "文件流 stream 被关闭\n" ); //关闭所有打开的文件流,返回关闭的文件流个数 numclosed = _fcloseall( );

DICOM数据集与DCM文件格式

作者简介:全海英(1971-),讲师,博士研究生,主要研究方向:医学信号与图像处理、小波分析; 杨源(1976-),硕士研究生,主要研究方向:数字图像处理; 张歆东(1970-),硕士,主要研究方向:多媒体、信号处理; 郭树旭(1959-),教授,博士研究生,主要研究方向:多媒体、数 字图像处理与传输、小波分析、微波通讯; 刘景鑫(1967-),工程师,主要研究方向:医学影像设备学. 文章编号:1001-9081(2001)08-0145-02 DICOM 数据集与DC M 文件格式 全海英1,3,杨 源1,张歆东1,郭树旭1,刘景鑫2 (1.吉林大学电子工程系,吉林长春130023; 2.长春市中日联谊医院,吉林长春130031; 3.中国科学院长春光学精密机械与物理研究所,吉林长春130021)摘 要:该文在介绍医学信息领域的一种通用的图像及数据通讯标准DIC OM3.0的基础上,对DIC OM 数据集和DC M 文件的组织形式进行了分析,并且提出了在实际应用中对DIC OM 数据集的编解码接口的实施方案。 关键词:DIC OM3.0;医学图像;文件格式中图分类号:TP311.52 文献标识码:A 1 前言 随着信息技术的发展和计算机应用水平的不断提高,新一代医疗信息系统已逐步发展成为面向医疗服务,集成医疗信息、医学影象信息和医疗管理信息的综合化多媒体医院管理信息系统[3]。 为了便于影象信息的共享和交流,美国放射学会 (American C ollege of Radiology ,ACR )和美国国家电器制造商协会(National E lectrical Manu factures Ass ociation ,NE M A )联合制定了医学数字图像通讯标准ACR/NE M A DIC OM 3.0(Digital Imaging and C ommunications in Medicine )[1],其主要目的是为了在各种医疗影象产品之间提供一致性接口,以便更有效地在医学影象设备之间传输交换数字影象[2,3]。目前,世界上主要的医疗设备生产厂家都采用此标准作为医学影象设备的互操作接口及医学影象数字接口[4]。 2 DICOM 数据集 2.1 实体-联系(Entity -Relationship ,简称E -R )模型 概念模型是现实世界事物及其在信息世界的反映, DIC OM 表达概念模型最常用的方法是实体-联系方法。2.2 数据集(Data Set ) 一个数据集描述了现实世界信息对象的一个实例。数据集由数据元素(Data E lements )构成。数据元素是对对象属性值的编码。 1)数据元素结构 数据元素是由数据元素标签(Data E lement T ag )唯一定义的。数据元素在一个数据集中按标签值逐渐增大的顺序存放,且除了嵌套数据集外,任何一个数据元素在一个数据集中只出现一次。一个数据元素必为三种结构之一。其中两种结构包含了数据元素的VR (Value Representation ,值类型表述)即显式VR (Explicit VR )],但是它们的值长度的表达是不一样的。另外一种结构不包含数据元素的VR 即隐式VR (Im plicit VR )。这三种结构都包含了数据元素标签、数据值长度(Value Length )、数据值体(Value Field )。 2) 数据元素字段 图1 DICOM 数据集与数据元素结构 一个数据元素由若干字段组成,如图1所示。它至少包含 三项内容:数据元素标签、数据值长度、数据值体。VR 字段仅出现于两类显式VR 数据元素结构中。各字段的定义如下: 数据元素标签 一个16bits 无符号整数对,按顺序排列包括群号码和元素号码。 值类型表述(VR ) 是2Bytes 的字符串,是数据元素的VR 值。已给定数据元素标签的VR 在数据词典中有详细定义。这两字节的VR 用DIC OM 的默认字符集编码。 数据值长度 一个16或32bits (取决于显式或隐式VR )无符号整数,表明了准确的数据值体的长度,按字节数目(为偶数)记录。此长度不包含数据元素标签、VR 、值长度字段。一个32bit 的长度字段(FFFFFFFFH )表示未定义的数据值体长度。未定义长度可能被用于VR 为项目序列(Sequence of I tems ,S Q )类型的数据元素。对于值类型为OW (Other W ord S tring )或OB (Other Byte S tring )的数据元素,未定义长度则依赖于传输句法协议。 数据值体 其长度为偶数字节,表明了数据元素的值。该字段的数据类型是由数据元素的VR 所明确定义。值多重性(Value Multiplicity ,VM )指定了该类型的数据值体中可包括的取值的数目。如果VM 大于1,表明元素是多重取值的。未定义长度的数据值体通过序列定界项目界定。 3 DC M 文件 符合DIC OM 标准的文件通常后缀为.dcm ,大多数的图像 处理软件中都不支持这种图像格式。因此,了解.dcm 文件格式的详细内容对于读写及其它处理都是十分必要的。 DIC OM 文件格式提供了一种在一个文件中封装数据集的方法。这个数据集体现了一个与DIC OM I OD 相联系的S OP 实例,它被放置在DIC OM 文件元信息之后。每一个文件包括 第21卷第8期2001年8月 计算机应用C om puter Applications V ol.21,N o.8Aug.,2001

Dicom文件中图像参数详解

DICOM文件可以大致分为两部分: 一部分:与图像相关的元信息,包括患者信息,检查信息,序列信息,图像信息等等。 另一部分:图像的像素数据。 在解析DICOM文件中的像素数据的时候,我们先需要读取以下图像相关信息: 以下是某个CT影像中的图像信息示例: (0028,0002) Samples per Pixel VR: US Length: 2 Value: 1 (0028,0004) Photometric Interpretation VR: CS Length: 12 Value: MONOCHROME2 (0028,0010) Rows VR: US Length: 2 Value: 512 (0028,0011) Columns VR: US Length: 2 Value: 512 (0028,0030) Pixel Spacing VR: DS Length: 22 Value: 0.48828125\0.48828125 (0028,0100) Bits Allocated VR: US Length: 2 Value: 16 (0028,0101) Bits Stored VR: US Length: 2 Value: 12 (0028,0102) High Bit VR: US Length: 2 Value: 11 (0028,0103) Pixel Representation VR: US Length: 2 Value: 0 (0028,1050) Window Center VR: DS Length: 12 Value: 00100\00100 (0028,1051) Window Width VR: DS Length: 12 Value: 00500\00500 (0028,1052) Rescale Intercept VR: DS Length: 6 Value: -1000 (0028,1053) Rescale Slope VR: DS Length: 2 Value: 1 (0028,2110) Lossy Image Compression VR: CS Length: 2 Value: 01 (0028,2112) Lossy Image Compression Ratio VR: DS Length: 8 Value: 6.228918 1.(0028,0002) Samples per Pixel 每一个像素的取样数,一般来说,CT,MR,DR等灰度图像都是1,而彩超等彩**图像都是3,分别表示R, G, B三个颜色通道。 2.(0028,0004) Photometric Interpretation 我们经常碰到的Photometric Interpretation有以下几种类型: Monochrome2 一般的灰度图像都采用这种,Pixel值越大,图像就越白。

C#中的文件读写操作详解

C#中的文件读写操作详解(摘自互动维客:https://www.sodocs.net/doc/8f1247380.html,,更多内容 请访问互动维客!) C#中的文件操作详解 微软的.Net框架为我们提供了基于流的I/O操作方式,这样就大大简化了开发者的工作。因为我们可以对一系列的通用对象进行操作,而不必关心该I/O操作是和本机的文件有关还是和网络中的数据有关。.Net框架主要为我们提供了一个System.IO命名空间,该命名空间基本包含了所有和I/O操作相关的类。 本文将向大家介绍一些基本的文件操作方法,包括对文件系统中的目录和文件的操作,还有就是文件的读写操作等。通过运用System.IO.DirectoryInfo类和System.IO.FileInfo类我们可以轻易的完成与目录和文件相关的操作,而通过运用System.IO.StreamReader类和System.IO.StreamWriter类我们可以方便的完成与文件的读写相关的操作。 命名空间概览 下面的表格显示了System.IO命名空间中最重要的一些类,通过运用这些类我们就能完成基本的文件操作。 表1 类名功能和用途 BinaryReader、BinaryWriter 读写二进制数据 Directory、File、DirectoryInfo以及FileInfo 创建、删除并移动目录和文件,通过属性获取特定目录和文件的相关信息 FileStream 以随机方式访问文件 MemoryStream 访问存储在内存中的数据 StreamReader 、StreamWriter 读写文本数据信息 StringReader、StringWriter 运用字符串缓冲读写文本数据信息 运用DirectoryInfo类和FileInfo类 DirectoryInfo类和FileInfo类的基类都是FileSystemInfo类,这个类是一个抽象类,也就是说你不可以实例化该类,只能通过继承产生其子类并实例化其子类。然而你却可以运用由该类定义的各种属性,下面的表格显示了该类已经定义了的各种属性。 表2 属性功能和用途

软件测试中的43个功能测试点

软件测试中的43个功能测试点 功能测试就是对产品的各功能进行验证,根据功能测试用例,逐项测试,检查产品是否达到用户要求的功能,针对web系统我们有哪些常用测试方法呢?今天我们一起来了解了解~~ 1. 页面链接检查 每一个链接是否都有对应的页面,并且页面之间切换正确。可以使用一些工具,如:LinkBotPro、File-AIDCS、HTMLLink Validater、xenu等工具。LinkBotPro不支持中文,中文字符显示为乱码;HTMLLink Validater只能测试以Html或者htm结尾的网页链接;xenu无需安装,支持asp、do、jsp等结尾的网页,xenu测试链接包括内部链接和外部链接,在使用的时候应该注意,同时能够生成html格式的测试报告。 2.相关性检查 功能相关性:删除/增加一项会不会对其它项产生影响,如果产生影响,这些影响是否都正确,常见的情况是,增加某个数据记录以后,如果该数据记录某个字段内容较长,可能会在查询的时候让数据列表变形。 3.检查按钮的功能是否正确 如新建、编辑、删除、关闭、返回、保存、导入、上一页、下一页、页面跳转、重置等功能是否都正确。常见的错误会出现在重置按钮上,表现为功能失效。 4.字符串长度检查 输入超出需求所说明的字符串长度的内容,看系统是否检查字符串长度。还要检查需求规定的字符串长度是否都正确,有时候会出现,需求规定的字符串长度太短而无法输入业务数据。 5.字符类型检查 在应该输入指定类型的内容的地方输入其他类型的内容(如在应该输入整型的地方输入其他字符类型)看系统是否检查字符类型。 6.标点符号检查 输入内容包括各种标点符号,特别是空格,各种引号,回车键。看系统处理是否正确。常见的错误是系统对空格的处理,可能添加的时候,将空格当作一个字符,而在查询的时候空格被屏蔽,导致无法查询到添加的内容。

matlab文件操作及读txt文件(fopen,fseek,fread,fclose

matlab文件操作及读txt文件(fopen,fseek,fread,fclose) matlab文件操作 文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件。MATLAB提供了一系列低层输入输出函数,专门用于文件操作。 1、文件的打开与关闭 1)打开文件 在读写文件之前,必须先用fopen函数打开或创建文件,并指定对该文件进行的操作方式。fopen函数的调用格式为: fid=fopen(文件名,‘打开方式’) 说明:其中fid用于存储文件句柄值,如果返回的句柄值大于0,则说明文件打开成功。文件名用字符串形式,表示待打开的数据文件。常见的打开方式如下:λ‘r’:只读方式打开文件(默认的方式),该文件必须已存在。 ‘r+’:读写方式打开文件,打开后先读后写。该文件必须已存在。λ λ‘w’:打开后写入数据。该文件已存在则更新;不存在则创建。 ‘w+’:读写方式打开文件。先读后写。该文件已存在则更新;不存在则创建。λ λ‘a’:在打开的文件末端添加数据。文件不存在则创建。 λ‘a+’:打开文件后,先读入数据再添加数据。文件不存在则创建。 另外,在这些字符串后添加一个“t”,如‘rt’或‘wt+’,则将该文件以文本方式打开;如果添加的是“b”,则以二进制格式打开,这也是fopen函数默认的打开方式。

2)关闭文件 文件在进行完读、写等操作后,应及时关闭,以免数据丢失。关闭文件用fclose 函数,调用格式为: sta=fclose(fid) 说明:该函数关闭fid所表示的文件。sta表示关闭文件操作的返回代码,若关闭成功,返回0,否则返回-1。如果要关闭所有已打开的文件用fclose(‘all’)。 2、二进制文件的读写操作 1)写二进制文件 fwrite函数按照指定的数据精度将矩阵中的元素写入到文件中。其调用格式为:COUNT=fwrite(fid,A,precision) 说明:其中COUNT返回所写的数据元素个数(可缺省),fid为文件句柄,A用来存放写入文件的数据,precision代表数据精度,常用的数据精度有:char、uchar、int、long、float、double等。缺省数据精度为uchar,即无符号字符格式。 例6.8 将一个二进制矩阵存入磁盘文件中。 >> a=[1 2 3 4 5 6 7 8 9]; >> fid=fopen('d:\test.bin','wb') %以二进制数据写入方式打开文件 fid =3 %其值大于0,表示打开成功 >> fwrite(fid,a,'double') ans = 9 %表示写入了9个数据 >> fclose(fid)

dicom读取方法

Dicom格式文件解析器 学数字图像与通讯,这里讲的暂不涉及通讯那方面的问题只讲*.dcm 也就是diocm格式文件的读取,读取本身是没啥难度的无非就是字节码数据流处理。只不过确实比较繁琐。 分析 整体结构先是128字节所谓的导言部分,说俗点就是没啥意义的破数据跳过就是了,然后是dataElement依次排列的方式就是一个dataElement接一个dataElement的方式排到文件结尾通俗的讲dataElement就是指tag 就是破Dicom标准里定义的数据字典。tag是4个字节表示的前两字节是组号后两字节是偏移号比如0008,0018。所有dataElement在文件中都是按tag排序的 比如0002,0001 0002,0002 0003,0011 文件整体结构如下: 又把论文里的这图贴上来总结的很好。单个dataElement的结构如下: 显示VR:VR为OB OW OF UT SQ UN的元素结构 显示VR:VR为普通类型时元素结构(少了预留那一行) 隐式VR 时元素结构

要问VR是啥东东,值表示法啥叫值表示法啊俺不懂 int string short ushort 懂不就是这个意思,Dicom标准真坑爹非要整个怪怪的概念。 VR总共27个跟c#值类型对应关系我都写好了: 1string getVF(string VR, byte[] VF) 2 { 3string VFStr = string.Empty; 4switch (VR) 5 { 6case"SS": 7 VFStr = BitConverter.ToInt16(VF, 0).ToString(); 8break; 9case"US": 10 VFStr = BitConverter.ToUInt16(VF, 0).ToString(); 11 12break; 13case"SL": 14 VFStr = BitConverter.ToInt32(VF, 0).ToString(); 15 16break; 17case"UL": 18 VFStr = BitConverter.ToUInt32(VF, 0).ToString(); 19 20break; 21case"AT": 22 VFStr = BitConverter.ToUInt16(VF, 0).ToString(); 23 24break; 25case"FL": 26 VFStr = BitConverter.ToSingle(VF, 0).ToString(); 27 28break; 29case"FD": 30 VFStr = BitConverter.ToDouble(VF, 0).ToString(); 31 32break; 33case"OB": 34 VFStr = BitConverter.ToString(VF, 0); 35break; 36case"OW": 37 VFStr = BitConverter.ToString(VF, 0);

Matlab的各种数据读取、文件读写等操作汇总

Matlab 的各种数据读取、文件读写等操作汇总 MATLAB 提供了多种方式从磁盘读入文件或将数据输入到工作空间,即读取数据,又叫导入数据;将工作空间的变量存储到磁盘文件中称为存写数据,又叫导出数据。至于选择哪种机制,则根据下面两个因素决定:?用户所执行的 操作是导入数据还是导出数据;?数据的格式为文本格式、 二进制格式还是如HDF 之类的标准格式。将数据导入MATLAB 中最容易的方法就是使用导入数据模板(Import Wizard) ,使用该模板时不需要知道数据的格式,只需指定包含这些数据的文件,然后导入模板会自动处理文件内容。本章重点内容如下:? 文件的打开和关闭? 文本文件的读取?存写ASCII数据?二进制数据的读取? 二进制数据的存写? 使用I/O文件函数进行数据读写?MAT 文件的读写 2.1 文件的打开和关闭2.1.1 文件的打开无论是要读写ASCII 码文件还是二进制文件,都必须先用fopen 函数将其打开,在默认情况下,fopen 以二进制格式打开文件,它的使用语法如下:fopen ('filename', 'mode') 其中filename 表示要读写的文件名称,mode 则表示要对文件进行的处理方式,如下:rt :以只读方式(Reading)打开文件wt:以只写方式(Writing)打开文件at:以追加方式(Appending)打开文件,新内容将从原文件后面续写r+t:以同时读写方式打开文件w+t :以同时读写创建文件,原文件内容被清除

a+t :以同时读和追加(Reading and Appdending) 方式,原文件内容被保留,新内容将从原文件的后面开始At :以读写方式打开或创建文件,适用于对磁带介质文件的操作Wt :以写入方式打 开或创建文件,原文件内容被清除,适用于磁带介质文件的操作fopen 函数有两个返回值,一个是返回一个文件标志(file Identifier) ,它会作为参数被传入其他对文件进行读写操作的命令,通常是一个非负的整数,可用此标识来对此文件进行各种处理。如果返回的文件标识是-1,则代表fopen无法打开文件,其原因可能是文件不存在,或是用户无法打开此文件权限。另一个返回值就是message ,用于返回无法打开文件的原因。为了安全起见,最好在每次使用fopen 函数时,都测试其返回值是否为有效值。下面以脚本m 文件为例来声明文件的打开。例 2-1 %exam1.m[f,message]=fopen('fileexam1', 'r')if f==-1disp (message); % 显示错误信息end 若文件fileexam1 不存在,则显示如下信息。Cannot open file.existence?permissions?memory?... 例2-2 %exam2.m[f,message]=fopen('fileexam2', 'r');if f==-1disp (message); % 显示错误信息else disp(f);end 若文件fileexam2 存在,则返回f值。 2.1.2 文件的关闭一旦完成文件的读写,最好关闭文件,以便对其进行其他操作。这时就可以使用fclose 函数来关闭文件,其适用语法如下:fclose(f) 。其中 f 为打开文件的标志,若fclose 函数返回值为0 ,则表示成功关闭 f 标志的文件;若返回值为-1,

软件测试中的43个功能测试点

软件测试中的43个功能测试点软件测试 功能测试就是对产品的各功能进行php?name=%D1%E9%D6%A4">验证,根据功能测试用例,逐项测试,检查产品是否达到用户要求的功能。针对web系统的常用测试方法如下: 1. 页面链接检查:每一个链接是否都有对应的页面,并且页面之间切换正确。可以使用一些工具,如LinkBotPro、File-AIDCS、HTML Link Validater、Xenu等工具。LinkBotPro不支持中文,中文字符显示为乱码;HTML Link Validater只能测试以Html或者htm结尾的网页链接;Xenu无需安装,支持asp、do、jsp等结尾的网页,xenu测试链接包括内部链接和外部链接,在使用的时候应该注意,同时能够生成html格式的测试报告。如果系统用QTP进行自动化测试,也可以使用QTP的页面检查点检查链接。 2. 相关性检查:功能相关性:删除/增加一项会不会对其他项产生影响,如果产生影响,这些影响是否都正确,常见的情况是,增加某个数据记录以后,如果该数据记录某个字段内容较长,可能会在查询的时候让数据列表变形。 数据相关性:下来列表默认值检查,下来列表值检查,如果某个列表的数据项依赖于其他模块中的数据,同样需要检查,比如,某个数据如果被禁用了,可能在引用该数据项的列表中不可见。 3. 检查按钮的功能是否正确:如新建、编辑、删除、关闭、返回、保存、导入,上一页,下一页,页面跳转,重置等功能是否正确。常见的错误会出现在重置按钮上,表现为功能失效。 4. 字符串长度检查: 输入超出需求所说明的字符串长度的内容, 看系统是否检查字符串长度。还要检查需求规定的字符串长度是否是正确的,有时候会出现,需求规定的字符串长度太短而无法输入业务数据。 5. 字符类型检查: 在应该输入指定类型的内容的地方输入其他类型的内容(如在应该输入整型的地方输入其他字符类型),看系统是否检查字符类型。 6. 标点符号检查: 输入内容包括各种标点符号,特别是空格,各种引号,回车键。看系统处理是否正确。常见的错误是系统对空格的处理,可能添加的时候,将空格当作一个字符,而在查询的时候空格被屏蔽,导致无法查询到添加的内容。 7.特殊字符检查:输入特殊符号,如@、#、$、%、!等,看系统处理是否正确。常见的错误是出现在% ‘" 这几个特殊字符 8. 中文字符处理: 在可以输入中、英文的系统输入中文,看会否出现乱码或出错。 9. 检查信息的完整性: 在查看信息和更新信息时,查看所填写的信息是不是全部更新,更新信息和添加信息是否一致。要注意检查的时候每个字段都应该检查,有时候,会出现部分字段更新了而个别字段没有更新的情况。

Java流(文件读写操作)

Java流 一、流的分类 ?按数据流动方向 –输入流:只能从中读取字节数据,而不能向其写出数据 –输出流:只能向其写入字节数据,而不能从中读取数据?按照流所处理的数据类型 –字节流:用于处理字节数据。 –字符流:用于处理Unicode字符数据。 ?按照流所处理的源 –节点流:从/向一个特定的IO设备读/写数据的流。(低级流)–处理流:对已存在的流进行连接和封装的流。(高级流)二、缓冲流 ?缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法。 ?J2SDK提供了四种缓存流: –BufferedReader –BufferedWriter –BufferedInputStream s –BufferedOutputStream

?缓冲输入流支持其父类的mark()和reset()方法: –mark()用于“标记”当前位置,就像加入了一个书签,可以使用reset()方法返回这个标记重新读取数据。?BufferedReader提供了readLine()方法用于读取一行字符串(以\r 或\n分隔)。 ?BufferedWriter提供了newLine()用于写入一个行分隔符。 ?对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()方法将会使内存中的数据立刻写出。 三、类层次 3.1、InputStream类层次

3.2、OutputStream类层次 3.3、Reader类层次

3.4、Writer类层次 四、常用的字符流与字节流的转化 说明: 1.字节流用于读写诸如图像数据之类的原始字节流。 2.字符流用于读写诸如文件数据之类的字符流。 3.低级流能和外设交流。 4.高级流能提高效率。 5.InputStreamReader 是字节流通向字符流的桥梁。 6.OutputStreamWriter 是字符流通向字节流的桥梁。

DICOM文件解读

接着昨天,对文件格式的分析: 0100h-0119h:这里的02001000将其进行字节转换后为00020010,这里标签的含义是UID,5549即为UI,值域长度为1200,将其字节转换后为0012即为18,表示后面18个字节都 是值的内容即1.2.840.10008.1.2。 011ah-0135h:这里的02001200将其进行字节转换后为00020012,值描述还是UI,值域长度为14 00即为20个字节,值的内容为1.2.40.0.12.0.9812.0。 0136h-0147h:这里的02001300将其进行字节转换后为00020013,其值描述为5348即为 SH(可见标准第五部分short string),0A00即值域长度为10,其值的内容为0.1B/WIN32。 0148h-015fh:这里的02001600将其进行字节转换后为00020016,其值描述为4145即为 AE,值域长度为1000字节顺序转换为0010即为16个字节长度,其值的内容为IRIS后跟 12个字节的空格(20)。 0160h-0171h:这里的08000500将其进行字节转换后为00080005,查找标准第六部分得知 其为特定字符集(Specific Character Set),0A00字节转换后000A知字节长度为10,后跟 两个字节的保留字,从0168h开始为ISO_IR100,表示ISO组织的一个记录号。 0172h-018fh:这里的08000800将其进行字节转换后为00080008,查找标准第六部分得知 其为图像类型(Image Type),同上一个数据元一样这个地方也没有表示值,1600字节转换 后0016得知字节长度为22,后跟两个字节的保留字,从017ah开始为ORIGINAL\PRIMARY\AXIAL,表示该图像的类型。 0190h-01b1h:这里的08001600将其进行字节转换后为00080016,查找标准第六部分得知 其为SOP服务类的UID(SOP Class UID),为什么这里就不需要判断VR而有标签直接得 出是属于UI呢?是隐形显示吗隐形显示与显性显示的区别如何从图像中看出来! 1A00转换字节为001A即长度为26,后跟两个字节的保留字,从0198h开始为 1.2.840.10008.5.1.4.1.1.2,与下面的00080018一起指得是与DICOM文件的传输识别有关的 识别码。 01b2h-01f1h:这里的08001800将其进行字节转换后为00080018,查找标准第六部分得知 其为SOP实例的UID(SOP Instance UID),3800进行字节转换为0038则为56个字节,后 跟两个保留字,从01bah开始为1.2.840.1136.19.2.81.290.23014.32126.1.2.20081217.250500。 01f2h-0201h:这里的08002000将其进行字节转换后为00080020,查找标准第六部分得知 其为研究日期(Study Date),0800转换后为0008即八个字节的长度,后跟两个字节的保 留字,从01fah开始其值内容为20081217。 0202h-0211h:这里的08002100将其进行字节的转换后为00080021,查找标准第六部分得 知其为系列日期(Series Date),0800即八个字节,后跟两个字节的保留字,从020ah开始 其内容20081217。 0212h-0221h:这里的08002200将其进行字节的转换后为00080022,查找标准第六部分得 知其为Acquisition Date,同上一样在021ah开始其值内容为20081217。 0222h-0231h:这里的08002300将其进行字节的转换后为00080023为图像日期(Image Date),同上一样在022ah开始其值内容为20081217。 0232h-023fh:这里的08003000将其进行字节的转换后为00080030为研究时间(Study Time),这里的值域长度为六个字节,后跟两个保留字,在023ah开始150437。 0240h-024dh:这里的08003100将其进行字节转换后为00080031为Series Time,同上一 样在0248h开始为150437。 024eh-0263h:这里的08003200将其进行字节转换后为00080032为Acquisition Time,0E 00得知其值域长度为14个字节,后面有两个保留字,从0256h开始值域内容为150453.564159。

CTF 文件上传检测的基本思路

文件上传检测的基本思路 1: 前台脚本检测扩展名—绕过 原理 当用户在客户端选择文件点击上传的时候,客户端还没有向服务器发送任何消息,就对本地文件进行检测来判断是否是可以上传的类型,这种方式称为前台脚本检测扩展名。 ?1绕过方法 . 绕过前台脚本检测扩展名,就是将所要上传文件的扩展名更改为符合脚本检测规则的扩展名,通过BurpSuite工具,截取数据包,并将数据包中文件扩展名更 改回原来的,达到绕过的目的。 . . 例如:文件名本来为【evil.jpg】,上传时,用BurpSuite截包后,将数据包中的名字改为【evil.php】(或其它脚本类型)即可。 . ?1 ?2 2: Content-Type检测文件类型—绕过 原理

当浏览器在上传文件到服务器的时候,服务器对说上传文件的Content-Type 类型进行检测,如果是白名单允许的,则可以正常上传,否则上传失败。 ?1绕过方法 绕过Content--Type文件类型检测,就是用BurpSuite截取并修改数据包中文件的Content-Type类型(如改为:image/gif),使其符合白名单的规则,达到上传的目的。 ?1 3: 文件系统00截断—绕过 原理 在上传的时候,当文件系统读到【0x00】时,会认为文件已经结束。利用00截断就是利用程序员在写程序时对文件的上传路径过滤不严格,产生0x00上传截断漏洞。 ?1绕过方法 通过抓包截断将【evil.php.jpg】后面的一个【.】换成【0x00】。在上传的时候,当文件系统读到【0x00】时,会认为文件已经结束,从而将【evil.php.jpg】的内容写入到【evil.php】中,从而达到攻击的目的。 ?1 4: 服务器端扩展名检测黑名单—绕过

MFC-txt文件读写

文本文件的读写 正确的文本文件读写过程 1.定义文件变量; 2.打开指定的文件; 3.向从文本文件中写入信息; 4.从文本文件中读取信息; 5.关闭文件 1、定义文件变量 定义文件变量格式:CStdioFile 文件变量; 例如,定义一个名称为f1的文件变量,语句如下:CStdioFile f1; 2、打开指定文件 可以直接通过CStdioFile的构造函数来打开磁盘文件,同时可以用标志位指定打开方式(只读、只写、读写等): CStdioFile(LPCTSTR lpszFileName,UINT nOpenFlags); 其中,lpszFileName表示要打开的文件名,可以是相对路径或绝对路径 nOpenFlags设置文件打开方式标志位,可以指定用“|”连接多个标志位。下面是常用的打开标志: CFile::typeText:以文本文件的形式打开文件 CFile::typeBinary:以二进制文件的形式打开文件 CFile::modeCreate:如果指定文件名的文件不存在,则新建文件;如果文件存在并且没有设置CFile::modeNoTruncate标志,则清空文件。 CFile::modeNoTruncate:如果文件存在,不把它的长度删除为0(即不清空文件中的数据)。

:以只读方式打开文件 CFile::modeReadWrite:以可读可写方式打开文件 CFile::modeWrite:以只写方式打开文件 CFile::shareDenyNone:文件打开后,不禁止其他进程对文件的读写操作CFile::shareExclusive:文件打开后,禁止其他进程对文件的读写操作CFile::shareDenyRead:文件打开后,禁止其他进程对文件的读操作

百度文库上传测试文档一

核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。核实:文档中是否出现乱码/图片、文字模糊或相互遮挡/大量空白页面等内容,影响文档正常阅读。

相关主题