473,756 Members | 5,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Append a number to set of characters

29 New Member
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 1953
Savage
1,764 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Moderator Specialist
aeo3-

Please use code tags around your code, they are [code=cpp] and [/code]
Sep 13 '07 #8
Savage
1,764 Recognized Expert Top Contributor
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
24119
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 convert n = "" # init new string for i in str: if i in chr: # if special character in str n+='\\' # append it with a backslash n+=i
9
4515
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 linked table from another ms database. when i try to run the append query, i receive an error message that it cannot append all the records due to key violations to the . so i looked at all of the relationships and
1
2481
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 two csv files. (One for updated data, and the other for unupdated data.) The CSV files are attached to my Jobcosting database. After the CSV
3
2293
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 it filled with blanks to reach 30 chars?
8
4362
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 and stored in a string. I can do all the normal stuff with the string like msgbox and I can append it to another string but I cannot append to the end of it. I have tried things like trim and using a string builder but it still won't work. Any...
4
5766
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 run. This makes adding lines to an order too slow for the users. The result of the query provides real time availabilty, so I really do need this to work. The tblJobs has only 10,000 records, tblJobsLineItems has 150,000 records.
3
4822
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, OpenShare.Default) FilePut(1, "My string") '<****** ERROR IN THIS LINE ****** FileClose(1)
1
2180
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 one by one. But hte 2nd variable needs to be appended to the first variable in the Cell. I had posted something on related topic sometime back (http://www.thescripts.com/forum/thread682464.html), and i was able to find out that there is some...
3
3170
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 with data Now, in the temp table, I have a memo field. I append information from a text column (Oracle DB) into the memofield in the temp table. For some reason, only the first 255 characters copied across as if it
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.