1

I am using a tcl script which takes a movie file trace and convert it into binary file which is further used by the application agent in ns-2. Here is the code snippet of the script which converts the movie file trace into binary file:

set original_file_name Verbose_Silence_of_the_Lambs_VBR_H263.dat
set trace_file_name video.dat
set original_file_id [open $original_file_name r]
set trace_file_id [open $trace_file_name w]
set last_time 0
while {[eof $original_file_id] == 0} {
    gets $original_file_id current_line
    if {[string length $current_line] == 0 ||
    [string compare [string index $current_line 0] "#"] == 0} {
       continue  
    }

    scan $current_line "%d%s%d" next_time type length
    set time [expr 1000*($next_time-$last_time)]
    set last_time $next_time
    puts -nonewline $trace_file_id [binary format "II" $time $length]
}
close $original_file_id
close $trace_file_id 

But when I used this created video.dat file further for traffic generation used by application agent I got the following error:

Bad file siz in video.dat
Segmenatation fault

Kindly have a loot at this. what is the meaning of binary format "II" in the code. as I have not found it mentioned in tcl-binary(n) documentation or is it outdated and not supported now.

user976754
  • 361
  • 1
  • 4
  • 19
  • So finally I have done it. Actually the record in trace file for ns-2 Application/Traffic/Trace contains 2 32-bit integer in network byte order. The first integer gives the time in microsecond and second integer gives the packet size in byte. The tcl script binary format "II" could not convert it in this format as I checked by reading it. So what I did I just wrote a C program and convert the movie file trace in the required binary format and then used it in the ns-2 simulation and it worked. – user976754 Feb 28 '13 at 12:23

1 Answers1

1

The problem is probably that you don't open your file in binary mode.

Change

set trace_file_id [open $trace_file_name w]

to

set trace_file_id [open $trace_file_name wb]

Otherwise Tcl will change the output, e.g. replaces \n with \r\n on windows. (And for byte values > 127 it will be treated as unicode code point, then converted to your system encoding and thereby messing up your entire binary stuff) While such things are fine for text files, it generates problems with binary files.

Fortunately only a single character is needed to fix that: b as modifier for open

Edit: I just looked up in the change list for Tcl, the b modifier for open was added with 8.5. I usually only use 8.5 or 8.6, so if you are using an older version of Tcl, add the following line after the open:

fconfigure $trace_file_id -translation binary

The b modifier is just a shortcut for that.

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73