473,396 Members | 1,834 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,396 software developers and data experts.

Append a number to set of characters

29
Hello Everybody;
I would like to append a number to set of char, I do not like to use string, because In my project I am using several files.
What I did but there is a problem with it is as follows
1. char filename[30];
2. int x=2;
3. filename="xxxxx";

But i do not know how to append 2 to filename.
Thanks
Sep 13 '07 #1
8 1938
Savage
1,764 Expert 1GB
Hello Everybody;
I would like to append a number to set of char, I do not like to use string, because In my project I am using several files.
What I did but there is a problem with it is as follows
1. char filename[30];
2. int x=2;
3. filename="xxxxx";

But i do not know how to append 2 to filename.
Thanks
What's seems to be the most logical way to you?

How would you do it with string?

Savage
Sep 13 '07 #2
aeo3
29
What's seems to be the most logical way to you?

How would you do it with string?

Savage
With string we can use a function strcpy, but with character variable it is not available to do this
I mean, I would like to change my filename within a loop
filename1.txt
filname2.txt ...etc
Sep 13 '07 #3
Savage
1,764 Expert 1GB
With string we can use a function strcpy, but with character variable it is not available to do this
I mean, I would like to change my filename within a loop
filename1.txt
filname2.txt ...etc
But you can have two cstrings(you are working in c,right?),one will contain base filename and second it's number converted to char array using itoa.After that you just append second string to the first,and there you have it.

Savage
Sep 13 '07 #4
aeo3
29
But you can have two cstrings(you are working in c,right?),one will contain base filename and second it's number converted to char array using itoa.After that you just append second string to the first,and there you have it.

Savage
I was tried to use itoa, but compiler complains, Also, i am using c++ with Cgywin complier
Sep 13 '07 #5
Savage
1,764 Expert 1GB
I was tried to use itoa, but compiler complains, Also, i am using c++ with Cgywin complier
Can you show us that line of code?

Savage
Sep 13 '07 #6
aeo3
29
Can you show us that line of code?

Savage
This is the new trial that i am trying to use string but there is many problems so I thought to use char
Expand|Select|Wrap|Line Numbers
  1. string int2string(const int& number)
  2. {
  3.   ostringstream oss;
  4.   oss << number;
  5.   return oss.str();
  6. }
  7.  
  8.  
  9.  
  10. string FILENAME;
  11.  
  12.  for(int i=0;i<numEco;i++)
  13.  {
  14.     FILENAME="inf_h_";
  15.     FILENAME+=int2string(h);
  16.     FILENAME.append("_f_");
  17.     FILENAME+=int2string(f);
  18.     FILENAME.append("_");
  19.     FILENAME+=int2string(i);
  20.     FILENAME.append(".txt");
  21.    // strcpy(str,FILENAME);
  22.     EcoArray[i]->Run_Economy();
  23. }
  24.  
and here is an attempt but by using char

Expand|Select|Wrap|Line Numbers
  1.  int h=5;
  2.  char filename[30]="inf_"
  3.  filename=filename+h;
Sep 13 '07 #7
sicarie
4,677 Expert Mod 4TB
aeo3-

Please use code tags around your code, they are [code=cpp] and [/code]
Sep 13 '07 #8
Savage
1,764 Expert 1GB
This is the new trial that i am trying to use string but there is many problems so I thought to use char
Expand|Select|Wrap|Line Numbers
  1. string int2string(const int& number)
  2. {
  3.   ostringstream oss;
  4.   oss << number;
  5.   return oss.str();
  6. }
  7.  
  8.  
  9.  
  10. string FILENAME;
  11.  
  12.  for(int i=0;i<numEco;i++)
  13.  {
  14.     FILENAME="inf_h_";
  15.     FILENAME+=int2string(h);
  16.     FILENAME.append("_f_");
  17.     FILENAME+=int2string(f);
  18.     FILENAME.append("_");
  19.     FILENAME+=int2string(i);
  20.     FILENAME.append(".txt");
  21.    // strcpy(str,FILENAME);
  22.     EcoArray[i]->Run_Economy();
  23. }
  24.  
and here is an attempt but by using char

Expand|Select|Wrap|Line Numbers
  1.  int h=5;
  2.  char filename[30]="inf_"
  3.  filename=filename+h;

What are the problems with c++ strings?

If you want to use c style strings you cannot use operator+,if you try to use you will get something as :Invalid pointer arithmetic...

If you want to append then use strcat
Sep 13 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
9
by: JMCN | last post by:
hi- i have inherited an access 97 database that keeps track of the loans. i have been running into referential intergrity problems when i try to append new loans to table. first of all is a...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
3
by: todorov-fkt | last post by:
Hello Provided the following code: string temp = "Short string"; // 12 chars StringBuilder sb = new StringBuilder(); sb.Append(temp, 0, 30); How many characters is the string in sb? 12 or is...
8
by: Two Beards | last post by:
Hi, I'm new to VB and .net so I may be mising something. I am trying to dynamically build some SQL by appending to a string. Part of the SQL comes from an mp3 id tag I have extracted from a file...
4
by: pmacdiddie | last post by:
I have an append query that needs to run every time a line item is added to a subform. The append writes to a table that is the source for a pull down box. Problem is that it takes 5 seconds to...
3
by: Freddy Coal | last post by:
Hi, I would like append strings to a binary file, but I don´t understand how make that. I try with: FileOpen(1, Folder_Trabajo & "\Toma_Trazas.FC", OpenMode.Append, OpenAccess.Write,...
1
by: thanawala27 | last post by:
Hi, I wanted to append a cell of an Excel file. Basically, the excel file is not taking more than 256 characters for a cell at a time. So i thought of breaking the varaible into 2. n copying it...
3
by: nujcharee | last post by:
Hi I have a series of queries, I have a number of temp tables which I use as templates for my data. I start with 1. Delete the data in a temp table 2. Use append query to fill the temp table...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.