File read operations

Hi,

I want to read two kinds of data (data and key) from two different files in the sequence and send those to the driver. The problem is that the data file has 500 lines whereas key file has 100 lines. What is the best way to read both the files properly.
I have written this code but getting extra data content while reading the file.

                       file_read_data = $fopen("stimulus/data.txt", "r");
                       file_read_key = $fopen("stimulus/key_get_data.txt","r");
                           while($fscanf (file_read_data, "%h", plain_text) == 1) && ($fscanf(file_read_key, "%h", cipher_key) == 1))
                              begin
                                 $display("plain_text = %h", plain_text);
                                 trans.valid_in = 1; 
                                 trans.data = plain_text;     
                                 trans.key = cipher_key;   
                                 start_item(trans);
                                 finish_item(trans);                                   
                              end

In reply to shankar_logic:

Your code looks like it should exit the while loop when you get to the end of the key file. Is that what you want it to do? What do you mean by “extra data content”? Do you mean it reads the 101st line into plain_text? What is being read into cipher_key?

Extra data means that if i am having 100 lines in one file (containing data) and 500 lines in another file(containing key), then in that case, while executing the above loop the last data is read more times.

In reply to shankar_logic:

This is what you have written. The expression is valid for more than 100 entries.
If you want to see you have to change the expression in your while loop, i.e. checking only plain_texxt == 1. Inside the loop you can check for the cipher_key.

In reply to shankar_logic:

Then what you need to do is restructure your loop to save the return codes from each $fscanf and check them aside the loop

while( (data_code = $fscanf(file_read_data, "%h", plain_text)) == 1) || // need to OR
       (key_code =  $fscanf(file_read_key, "%h", cipher_key))  == 1))
                              begin
                                 if (data_code == 1)
                                   trans.data = plain_text;     
                                 else
                                   ...; // do what ever you need to do
                                 if (key_code == 1)
                                   trans.key = cipher_key;     
                                 else
                                   ...; // do what ever you need to do 
                                 ...;
                              end

I tried this but i am getting 100 lines of data and 100 lines of key. This is an issue because I need to read 500 lines of data and 100 lines of key

In reply to shankar_logic:

This sounds confusing. You said above your data-file is only 100 lines and the key file is 500 lines. Do you mean you want to wrap aroundin the data file reading this several times?

No, data.txt has 500 lines whereas key.txt has 100 lines. So, I want to read both the files till the end of these files and send each line of the file to the driver. Now, I am facing the issue that only 100 lines are read from both the files whereas the requirement is to read 500 lines from another file.

In reply to shankar_logic:

I made a mistake in my example, you need to OR the two $fscanfs not AND. Your transaction only has content from one file.