Thread: constructor not working with strcpy()

  1. #1
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332

    constructor not working with strcpy()

    hello there.

    i'm trying to code a basic file class. but for the moment, it seems that my program is choking on the constructor of the class. it compliles fine, but wheni run it it hangs on just on the str cpy function strcpy(SZ_fpath, PARAM_path)


    Code:
    class C_fsystem
    {
     protected:
               char SZ_fpath [200];
               char SZ_fname [200];
               char SZ_fexten [100];
               char TMP_path[1000];
     public:
               void setPath(char* TMP_buffer){strcpy(SZ_fpath, TMP_buffer);}
               void setName(char* TMP_buffer){strcpy(SZ_fname, TMP_buffer);}
               void setExten(char* TMP_buffer){strcpy(SZ_fexten, TMP_buffer);}
               char* fullName(void)
               {
               strcpy(TMP_path, SZ_fpath);
               strcat(TMP_path, SZ_fname);
               strcat(TMP_path, ".");
               strcat(TMP_path, SZ_fexten);
               return(TMP_path);
               }
               C_fsystem(char PARAM_path[100], char PARAM_name[100], char PARAM_exten[50]);
    };
    
    C_fsystem::C_fsystem(char PARAM_path[100], char PARAM_name[100], char PARAM_exten[50])
    {
    cout<<"worx"<<endl;
    cout<<PARAM_path<<endl;
    cout<<PARAM_name<<endl;
    cout<<PARAM_exten<<endl;
    strcpy (SZ_fpath,PARAM_path);
    strcpy (SZ_fpath,PARAM_name);
    strcpy (SZ_fpath,PARAM_exten);
    }
    
    
    
    //main
    
    int main()
    {
      C_fsystem myfile("D:\\my documents 2\\robin\\cpp sources\\terraserve\\", "main", "cpp");
      cout<<myfile.fullName();
      cout<<"object declared"<<endl;
      myfile.setPath("D:\\my documents 2\\robin\\cpp sources\\terraserve\\");
      cout<<"setpath done"<<endl;
      myfile.setName("file1");
      cout<<"setname done"<<endl;
      myfile.setExten("cpp");
      cout<<myfile.fullName()<<endl;
      cout<<"funtion returned";
      cin.get();
      exit(0);
    }
    why do c++ strings have to be so complicated...

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just took a quick look but the following looks like it needed correction:
    Code:
    C_fsystem::C_fsystem(char PARAM_path[100], char PARAM_name[100], char PARAM_exten[50])
    {
    cout<<"worx"<<endl;
    cout<<PARAM_path<<endl;
    cout<<PARAM_name<<endl;
    cout<<PARAM_exten<<endl;
    strcpy (SZ_fpath,PARAM_path);
    strcpy (SZ_fname,PARAM_name);
    strcpy (SZ_fexten,PARAM_exten);
    }
    Why not just use actual string containers instead of character arrays? It would be much easier!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    doh! this is the second time ive been looking for problkemsin the worng place. shouldn't that work theoreticly, assigning differn't values to the string consecutivly (or is my computer auto detecting it isn't the least bit usefull )?

    as for string containers and character arrays, you lost me there i'm a PHP programmer most of the time, and i really don't understand much about all there char arrays, memory adresses, and all that. i've read through the tutorials and all but i still don't get it with stings. you got "*" and "[] " all over the place

    thank you for solving my problem!

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The string container hk_mp5kpdw is referring to is the standard string class that comes in <string>. The benefits of it are that you don't have to understand "*" and "[]" all over the place. Here is your same code using the standard string class instead of character arrays (C style strings). Notice that you don't have to worry about a length, the string class can handle any length (within reason). Also, you can just use "=" to assign a value more naturally. You don't have to use strcpy.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class C_fsystem
    {
     protected:
               string SZ_fpath;
               string SZ_fname;
               string SZ_fexten;
     public:
               void setPath(const string& TMP_buffer){ SZ_fpath = TMP_buffer;}
               void setName(const string& TMP_buffer){ SZ_fname = TMP_buffer;}
               void setExten(const string& TMP_buffer){ SZ_fexten = TMP_buffer;}
               string fullName(void)
               {
                   string TMP_path = SZ_fpath;
                   TMP_path += SZ_fname;
                   TMP_path += ".";
                   TMP_path += SZ_fexten;
                   return(TMP_path);
               }
               C_fsystem(const string& PARAM_path, const string& PARAM_name, const string& PARAM_exten);
    };
    
    C_fsystem::C_fsystem(const string& PARAM_path, const string& PARAM_name, const string& PARAM_exten)
    {
    cout<<"worx"<<endl;
    cout<<PARAM_path<<endl;
    cout<<PARAM_name<<endl;
    cout<<PARAM_exten<<endl;
    SZ_fpath = PARAM_path;
    SZ_fname = PARAM_name;
    SZ_fexten = PARAM_exten;
    }
    
    
    
    //main
    
    int main()
    {
      C_fsystem myfile("D:\\my documents 2\\robin\\cpp sources\\terraserve\\", "main", "cpp");
      cout<<myfile.fullName();
      cout<<"object declared"<<endl;
      myfile.setPath("D:\\my documents 2\\robin\\cpp sources\\terraserve\\");
      cout<<"setpath done"<<endl;
      myfile.setName("file1");
      cout<<"setname done"<<endl;
      myfile.setExten("cpp");
      cout<<myfile.fullName()<<endl;
      cout<<"funtion returned";
      cin.get();
      exit(0);
    }

  5. #5
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    oooooh. praise the lord! im liking that alot thanks.one thing i don't understand is when passing parameters

    const string& TMP_buffer

    why do you have to put const in frot of it and pass its reference and not its value?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you don't have to put the const there. the const is another safety device to allow your compiler help to manage your code. in this case it means that the setPath, setName, and setExt functions will not change the value of the parameter passed in by reference. If any attempt is made by the code in the body of the function to change the parameters passed in, the compiler will through an error message. Sometimes you pass the parameters by reference so you can change the value of the parameter back in the calling function, in which case you don't use const. Sometimes you pass parameters by reference because it is easier/faster and you don't want to change the value of the parameter back in the calling function, so you use const.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In addition to what elad said, you could just pass a string like you would an int or float. That would work fine, but just like an int or a float, a copy of that string would be passed into the function. This is done so that you don't accidentally modify the original value. The problem is that a copy of a string is an expensive operation compared to just a copy of a reference to that string.

    In general, when you want to pass an object into a function, and it is not a plain old datatype (POD) like int, bool, char, float, long, double, etc., then you should get in the habit of passing a constant reference. It is just more efficient and in the future might have some beneficial performance effect on your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM
  5. strcpy() function
    By Narciss in forum C Programming
    Replies: 4
    Last Post: 09-21-2001, 12:52 AM