strf length-limitation

Posted By: Grant

strf length-limitation - 08/18/17 14:15

Greetings all,

I'm working on a script to create a data set for R which requires a lot of input variables. As a result, the total character length per record exceeds the 1000 characters limit from the strf function. Is there a workaround for this?

for better readability I'm limiting the length of my code, but it looks like this

Code:
for(i=0; i < 10; i++)
{
file_append(name,strf(
				"n%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,etc...",
DS, (DS + IH), (DS + IH - 10), (DS + IH - 20), (DS + IH - 30), (DS + IH - 40), (DS + IH - 50), (DS + IH - 60), (DS + IH - 70), (DS + IH - 80), (DS + IH - 90),etc...));

DS = DS - 1;
}



Thanks for any feedback!

Grant
Posted By: jcl

Re: strf length-limitation - 08/18/17 16:38

Use not strf, but the equivalent C function sprintf, with a string of the desired length that you allocated before with malloc.
Posted By: Grant

Re: strf length-limitation - 08/20/17 16:49

Thank you for your suggestion, JCL.

I'm not familiar yet with this sprintf function, so I had to look it up.

I've tried the following simplified code, but now it seems that there's a hard limit for the number of individual arguments from the sprintf function: 123 max. Is this correct?

Once I try to compile my script with more arguments, it results in syntax error with a reference to the sprintf function.

Code:
function run()
  {
  char buffer[2000];
  int DS = 1500;
  string name = "DataExport.csv";
	
  if(is(FIRSTRUN))
    for(i=0; i < 10; i++)
      {
      sprintf(buffer,"n%i,etc..", DS, etc..);
      file_append(name, buffer);	
      DS = DS - 1;
      }
  }

Posted By: jcl

Re: strf length-limitation - 08/21/17 10:01

I don't know if there's a hard limit, but 123 is an awfully high number of arguments to a function. It is quite possible that you went where mankind has never been before. At least I never heard of a C function called with 123 arguments. I believe my own maximum was 18 arguments. Beyond, all sorts of things can happen.
Posted By: Grant

Re: strf length-limitation - 08/21/17 14:13

I was able to throw it in a for-loop. It's definitely not the fastest way, but I only need this routine during the initialization of the model.
Once again thanks for your input!

Grant



Code:
function run()
{
char buffer[2500];
int DS = 1500, i, i2, i3, IH = 60;	
	
if(is(FIRSTRUN))
  for(i=0; i < 10; i++)
    {
    i3 = sprintf(buffer, "n%i", DS);
    for (i2=0; i2 < 144; i2++)
      {
      i3 += sprintf(buffer + i3, "%s", ",");
      i3 += sprintf(buffer + i3, "%i", DS + IH - (10 * i2));
      }
    file_append(name, buffer);
    DS = DS - 1;
    }
}

© 2024 lite-C Forums