CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: strcat problem

  1. #16
    Join Date
    Aug 2004
    Posts
    15

    about String class

    Thanks for the support CJ!

    In responce to the string class / library, I tried to use that, but can't seem to get it to work.

    #include<string>

    void test()
    {

    string str;
    str = "This is a test";

    }


    this returns the following
    error C2065: 'string' : undeclared identifier (among other errors..)

    I also tried #include <string.h> ...so, I gave up on this. But I do know that the string class would solve a lot of my problems!

  2. #17
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58
    Quote Originally Posted by VladimirF
    Well, this is just wrong! The "%.5d" does NOT limit the output - go check your manuals. It is responsibility of the programmer to provide enough room in the destination buffer (that is why printf's are not safe).
    You better read your manual more carefully and concentrate on the precision field
    From MSDN:
    The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see the How Precision Values Affect Type table). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value.
    that is how you limit the output size.
    Quote Originally Posted by VladimirF
    Also, even if this code compiles in C++ compiler, it still has nothing to do with C++. But I do think that it was OK to post the question here.
    Again read the C++ specification, it includes most of C as part of C++ (there are some minor differences), so the code is both valid C & C++ code just like a = b + c; is valid in C, C++, C#, PASCAL, etc.
    Quote Originally Posted by VladimirF
    I am surprised that nobody pointed out an obvious OP's misunderstanding:
    Code:
    char *fp = strcat(filepath,strcat(filesuffix,bunotostring));
    strcat appends (concatenates) second string to the first string, returning pointer to the target (first string). It does NOT allocate any new memory for char* fp! In that example, fp is equal to filepath. And filepath buffer is not capable to accept all other strings at its end.
    Sorry that is not correct, strncat (as well as the other str... functions) return a pointer to the destination (argument 1) for just that reason it can be used where the calls can be chained. fp will end up pointing to filepath which has a reserved size, same thing for filesuffix which also has a reserved size.
    Quote Originally Posted by VladimirF
    Here comes C++, that provides string classes that take care of the memory management (like std::string and MFC's CString), so you no longer need to guess if 64 bytes will be enough for the file path (you know, it won't).
    While 64 is below the maximum possible size file a path it is within the limits presented in the example.

  3. #18
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Quote Originally Posted by hebes_99
    Thanks for the support CJ!

    In responce to the string class / library, I tried to use that, but can't seem to get it to work.

    #include<string>

    void test()
    {

    string str;
    str = "This is a test";

    }


    this returns the following
    error C2065: 'string' : undeclared identifier (among other errors..)

    I also tried #include <string.h> ...so, I gave up on this. But I do know that the string class would solve a lot of my problems!
    string is located in the std namespace. Either use:
    using namespace std;
    to promote the library names to the current namespace
    or
    std::string str;

  4. #19
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    As to everyone else...Take a deep breath, turn to the person to your right, and give them a big hug.

  5. #20
    Join Date
    Aug 2004
    Posts
    15
    Thanks! That helped with the identifier part...BUT...now I get..

    error C2872: 'ifstream' : ambiguous symbol

    in which ifstream worked before I used namespace...



    HOWEVER, std::stream str; worked fine...Thanks !!! I am glad for the help... Hopefully, in a few months, I will have a better handle on all of this!
    Last edited by hebes_99; August 19th, 2004 at 01:26 PM.

  6. #21
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537

  7. #22
    Join Date
    Aug 2004
    Posts
    15

    Red face

    Thanks again....

    wow...much to learn....

  8. #23
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Quote Originally Posted by hebes_99
    Thanks again....

    wow...much to learn....
    Search the forum for "books" or "c++ books" there are many recommendations, there is also a listing of books in the C++ faq forum.

    In short...getting a C++ book will facilitate your learning.

  9. #24
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Well, Mick - I did hug the person to my right. May I respond now?

    Quote Originally Posted by CJ1
    You better read your manual more carefully and concentrate on the precision field
    If you follow your link a little further "(see the How Precision Values Affect Type table)", you would see:
    d, i, u, o, x, X The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision.
    Quote Originally Posted by CJ1
    that is how you limit the output size.
    Do you? Did you try?

    Quote Originally Posted by CJ1
    Sorry that is not correct, strncat (as well as the other str... functions) return a pointer to the destination (argument 1) for just that reason it can be used where the calls can be chained...
    So, what is not correct?

    My point, however, was that OP might believe that he would get a new string with the result of strcat() in it. (Otherwise, why would he need another variable?) That, of course, was a guess. I am ready to bet a quarter I was right, though.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  10. #25
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58
    Right you are on the precision field, my error.

    As for the quarter, I wouldn't want to guess or assume the intentions.

    This point only undelines that C++ is NOT a language for beginners, it is much too easy to get caught by all the subtleties (like if the intention was to get a new string or just a pointer), but you can't beat C++ for its power!

  11. #26
    Join Date
    Apr 1999
    Posts
    27,449
    To hebes 99,

    The problem seems to have started with learning 'C' before learning C++.

    Somehow you stumbled onto strcat(), but this should have been the last function to use. C++ has a string class (as you now know), that should be introduced early, maybe sometimes in the first couple of lessons, of learning C++. All of those strcats, strcpys, etc. are not necessary for general purpose C++ programming.

    Also, in C++, pointers are less used and should be avoided unless you have to use them. I suggest you get a C++ (and only a C++) book such as "Accelerated C++" by Koenig & Moo. Do not read C books or tutorials to learn C++.

    http://www.parashift.com/c++-faq-lit....html#faq-28.2

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 19th, 2004 at 04:51 PM.

  12. #27
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58
    I agree, BUT I still think C++ is NOT a good first language. Also C# is a better introduction to C++ than C, and it would has the added advantage of protecting the beginner from system crashing errors.

    We have moved to C# for everyting that doesn't require C or C++, and when we require C or C++, we give that job to the senior developers only.

    The C# language is simple to learn, the .NET framework on the other hand is a mountian.

  13. #28
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Quote Originally Posted by CJ1
    I agree, BUT I still think C++ is NOT a good first language.
    I think that C++ is the ONLY language for Windows development. That makes it a very good first language (because there are no other languages).

    Quote Originally Posted by CJ1
    We have moved to C# for everything that doesn't require C or C++, and when we require C or C++, we give that job to the senior developers only.
    Do you have any openings?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  14. #29
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    Seems that I missed all the fun yesterday. And missed that hug also...
    Quote Originally Posted by CJ1
    VictorN is wrong is saying there is no specific C++ code as all the code you presented was valid C++ code, perhaps it should have been posted in the C++ (Non Visual C++ Issues) forum.
    Original code posted:
    Quote Originally Posted by hebes_99
    Code:
    int buno = 0;
    char filepath[64] = "C:\\some path here\\";
    char filesuffix[32] ="_flt_test.csv";
    char bunotostring[5] = "123456"; 
    
    // Convert buno number (int) to string
    sprintf(bunotostring, "%d", buno);
    
    char *fp = strcat(filepath,strcat(filesuffix,bunotostring));
    As I stated in my first post in this thread, there is nothing C++ specific, except for declaration char* fp, which in C should be made at the beginning of the block. Hebes said he is new to C++,
    Quote Originally Posted by hebes_99
    First of all, somewhat newbie to C++.
    so I presumed he comes from the C field. And the code was just C, even if as CJ says is valid C++ code (which of course is).

    Quote Originally Posted by CJ1
    I agree, BUT I still think C++ is NOT a good first language.
    I think that the first language to start with is Pascal. It was intended as a teaching language. Then you switch C and C++. This is my personal opinion.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured