Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to concatenate 2 values stored in a array?

Status
Not open for further replies.

muthuivs

Technical User
Jan 30, 2006
36
CA
Good Day,

As u can see I have been away from programming for a while and I have a task that desperately needs me finsh a program. I am trying to use the rename(arg1,arg2),feature of c to rename files on the system. Since the path to these files are long I need to be able to concatenate a few char arg[] into one char arg[]. It is not working though!! Any tips?


Muthu

PS: here is what I did

int main()
{
int result;
for (int counter = 101;counter<=450;counter++)
{
char str1[] = "C:/";
char str2[] = "ABC/bbb/testing";
result = rename(str1+str2+counter+".txt","C:/newfolder/newfile.txt")
)
}
 
You can't combine char* strings with the + operator.
If you used STL strings instead of char arrays you could do it. Something like this should work better:
Code:
#include <string>
#include <cstdio>

using namespace std;

int main()
{
   int result;
   string str1( "C:/" );
   string str2( "ABC/bbb/testing" );
   string strNew;

   for ( int counter = 101; counter <= 450; ++counter )
   {
      strNew = str1 + str2 + counter + ".txt";
      result = rename( strNew.c_str(), "C:/newfolder/newfile.txt" );
   }

   return 0;
}
 
Thanks for the suggestion but it still isnt working.This is what I am doing..Exactly..

#include <string>
#include <iostream>
#include <cstudio>

using namespace std;

int main()
{
//Program Purpose
//Purpose is to rename 101.pdf and so on into something else

int result;
int counter = 101;
string str1(c:/MILLOND1205HRT/");
string str2(".pdf");
string str3("modified.pdf");
string strNew;
string strNew2;

for (counter=101;counter<=105;++counter)
{
strnew = str1+counter+str2; //here is where its bombing

strNew2 = str1+counter+str3;

result = rename(strNew.c_str(),strNew2.c_str());
}

return 0;

}
 
Oh, I forgot you were trying to add an integer into the string... In your case a stringstream might be better:
Code:
#include <sstream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
   int result = 0;
   string str1( "c:/MILLOND1205HRT/" );
   string str2( ".pdf" );
   string str3( "modified.pdf" );
   stringstream strNew;
   stringstream strNew2;

   for ( int counter = 101; counter <= 105; ++counter )
   {
      strNew << str1 << counter << str2;
      strNew2 << str1 << counter << str3;
      result = rename( strNew.str().c_str(), strNew2.str().c_str() );

      if ( result != 0 )
      {
         // rename() failed, do something about it here.
      }
   }

   return 0;
}
 
I'll try it thanks. I actually got it to work using sprintf() but this seems a little cleaner. Thanks again.

AM
 


Thanks CPJUST.

I had some problems with clearing the buffer (initialized stringstream variable, because the program added to the variable instead of starting fresh at every turn of the loop but a simple strNew.str("") fixed that and my program works!!
Have a great day!

AM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top