2D-Array Write into a file

Status
Not open for further replies.

sresam89

Member level 2
Joined
Sep 9, 2009
Messages
48
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,576
Hi all,
I am stuck in a code part


Code Verilog - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
reg [3:0] memory[0:255];
.....
initial begin
$readmemb("out.txt", memory,0,255);//this is reading fine
end
.....
always@(posedge clk) begin
 $display("%d\n",address);
 descriptor = $fopen("in.txt","w");
 $fwrite(descriptor,"%b\n",memory[address]);            //how do i write in here? [3X255]
 $display("%b : %b\n",address,memory[address][0:3]);  //also display the same here
 #210 $fclose(descriptor);
end


please help

SAM
 
Last edited by a moderator:

You're opening the file every clock cycle? you should move the $fopen to an initial block.

This line is using the wrong bit ordering:
Code:
 $display("%b : %b\n",address,memory[address][0:3]);  //also display the same here
you should be using:
Code:
 $display("%b : %b\n",address,memory[address][3:0]);  //also display the same here

You could also display and write to a file if you use multichannel file descriptors, which you would or together. see: http://www.testbench.in/TB_26_FILE_HANDLING.html

Regards
 

You can use like this...

#include <iostream>
#include <fstream>
using namespace std;

int main() {
char arr[][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char arr2[3][3];
ofstream out("io3.txt");
out.write(arr[0],9);
out.close();
ifstream in("io3.txt");
in.read(arr2[0],9);
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
cout << arr2[j];
cout << endl;
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…