LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read text line and save values into array

Hello,

 

maybe it sounds so stupid, but i have frustrated for hours with this task.

 

i have a text file, which containts data which following format:

 

Reg1_name   08DE   23234

Reg2_name   09DE   13827

Reg3_name   0ADE   19283

.....

 

i want to read the line and save the values at the end of each line into an array.

My code:

 

char reg_name_tmp;

unsigned int reg_add_hex_tmp;
unsigned int reg_val_tmp;

 

char reg_name[19];
unsigned int reg_add_hex[19];
unsigned int reg_val[19];

 

file_handle = fopen (file_path, "r");               

while(fgets(file_line,200,file_handle)!=NULL)
                {
                    Scan(file_line,"%s    %x   %d",&reg_name_tmp ,&reg_add_hex_tmp,&reg_val);
                    reg_val_int[counter++] = reg_val_int_tmp;
                }

 

 

There was always error.

Can somebody help me ?

0 Kudos
Message 1 of 6
(3,556 Views)

What error?

0 Kudos
Message 2 of 6
(3,553 Views)

i got : NON FATAL RUN TIME ERROR: Attempt to write beyond end of string. 

0 Kudos
Message 3 of 6
(3,550 Views)

If you have an error message, you can try to get some explanation in the help, see http://zone.ni.com/reference/en-XX/help/370051V-01/cvi/libref/cviruntime_errors_and_warnings/ It will tell you that your output array is smaller than the given format specifiers and input parameters require;

 

In the given code there is only one place with a format specifier, and only one instance of a string: You have

 

char reg_name_tmp;

 

but this is a single character while you probably want to read the string 'Reg1_name'... Try to use reg_name instead...

 

(If you report an error in the future is it even better to specify the line where the error occurs, it may not always be obvious Smiley Wink)

0 Kudos
Message 4 of 6
(3,545 Views)

hi, thank for ur replies.

i have found a work around by using "strtok"

Here is the code, in case somebody interests

              

               char * token;

               file_handle = fopen (file_path, "r");
                while(fgets(file_line,100,file_handle)!=NULL)
                {
                    
                    unsigned int intern_cnt = 0;
                    token = strtok(file_line,"\t");
                    while(token != NULL)
                    {
                        token = strtok(NULL,"\t");
                        intern_cnt++;
                        if(intern_cnt == 2)
                            reg_val_int[counter++] = atoi(token);    
                    }

                }

0 Kudos
Message 5 of 6
(3,544 Views)

...or more simple:

 

counter = 0;
while (fgets (file_line, 200, file_handle) != NULL)
   {
      Scan (file_line, "%s[xt32]%x[x]%d", reg_name, &reg_add_hex_tmp, &reg_val[counter++]);
   }



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 6
(3,540 Views)