Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Error concatinating file path and file name....

Error concatinating file path and file name....

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys... I am actually recording a wave file. I have hardcoded the file path but the file name is provided by the user. Now I want to wave file to be stored at this combination of these two which in im successful. Here is what im trying

    LPCSTR filePath = "d:\\Audios\\";
    LPCSTR fileNamePath = strcat(filePath, strcat(fileName, ".wav")); //NOTE: fileName is the argument to this function

    This is not successful, how can I do that?? thnx

    C L L _ 5 Replies Last reply
    0
    • A AmbiguousName

      hello guys... I am actually recording a wave file. I have hardcoded the file path but the file name is provided by the user. Now I want to wave file to be stored at this combination of these two which in im successful. Here is what im trying

      LPCSTR filePath = "d:\\Audios\\";
      LPCSTR fileNamePath = strcat(filePath, strcat(fileName, ".wav")); //NOTE: fileName is the argument to this function

      This is not successful, how can I do that?? thnx

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      overloaded Name wrote:

      This is not successful

      Please be specific when asking a question. What doesn't work exactly ? In your case, I would guess it is because you are using constant strings (LPCSTR, the C is for constant). Try using LPSTR instead, it should work. Don't forget that strcat will modify the destination string (check the documentation). BTW, it is far more easier to use std::string instead of using raw char pointers.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • A AmbiguousName

        hello guys... I am actually recording a wave file. I have hardcoded the file path but the file name is provided by the user. Now I want to wave file to be stored at this combination of these two which in im successful. Here is what im trying

        LPCSTR filePath = "d:\\Audios\\";
        LPCSTR fileNamePath = strcat(filePath, strcat(fileName, ".wav")); //NOTE: fileName is the argument to this function

        This is not successful, how can I do that?? thnx

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        strcat needs a writeable destination buffer. where is all this text going to be stored? you need a buffer large enough to hold it, I see it nowhere. Here is one way of doing it (C code):

        char* result=malloc(1000);
        strcpy(result, filePath);
        strcat(result, fileName);
        strcat(result, ".wav");

        This is far from optimal, as the size is overestimated, and the string operations are unsafe, they could overrun the destination buffer. It would also fail if filePath doesn't end on a backslash, or fileName already has an extension. But it would work. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        1 Reply Last reply
        0
        • A AmbiguousName

          hello guys... I am actually recording a wave file. I have hardcoded the file path but the file name is provided by the user. Now I want to wave file to be stored at this combination of these two which in im successful. Here is what im trying

          LPCSTR filePath = "d:\\Audios\\";
          LPCSTR fileNamePath = strcat(filePath, strcat(fileName, ".wav")); //NOTE: fileName is the argument to this function

          This is not successful, how can I do that?? thnx

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You are using strcat() incorrectly so this code wll not work. However, you would also be better using the more secure versions[^] of these functions.

          I must get a clever new signature for 2011.

          1 Reply Last reply
          0
          • A AmbiguousName

            hello guys... I am actually recording a wave file. I have hardcoded the file path but the file name is provided by the user. Now I want to wave file to be stored at this combination of these two which in im successful. Here is what im trying

            LPCSTR filePath = "d:\\Audios\\";
            LPCSTR fileNamePath = strcat(filePath, strcat(fileName, ".wav")); //NOTE: fileName is the argument to this function

            This is not successful, how can I do that?? thnx

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You could use the PathCombine[^] and PathAddExtension[^] functions, they solve the problems mentioned by Luc Pattyn. e.g.

            PathCombine(fileNamePath,filePath,fileName);
            PathAddExtension(FileNamePath,".wav");

            Note the remark, make fileNamePath able to hold at least MAX_PATH characters.

            X 1 Reply Last reply
            0
            • A AmbiguousName

              hello guys... I am actually recording a wave file. I have hardcoded the file path but the file name is provided by the user. Now I want to wave file to be stored at this combination of these two which in im successful. Here is what im trying

              LPCSTR filePath = "d:\\Audios\\";
              LPCSTR fileNamePath = strcat(filePath, strcat(fileName, ".wav")); //NOTE: fileName is the argument to this function

              This is not successful, how can I do that?? thnx

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              Here is the C++ way to do this -

              std::stringstream strm;
              strm << filepath << filename << ".wav";

              std::string str(strm.str());

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++)

              Polymorphism in C

              1 Reply Last reply
              0
              • L Lost User

                You could use the PathCombine[^] and PathAddExtension[^] functions, they solve the problems mentioned by Luc Pattyn. e.g.

                PathCombine(fileNamePath,filePath,fileName);
                PathAddExtension(FileNamePath,".wav");

                Note the remark, make fileNamePath able to hold at least MAX_PATH characters.

                X Offline
                X Offline
                XTAL256
                wrote on last edited by
                #7

                Pretty handy functions, i did not know they existed. To OP, if you do use them you will need to #include and link with Shlwapi.

                [Window Detective] - Windows UI spy utility

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups