how to cantenate strings in C

H

Helen

Hi there

I am planning to
open a lot of files name test1.txt, test2.txt ...
But I don't know how to change integer to be string
like 1 to char'1' and cantenate them with test and .txt

thanks a lot!
 
A

Aggro

Helen said:
Hi there

I am planning to
open a lot of files name test1.txt, test2.txt ...
But I don't know how to change integer to be string
like 1 to char'1' and cantenate them with test and .txt

/* Make sure you have enough room in your result char array. */
char result[100];

int filenumber = 10;

sprintf( result, "%s%d.txt", "filename", filenumber );
printf( "%s\n", result );
 
R

Randy Howard

Helen said:
Hi there

I am planning to
open a lot of files name test1.txt, test2.txt ...
But I don't know how to change integer to be string
like 1 to char'1' and cantenate them with test and .txt
concatenate?


/* Make sure you have enough room in your result char array. */
char result[100];

Shouldn't this bear some resemblance to the maximum length of
a filename on the machine and any bizarre semantics about the
characters in the filename? (8.3 perhaps).
int filenumber = 10;

sprintf( result, "%s%d.txt", "filename", filenumber );

How do you check to make sure there is room? In a real implementation,
"filename" might be
"whatevertheuserhappenedtoenterwhenpromptedforafilenamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoftheoven"
depending on how the input is collected. That, plus some number, like
INT_MAX might make for a nice overflow with the above.

OTOH, if you know as the OP indicated that it will always be "test"%d,
you need only be sure that the strlen of "filenumber" (as a string) +
"test" + ".txt" + 1 isn't longer than result and that it does not violate
the filename conventions on the platform.
 
J

Jeff

Randy Howard said:
Helen wrote:
[snip]
/* Make sure you have enough room in your result char array. */
char result[100];

Shouldn't this bear some resemblance to the maximum length of
a filename on the machine and any bizarre semantics about the
characters in the filename? (8.3 perhaps).
int filenumber = 10;

sprintf( result, "%s%d.txt", "filename", filenumber );

How do you check to make sure there is room? In a real implementation,
"filename" might be
"whatevertheuserhappenedtoenterwhenpromptedforafilenamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoftheoven"
depending on how the input is collected. That, plus some number, like
INT_MAX might make for a nice overflow with the above.

OTOH, if you know as the OP indicated that it will always be "test"%d,
you need only be sure that the strlen of "filenumber" (as a string) +
"test" + ".txt" + 1 isn't longer than result and that it does not violate
the filename conventions on the platform.


--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: (e-mail address removed)

IMHO, the OP can decide that what size would be enough for the "char
result[]".

If buffer overflow is a problem, we can make use of malloc


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

void print_name(long filenumber)
{

char *result;
double var1;

var1 = log10(filenumber);

result = malloc((long)var1 + 4 + 1);
/* adding 1 for rounding double to long, adding 4 for ".txt", adding 1 for
terminating null character */

sprintf( result, "%s%d.txt", "file", filenumber );
puts(result);

free(result);

}

int main()
{
print_name(99999999);

return 0;
}
 
I

Irrwahn Grausewitz

Randy Howard said:
/* Make sure you have enough room in your result char array. */
char result[100];
int filenumber = 10;

sprintf( result, "%s%d.txt", "filename", filenumber );

How do you check to make sure there is room? In a real implementation,
"filename" might be
"whatevertheuserhappenedtoenterwhenpromptedforafilenamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoftheoven"
depending on how the input is collected. That, plus some number, like
INT_MAX might make for a nice overflow with the above.
In C99 one could use snprintf and dynamically allocate the buffer to
make sure there's enough space to hold the resulting string.
 
M

Martin Dickopp

Randy Howard said:
/* Make sure you have enough room in your result char array. */
char result[100];

Shouldn't this bear some resemblance to the maximum length of
a filename on the machine

That would make it harder to port to systems which don't have a fixed
limit on the filename length. IMHO, it's usually better to code under the
assumption that *nothing* is arbitrarily limited whenever possible.

Martin
 
T

Tom Zych

Randy said:
How do you check to make sure there is room? In a real implementation,
"filename" might be
"whatevertheuserhappenedtoenterwhenpromptedforafilenamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoftheoven"
depending on how the input is collected. That, plus some number, like
INT_MAX might make for a nice overflow with the above.

As posted[1], "filename" can only be "filename" :)

[1] Which is probably not what Aggro meant to type.
 
R

Randy Howard

expires-nov2003@zero- said:
That would make it harder to port to systems which don't have a fixed
limit on the filename length. IMHO, it's usually better to code under the
assumption that *nothing* is arbitrarily limited whenever possible.

I agree, provided that it doesn't actually have to work on one of
the aforementioned systems.
 
R

Randy Howard

Randy said:
How do you check to make sure there is room? In a real implementation,

As posted[1], "filename" can only be "filename" :)
[1] Which is probably not what Aggro meant to type.

Which is precisely why I said "real implementation" above.
 
C

Charles Richmond

Helen said:
Hi there

I am planning to
open a lot of files name test1.txt, test2.txt ...
But I don't know how to change integer to be string
like 1 to char'1' and cantenate them with test and .txt
You can "catentate" the strings...and you can "concatenate"
the strings. But there is *no* such thing as "cancatenate".
 
P

pete

Charles said:
You can "catentate" the strings...and you can "concatenate"
the strings. But there is *no* such thing as "cancatenate".

Worst spelling flame ever!
 
J

Joona I Palaste

Charles Richmond said:
You can "catentate" the strings...and you can "concatenate"
the strings. But there is *no* such thing as "cancatenate".

So? Please show me where Helen used the word "cancatenate".

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
J

Joe Wright

Charles said:
You can "catentate" the strings...and you can "concatenate"
the strings. But there is *no* such thing as "cancatenate".
She said "cantenate", and it's not nice to correct spilling errors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,204
Latest member
MalorieSte

Latest Threads

Top