09-28-2012 09:17 AM
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",®_name_tmp ,®_add_hex_tmp,®_val);
reg_val_int[counter++] = reg_val_int_tmp;
}
There was always error.
Can somebody help me ?
09-28-2012 09:29 AM
What error?
09-28-2012 09:36 AM
i got : NON FATAL RUN TIME ERROR: Attempt to write beyond end of string.
09-28-2012 09:49 AM
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 )
09-28-2012 09:50 AM - edited 09-28-2012 09:50 AM
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);
}
}
09-28-2012 09:55 AM
...or more simple:
counter = 0;
while (fgets (file_line, 200, file_handle) != NULL)
{
Scan (file_line, "%s[xt32]%x[x]%d", reg_name, ®_add_hex_tmp, ®_val[counter++]);
}