Like Tree1Likes
  • 1 Post By grumpy

writing a c code in VC++ for windows

This is a discussion on writing a c code in VC++ for windows within the Windows Programming forums, part of the Platform Specific Boards category; This is a very simple question, but it is just hard for me to figure out. I try to move ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    26

    writing a c code in VC++ for windows

    This is a very simple question, but it is just hard for me to figure out. I try to move a file from my folder to a program files subfolder, but I think the cmd in windows take the space between program and files as a delimiter. Anyone?

    system("xcopy password.ini C:\Program Files\OpenSLP /i /y");

    Sometimes, it returns a message, "Invalid number of parameters". Sometimes, it is "parse error". Please help!

    Sharon

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Because \ is a magic character, and you need to be careful how you use it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Do you have any good suggestion of using \?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    Quote Originally Posted by sharonch View Post
    Do you have any good suggestion of using \?
    1. Not to use it; try forward slash instead "/"
    2. Double it; as in "\\" for each time you want a single "\"

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Hmmm, I tried, it didn't work. I still think the problem is in the "space" instead of "\".
    system("xcopy password.ini C:\Program Files\OpenSLP /i /y");
    Because it treats the C:\Program Files as C:\Program. It doesn't know what to do with the rest "Files\OpenSLP /i /y" It is also too many parameter for xcopy.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Any path (in windows) that includes a space needs to be wrapped in double quotes. And double quotes, in C string literals, need to be4 escaped with backslashes. This is in addition to the backslashing concern.

    So (with backslashes escaped)
    Code:
    system("xcopy password.ini \"C:\\Program Files\\OpenSLP\" /i /y");
    or (with backslashes replaced with forward slashes
    Code:
    system("xcopy password.ini \"C:/Program Files/OpenSLP\" /i /y");
    If you are happy for your code to be windows specific (which it already is, since xcopy is windows specific) better to use WIN32 API function calls rather than a system() call. I'll leave that hint there ...
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Yes! It is working. Thanks a lot. I want to check if I am right: the WinAPI that correspond to system() is WinExec(). But it didn't work in my program. I include <Windows.h> and <WinBase.h>, but not helpful. This also warns me that I don't understand windows c programming. I don't know how or where to find the appropriate library to use in my MSVC++2008 to write c program.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    I'm not suggesting using WinExec(). I'm suggesting using functions for traversing directories (FindFirst(), FindNext(), etc) and copying of files (CopyFile(), etc).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing writing code to reading code
    By binks in forum C Programming
    Replies: 8
    Last Post: 06-12-2012, 09:41 AM
  2. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 07:48 PM
  3. Writing to a Windows console
    By ItchyBob in forum Windows Programming
    Replies: 1
    Last Post: 02-13-2008, 02:35 PM
  4. Writing Screensavers for Windows in C++
    By identifier-less in forum C++ Programming
    Replies: 9
    Last Post: 03-31-2006, 01:22 PM
  5. writing a windows service
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-01-2004, 06:32 AM

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