Solved

# int to string size issues

Posted on 2004-11-14
233 Views
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
Question by:bzzoy

Author Comment

err i should add that the file number can be pretty large
0

LVL 5

Expert Comment

make sure u are allocating a big enough buffer.
what is INT_MAX (maximum integer calue on
0

Author Comment

INT_MAX is from the limits.h library.  value is like 32767.  how would i allocate a big enough buffer?
0

LVL 5

Accepted Solution

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

Author Comment

that did it!  thanks a ton.  oh can you exp;lain (n /= 10) for me?
0

LVL 5

Expert Comment

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

Author Comment

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

LVL 5

Expert Comment

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

Author Comment

nope still giving me an extra letter or thing at the end of it
0

LVL 5

Expert Comment

hmm.. may be you should post all of your code
0

Author Comment

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

Author Comment

anything else you can find wrong with that please let me know
0

LVL 5

Expert Comment

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

Author Comment

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

### Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.