• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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!!
0
bzzoy
Asked:
bzzoy
  • 8
  • 6
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now