no its not homework, and I don't want to use while statements. I want do you with some sort of string formating.
I don't want to fill up my code with unnecessary while, if or for statements every time I have to make a file name.
In that case sprintfI'm in c on Xcode
Is this homework by any chance? If so say so up front. Other than that I'd say look at while statements...
That's still one more function in a program loaded with them. I take it the answer to my original question is either no or probably not.
I'm not sure what is language appropriate here but guys like lee, lloyd and chown have been very helpful. You can go hide under a rock.
I'm not sure what is language appropriate here but guys like lee, lloyd and chown have been very helpful. You can go hide under a rock.
I didn't post my any original stab because I know how to do this with for statements, etc. I'm looking for some printf format or string routine that will this semi automatically.
this
sprintf(snum,"%3d", i + 1);
strcpy(file,cube);
strcat(file,snum);
adds blanks.
char ** makedatasetlist( char *root, int num, int reads)
{
char * cube, snum[5];
char **file;
int i;
if ( (cube =(char *)calloc(200, sizeof(char)))== NULL)
printf("No memory for cube\n");
if ( (file =(char **)calloc(reads - 1, sizeof(char*)))== NULL)
printf("No memory for filenames\n");
for( i = 0; i < reads; i++)
if ( (file[i] =(char *)calloc(100, sizeof(char)))== NULL)
printf("No memory for filenames\n");
strcpy(cube,root);
if ( num < 10)
strcat( cube, "00");
else
strcat(cube,"0");
sprintf(snum,"%d", num);
strcat(cube,snum);
strcat(cube,"_f");
for (i = 0; i < reads - 1; i ++)
{
strcpy(file[i],cube);
if ( i < 10)
strcat( file[i], "00");
else
strcat(file[i],"0");
sprintf(snum,"%d", i);
strcat(file[i],snum);
strcat(file[i],".fits");
printf("%s\n", file[i]);
}
return file;
}
There we go. The correct format string for sprintf is "%03d". I would still use a couple of if statements to protect against numbers <0 and >999 myself...Robbie
Sorry, that reply was to miles. And I didn't look at the link because I thought that if I had to I would just as soon go with if statements.
This link? NSNumberFormatter
no its not homework, and I don't want to use while statements. I want do you with some sort of string formating.
Kinky. And I'm flattered... but I'm just not that into you.
Edit: We should adopt the SSCCE mantra in this forum. Its much simpler than going through this with every new poster.