simply string query

Galligan
Galligan used Ask the Experts™
on
Hi I know this is quite basic, but ..... I have a set of files, ordered numerically ie file001.xyz, file002.xyz........... file999.xyz.

Now, i'm when working with these file names - i want to access the numerical part and place it into a variable

so instead of  
fp = fopen("file001.xyz","r");

i could use
fp = fopen("fileVAR.xyz","r");   where VAR is "001"

essentially, the filename is a string, but part of the string being converted into an integer. Any advice gratefully received..... thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Is this what you seek?
char filename[]="file000.xyz"; /* allocate space */
int var;
...
sprintf(filename, "file%03d.xyz", var);
fd=fopen(filename,"r")
...

Be sure to allocate enough space for the filename...

Regards
Tomas

Commented:
first u must calculate the file no. u want..and put in variable say
..
int fileno;
...
char filename[];
char fstart[]="file";
char fend[]=".xyz";
filename=malloc([length_of_filename]*sizeof(char));
strset(filename,'\0');
strcpy(filename,fstart);
strcat(filename,(char)fileno);
strcat(filename,fend);

filename should now be complete....
Hi Galligan, I´m just a beginner, any my source must look crazy for the profis but it works:


#include <stdio.h>
#include <stdlib.h>
#include <CONIO.H>
#include <iostream.h>


int main(void)
{

int number =0;
char temp[10];
char filename[20];

for (int i=0;i<=10;i++)
     {
   number++;
   filename[0] = '\0';
     strcat(filename,"file");
     itoa(number,temp,10);
     strcat(filename,temp);
     cout << filename << endl;
   //or here you could place your:
   //fp = fopen("file001.xyz","r");
   }
getch();
}


Maybe its a help ;-)

bye
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

sorry, change the line //fp = fopen("file001.xyz","r");
to //fp = fopen(filename,"r");

Commented:
See zellerin's post for the simple way to do it.

Unfortunately, you may need a more general way to deal with this sort of problem. I have developped a handy alloc_printf() function :

Typical usage of this function is :

char *filename;
int i;

for(i=0; i<200; ++i)
{
  filename = alloc_printf("file%03d.xyz", i);
  if(filename==0) {
    // error
    fprintf(stderr, "Malloc error");
    break;
  }
  // deal with file

  if(filename) free(filename);
}


This function is very handy because it determines and allocates enough memory for the string.

The function :

char * malloc_printf(char *format, void *arg)
{
  int i=snprintf(0,0,format,arg);
  char *ret;

  if(i<=0) retrun 0;
  ret = malloc(i+1);
  if(ret==0) return 0;

  snprintf(ret,i+1,format,arg);
  return ret;
}

Note: Your compiler's function snprintf must return the number of needed bytes for the string if the buffer length is 0.
There is also a version of this function that uses variable arguments.

--Gruime (plea

Commented:
See zellerin's post for the simple way to do it.

Unfortunately, you may need a more general way to deal with this sort of problem. I have developped a handy alloc_printf() function :

Typical usage of this function is :

char *filename;
int i;

for(i=0; i<200; ++i)
{
  filename = alloc_printf("file%03d.xyz", i);
  if(filename==0) {
    // error
    fprintf(stderr, "Malloc error");
    break;
  }
  // deal with file

  if(filename) free(filename);
}


This function is very handy because it determines and allocates enough memory for the string.

The function :

char * malloc_printf(char *format, void *arg)
{
  int i=snprintf(0,0,format,arg);
  char *ret;

  if(i<=0) retrun 0;
  ret = malloc(i+1);
  if(ret==0) return 0;

  snprintf(ret,i+1,format,arg);
  return ret;
}

Note: Your compiler's function snprintf must return the number of needed bytes for the string if the buffer length is 0.
There is also a version of this function that uses variable arguments.

--Gruime (please don't forget to accept an answer)

Commented:
Seeing Gruime's post, it may be worthy saying that actually there is similar functionality already included in GNU libc (glibc) - function asprintf. It may or may not be useful for you, depending on your environment and portability intents...
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by zellerin.

Please post any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial