Thread: writing a c code in VC++ for windows

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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,815
    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 ...
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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, 08:48 PM
  3. Writing to a Windows console
    By ItchyBob in forum Windows Programming
    Replies: 1
    Last Post: 02-13-2008, 03:35 PM
  4. Writing Screensavers for Windows in C++
    By identifier-less in forum C++ Programming
    Replies: 9
    Last Post: 03-31-2006, 02:22 PM
  5. writing a windows service
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-01-2004, 06:32 AM