473,383 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

how to cantenate strings in C

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!
Nov 13 '05 #1
19 3152
Helen wrote:
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 );

Nov 13 '05 #2
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...
Helen wrote:
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
"whatevertheuserhappenedtoenterwhenpromptedforafil enamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoft heoven"
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: po********@sco.com
Nov 13 '05 #3

"Randy Howard" <ra**********@FOOmegapathdslBAR.net> wrote in message
news:MP************************@news.megapathdsl.n et...
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...
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
"whatevertheuserhappenedtoenterwhenpromptedforafil enamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoft heoven"
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: po********@sco.com


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;
}
--
Jeff
Nov 13 '05 #4
Randy Howard <ra**********@FOOmegapathdslBAR.net> wrote:
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...

<SNIP>

/* Make sure you have enough room in your result char array. */
char result[100]; <SNIP> 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
"whatevertheuserhappenedtoenterwhenpromptedforafi lenamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutof theoven"
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.

--
6 * 9 = 42 (base 13)
Nov 13 '05 #5
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...
Helen wrote:
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
"whatevertheuserhappenedtoenterwhenpromptedforafil enamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoft heoven"
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: po********@sco.com
Nov 13 '05 #6
Randy Howard <ra**********@FOOmegapathdslBAR.net> writes:
/* 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
Nov 13 '05 #7
Randy Howard wrote:
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...
sprintf( result, "%s%d.txt", "filename", filenumber );

How do you check to make sure there is room? In a real implementation,
"filename" might be
"whatevertheuserhappenedtoenterwhenpromptedforafil enamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoft heoven"
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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #8

"Randy Howard" <ra**********@FOOmegapathdslBAR.net> wrote in message
news:MP************************@news.megapathdsl.n et...
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...
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
"whatevertheuserhappenedtoenterwhenpromptedforafil enamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoft heoven"
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: po********@sco.com


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;
}
--
Jeff
Nov 13 '05 #9
Randy Howard <ra**********@FOOmegapathdslBAR.net> wrote:
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...

<SNIP>

/* Make sure you have enough room in your result char array. */
char result[100]; <SNIP> 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
"whatevertheuserhappenedtoenterwhenpromptedforafi lenamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutof theoven"
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.

--
6 * 9 = 42 (base 13)
Nov 13 '05 #10
Randy Howard <ra**********@FOOmegapathdslBAR.net> writes:
/* 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
Nov 13 '05 #11
Randy Howard wrote:
In article <qh***********@read3.inet.fi>, sp**********@yahoo.com says...
sprintf( result, "%s%d.txt", "filename", filenumber );

How do you check to make sure there is room? In a real implementation,
"filename" might be
"whatevertheuserhappenedtoenterwhenpromptedforafil enamejustbecauseicankeep
ontypinguntilIgetboredormypizzaisreadytotakeoutoft heoven"
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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #12
In article <bj*************@news.t-online.com>, expires-nov2003@zero-
based.org says...
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.


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

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 13 '05 #13
In article <3F***************@pobox.com>, tz******@pobox.com says...
Randy Howard wrote:
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.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 13 '05 #14
In article <bj*************@news.t-online.com>, expires-nov2003@zero-
based.org says...
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.


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

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 13 '05 #15
In article <3F***************@pobox.com>, tz******@pobox.com says...
Randy Howard wrote:
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.

--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 13 '05 #16
Helen wrote:

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

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Nov 13 '05 #17
Charles Richmond wrote:

Helen wrote:

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


Worst spelling flame ever!

--
pete
Nov 13 '05 #18
Charles Richmond <ri******@ev1.net> scribbled the following:
Helen wrote:
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".


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #19
Charles Richmond wrote:

Helen wrote:

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

She said "cantenate", and it's not nice to correct spilling errors.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
14
by: manstey | last post by:
Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.