Define a string

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

    Define a string

    I need to put a filename is mixed from characters and numbers but these numbers are variable, for example;
    char filename[]="xxx" then var "xxx" then var;
    i.e., filename="ind23 ept12 " where 23 is var x and 12 is var y.


    int x;
    int y;
    char filename[20];
    cin>>x>>y;
    char *filename;
    filename=strcpy (filename,"inf_ h_");
    filename=itoa(f ilename,x);
    filename=strcpy (filename,"_f_" );
    filename=itoa(f ilename,y);
    myfile.open("fi lename");
  • Matthew Page
    New Member
    • Jul 2007
    • 36

    #2
    I don't know if it plays well in the C++ world, but for C you can use the sprintf function. The format would be the same as the printf function except that the first argument is the string you want to write to.

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Sprintf should work in C++. Just include the c header file.

      The C++ solution is to use something called an ostringstream. They are found in the <sstream> header file. You use them like cout and then extract the string info with str(). For example:

      ostringstream oss;
      oss << "path" << 11 << "/file" << 22.22;
      cout << oss.str()

      will print out "path11/file22.22"

      Comment

      • aeo3
        New Member
        • Jul 2007
        • 29

        #4
        When I am trying to use ostringstream oss;
        undecalare ostringstream

        Comment

        • Meetee
          Recognized Expert Contributor
          • Dec 2006
          • 928

          #5
          Originally posted by aeo3
          When I am trying to use ostringstream oss;
          undecalare ostringstream
          For that you need to add headers

          #include <iostream>
          #include <sstream>

          Try std::ostringstr eam instead of ostringstream.

          Regards

          Comment

          • aeo3
            New Member
            • Jul 2007
            • 29

            #6
            Thanks it works, but i need to put this string in another variable, for instance
            char filename[25];
            filename=oss.st r();
            but the error message appears which is incompatible types in assigment of 'st::basic_stri ng<char,.....

            Comment

            • Meetee
              Recognized Expert Contributor
              • Dec 2006
              • 928

              #7
              Originally posted by aeo3
              but the error message appears which is incompatible types in assigment of 'st::basic_stri ng<char,.....
              Read above lines again. You might have written st:: instead of std::

              That may be the cause of error. Please check it.

              Regards

              Comment

              • aeo3
                New Member
                • Jul 2007
                • 29

                #8
                char filename[25];
                std::ostringstr eam oss;
                oss<<"inf_h_"<< x<<"_f_"<<y<<". txt";
                filename=oss.st r();
                this is a part of my code,
                the error message is : incompatible types in assigment of 'std::basic_str ing(char,std::c har_traits<char >, std::allocator< char>>' to char[25]
                also, i tried to use pointer to char, but error messages appears.

                Comment

                • Darryl
                  New Member
                  • May 2007
                  • 86

                  #9
                  [CODE=cpp]char filename[25]
                  filename=oss.st r();
                  [/CODE]

                  you can't assign char arrays this way, only in construction like

                  char filename[25] = oss.str().c_str (); //plus you needed c_str()

                  but since you are using stringstreams anyway, easier would be

                  oss >> filename;

                  Comment

                  • RRick
                    Recognized Expert Contributor
                    • Feb 2007
                    • 463

                    #10
                    aeo3,

                    Watch how you mix these structures together. You're mixing C with C++. You can do it, but you need to be careful. One solution is to stay with C++ and use strings, and the various streams like cout and ostringstream.

                    If you want to use char * or char buffer[23], then you're mixing C and C++ together. You can it, but its not for the novice. For example:
                    Code:
                    const char * buffPtr = oss.str().c_str();
                    will work, but you can't modify the buffPtr values.

                    If you want to put the values in char buffer[23], then you will have to use the C string copy routines. For example:
                    Code:
                    char buffer[23];
                    strncpy( buffer, 23, oss.str().c_str());
                    but now buffer might not be terminated with a '\0' and you'll probably have to cast the oss c_str to (char *).

                    When I deal with strings, I keep them in the C++ string class and only use c_str(), when I have to pass them off to some routine.

                    Comment

                    • sicarie
                      Recognized Expert Specialist
                      • Nov 2006
                      • 4677

                      #11
                      aeo3-

                      Check your PM's in the top right of the page.

                      Thanks,

                      sicarie

                      Comment

                      • aeo3
                        New Member
                        • Jul 2007
                        • 29

                        #12
                        I think it is good idea, thanks a lot.
                        The only way that I found to use ostringstream is by adding std::ostringstr eam also, when I'm trying to define a string variable;
                        string var;
                        it is not accepted as a variable type
                        So, array of char is the alternative way to deal with it. But what is c.str()?.
                        The idea that i need to do, my program will generate a series of output files, and i need to distinguh between these files according to the value of two integer variables.

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          aeo3-

                          This is your last chance to respond to my PMs.

                          Comment

                          • aeo3
                            New Member
                            • Jul 2007
                            • 29

                            #14
                            to sicarie

                            what is the problem, you send me a message, i do not know the reasons to tell me something like that why? what happen?

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #15
                              aeo3-

                              Go to the top right corner of the page - you will see a number followed by "PMs". Click on it, read them, and respond.

                              Comment

                              Working...