C/C++ String Problem.

This is a discussion on C/C++ String Problem. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a simple program to act as a middle man for Borland's command line compiler. This way, ...

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    C/C++ String Problem.

    I'm trying to write a simple program to act as a middle man for Borland's command line compiler. This way, I save the time of typing out many different options. But I keep encountering a problem at runtime and the program closes.

    My problem, I think, is with a C style string, but since most of my code is C++, I decided to post it here. However, I did try using a C++ style string, but I got the same error at runtime. I managed to pinpoint the line on which the error occurs, each time, but as you know, this doesn't necessarily mean anything.

    Code:
    #include <stdio.h>
    
    #include "WindowProcedures.h"
    
    int main(int argc, char* argv[])
     {
      string Borland = "bcc32";
      string SetBorlandDirectory = "cd C:\\Borland\\Bcc55\\Bin";
      string source;
      char* csource = reinterpret_cast<char*>(malloc(MAX_PATH));
      int csourcesize;
      string directory;
      string resetdirectory;
      char cshortexecutable[MAX_PATH];
      for(int index = 0; index < MAX_PATH; ++index)
        cshortexecutable[index] = 0;
      string shortexecutable;
      string executable;
      string arguments;
    
      if(argc == 1)
       {
        system("C:\\Borland\\Bcc55\\Bin\\bcc32");
        return EXIT_SUCCESS;
       }
    
      source = argv[1];
      csource = strcpy(csource, argv[1]);
      for(int index = 0; 1; ++index)
       {
        if(csource[index] != 0)
          ++csourcesize;
        else
          break;
       }
      for(int index = csourcesize; index >= 0; --index)
       {
        if(csource[index] == '\\') // ERROR OCCURS HERE...
         {
          for(; index < csourcesize; ++index)
            csource[index] = 0;
          break;
         }
       }
    
      directory = csource;
      resetdirectory = "cd \"" + directory + "\""; 
    
      csource = strcpy(csource, argv[1]);
      for(int index = csourcesize + 1; index >= 0; --index)
       {
        if(csource[index] == '\\')
         {
          for(int index2 = 0; index < csourcesize; ++index2)
           {
            ++index;
            if(csource[index] != '.')
              cshortexecutable[index2] = csource[index];
            else
             {
              cshortexecutable[index2] = '.';
              cshortexecutable[++index2] = 'e';
              cshortexecutable[++index2] = 'x';
              cshortexecutable[++index2] = 'e';
              break;
             }
           }
          break;
         }
       }
      shortexecutable = cshortexecutable;
      executable = directory + shortexecutable;
    
      arguments = Borland + " -I\"" + directory + "\" -e\"" + executable + "\" \"" + source + "\"";
      cout << SetBorlandDirectory << endl;
      cout << arguments << endl;
      cout << resetdirectory << endl;
    /*  system(SetBorlandDirectory.c_str());
      system(arguments.c_str());
      system(resetdirectory.c_str());*/
    
      free(csource);
      return EXIT_SUCCESS;
     }
    Code:
    void function(void)
     {
      function();
     }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You never initialized csourcesize to 0.

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    .......that I didn't.....good Lord.....it's always something stupid. Thanks a lot, man.
    Code:
    void function(void)
     {
      function();
     }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You could have just used strlen. Even better, you could just use C++ strings for everything. They have a length() method, and copying is as simple as a single assignment.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    (C mixed with C++) casting malloc != good: faq
    shouldn't even have to use malloc() in c++
    Code:
     char* csource = reinterpret_cast<char*>(malloc(MAX_PATH));


    this:
    Code:
     else
             {
              cshortexecutable[index2] = '.';
              cshortexecutable[++index2] = 'e';
              cshortexecutable[++index2] = 'x';
              cshortexecutable[++index2] = 'e';
              break;
             }

    can probably be written like this:
    Code:
    cshortexecutable += ".exe";    // overloaded += operator for the <string> class simulates the strcat( ) cstring function


    Ok.. down to what I think is your error:

    Here you have csource as a char pointer
    Code:
    char* csource = reinterpret_cast<char*>(malloc(MAX_PATH));
    Here.. you are assigning your char pointer to an int.. ?
    Code:
    for(int index = csourcesize;

    maybe this would work better:
    Code:
    for(int index = strlen(csourcesize);      //from the <cstring> library
    Last edited by The Brain; 07-09-2005 at 01:57 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    system() requires <stdlib.h>. So does malloc() (at least it does if you don't want warnings about not having a cast).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Here.. you are assigning your char pointer to an int.. ?
    Code:
    for(int index = csourcesize;

    maybe this would work better:
    Code:
    for(int index = strlen(csourcesize);      //from the <cstring> library
    csourcesize isn't a char*. csource is, but csourcesize isn't. It's an int.

    And I am including stdlib.h. My will not compile without a reinterpret_cast. I read that article on casting malloc a long time ago, but in C++, you must use a cast to convert void pointers.
    Code:
    void function(void)
     {
      function();
     }

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Yes but in C++ we use new and delete instead of malloc, calloc, and free.
    So this:
    Code:
    char* csource = reinterpret_cast<char*>(malloc(MAX_PATH));
    Should be:
    Code:
    char* csource = new char[MAX_PATH];
    And to clean up
    Code:
    delete [] csource;
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21