Append a number to set of characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aeo3
    New Member
    • Jul 2007
    • 29

    Append a number to set of characters

    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
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by aeo3
    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

    Comment

    • aeo3
      New Member
      • Jul 2007
      • 29

      #3
      Originally posted by Savage
      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

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by aeo3
        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

        Comment

        • aeo3
          New Member
          • Jul 2007
          • 29

          #5
          Originally posted by Savage
          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

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by aeo3
            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

            Comment

            • aeo3
              New Member
              • Jul 2007
              • 29

              #7
              Originally posted by Savage
              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
              [code=cpp]
              string int2string(cons t int& number)
              {
              ostringstream oss;
              oss << number;
              return oss.str();
              }



              string FILENAME;

              for(int i=0;i<numEco;i+ +)
              {
              FILENAME="inf_h _";
              FILENAME+=int2s tring(h);
              FILENAME.append ("_f_");
              FILENAME+=int2s tring(f);
              FILENAME.append ("_");
              FILENAME+=int2s tring(i);
              FILENAME.append (".txt");
              // strcpy(str,FILE NAME);
              EcoArray[i]->Run_Economy( );
              }
              [/code]
              and here is an attempt but by using char

              [code=cpp]
              int h=5;
              char filename[30]="inf_"
              filename=filena me+h;[/code]
              Last edited by sicarie; Sep 13 '07, 01:13 PM. Reason: Code tags

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                aeo3-

                Please use code tags around your code, they are &#91;code=cp p] and [/code]

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by aeo3
                  This is the new trial that i am trying to use string but there is many problems so I thought to use char
                  [code=cpp]
                  string int2string(cons t int& number)
                  {
                  ostringstream oss;
                  oss << number;
                  return oss.str();
                  }



                  string FILENAME;

                  for(int i=0;i<numEco;i+ +)
                  {
                  FILENAME="inf_h _";
                  FILENAME+=int2s tring(h);
                  FILENAME.append ("_f_");
                  FILENAME+=int2s tring(f);
                  FILENAME.append ("_");
                  FILENAME+=int2s tring(i);
                  FILENAME.append (".txt");
                  // strcpy(str,FILE NAME);
                  EcoArray[i]->Run_Economy( );
                  }
                  [/code]
                  and here is an attempt but by using char

                  [code=cpp]
                  int h=5;
                  char filename[30]="inf_"
                  filename=filena me+h;[/code]

                  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

                  Comment

                  Working...