• C

int to string size issues

well i managed to get a dynamically changing file name given a number (ie myfile_3.txt), using the sprintf thing,
however if i enter in a value > 99 i get different letters at the end of the .txt part (ie myfile_3.txa).  i realize i have to allocate a correctly sized memory section for this but im not sure how to.

name = (char *)malloc(sizeof(INT_MAX+1));
sprintf(name, "myfile_%d.txt", number);

thanks!!
bzzoyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bzzoyAuthor Commented:
err i should add that the file number can be pretty large
0
van_dyCommented:
make sure u are allocating a big enough buffer.
what is INT_MAX (maximum integer calue on
your machine) ????
0
bzzoyAuthor Commented:
INT_MAX is from the limits.h library.  value is like 32767.  how would i allocate a big enough buffer?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

van_dyCommented:
well you dont need to allocate that much memory.
try this:


#include <stdio.h>
#include <stdlib.h>

int main()
{
      int number = 3423545;  //any number
      int size = 0;
      int n = number;
      char *p;

      while(n /= 10)
            ++size;

      size += 11;  //for myfile_ and .txt
      p = malloc(size);
      sprintf(p, "myfile_%d.txt", number);        // now p will contain the name of your file
      printf("%s\n", p);        //print out to check
      return 0;    
}

hope this helps,
van_dy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bzzoyAuthor Commented:
that did it!  thanks a ton.  oh can you exp;lain (n /= 10) for me?  
0
van_dyCommented:
its just another way to write

while(n = n/10)
      size++;

every time through the while loop,
n is divided by 10 and the value is stored
back in n. this is done till n is not equal
to 0. when n becomes zero, the value of
expression in the while() is zero, the loop
terminates. eventually, what u have is the
number of digits in the integer stored in size.
0
bzzoyAuthor Commented:
i hope you're still reading this, for some reason its still adding characters to the back of the file so im still getting myfile_35.txt? or myfile_11.txt1
0
van_dyCommented:
ummhh,
      i dont see why should that happen,
well, replace the line
     p = malloc(size);
in my previous post with
      p = calloc(size + 1, 1);

see if it works now.
0
bzzoyAuthor Commented:
nope still giving me an extra letter or thing at the end of it
0
van_dyCommented:
hmm.. may be you should post all of your code
0
bzzoyAuthor Commented:
size is the number that is supposed to go into the file format (ie size is 5, so should create magic-5.txt).  it works for 1-9, however at 11 i get magic-11.txt1, and going above that just gets me similar things at the end.  

int saveTable(int** table, int size){
  int i, j;
  char *fileName;
  int** magSquare;
  int allocSize = size;
  int n = 0;
  FILE *outFile;
  while (allocSize = allocSize/10){
    ++n;
  }
  n += 10;
  fileName = calloc(n +1, 1);
  sprintf(fileName, "magic-%d.txt\0", size);
  magSquare = (int **)malloc(size * sizeof(int));
  for(i = 0; i < size; i++) {
    magSquare[i] = (int *)malloc(size * sizeof(int));
  }
  magSquare = table;
  printf("%d\n", magSquare[4][4]);
  printf("%s\n", fileName);
  if ((outFile = fopen(fileName, "w")) == NULL) {
    printf("Error: unable to save %s\n", fileName);
    return 0;
  }
  for (i = 0; i < size; i++){
    for (j = 0; j < size; j++){
      if (table[i][j] <= 9){
        fprintf(outFile, "0%d\t", table[i][j]);
      }
      else{
        fprintf(outFile, "%d\t", table[i][j]);
      }
    }
    fprintf(outFile, "\r\n");
  }
  fclose(outFile);
  return 1;
}
0
bzzoyAuthor Commented:
anything else you can find wrong with that please let me know
0
van_dyCommented:
>> int saveTable(int** table, int size)   //is table a two dimensional array ?, you will have to pass its second dimension,
                                                               //there is a difference betwen double pointers and two dimensional arrays
suppose, table is a 2 dimensional array and is declared in the main function as

int table[10][10];

when u pass table to a function you will have to pass it like,

saveTable(table[][10], size);

and the function should be written as


 int saveTable(int table[][10], int size)
{
           .....
while (allocSize = allocSize/10){
    ++n;
  }
  n += 11;         //not 10
  fileName = calloc(n +1, 1);
  sprintf(fileName, "magic-%d.txt", size);       //why use \0 ???, just keep it this way


for(i = 0; i < size; i++) {
    magSquare[i] = (int *)malloc(size * sizeof(int));     //why allocate memory when u are going to assign it to table ??
  }
  magSquare = table;         //here is where u are assigning
0
bzzoyAuthor Commented:
ok it seems to be working now.  it was that i had n+= 10.  i thought that since magic- and .txt was only 10 thats what i needed.  anyways thanks for the help once again :)  you rock
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.