system() not working - c

I am trying to launch executables from a C source file. When there is a space in the path I.e.
system("D:\\Games\\Subway Surfers\\Subway_Surfers.exe")
it does not work but
when I change the folder name and remove the space it works. Is there a way around this?

You have to use escape characters while using spaces in path.
Ex: system("D:\\Games\\Subway\ Surfers\\Subway_Surfers.exe");

Try replacing the \ with \\ and with \. You have to replace the characters with their respective escape characters.
system("\"D:\\Games\\Subway\ Surfers\\Subway_Surfers.exe\"");
This command would be interpreted as:
"D:\Games\Subway Surgers\Subway_Surfers.exe"
And, the quotes around the path with spaces ensure that the string is not truncated about the space.

Thanks guys escape characters didn't work so I just used CreateProcess() function. Its long but works fine even with spaces

You need to quote subdir name containing space character. For example like
system("D:\\Games\\\"Subway Surfers\"\\Subway_Surfers.exe") where \"Subway Surfers\" is quoted subdir with spaces.

I have found a perfect workaround to use the system() function. it requires a string in the argument so i just create a string whose contents are the path e.g char path[50] = "D:\SubwaySurfers\SubwaySurfers.exe" then call the function as
system(path);
however in some specific applications such as Apache(Game) it doesnt work whether i use CreateProcess or System.

Related

Single Quotes or No quotes in file paths in Unix shells

I am new to Unix systems and trying to learn some thing with help of terminal. I have following question in my mind. If we can write filepath without single quotes in terminal (for ex : mv path1 path2) then why we sometime use single quotes to specify paths. What is the difference between these two?
This is not a question of the operating system, but of the shell you use. You can actually chose what shell you want to use on a unixoid system if multiple are installed (which usually is the case).
In general the shell has to interpret the input you make. It has to decide how to handle the tokens of the input. What to consider as the "command" you want to execute, what as arguments. For the arguments it has to decide if the string is meant as a single argument or multiple arguments.
Without quotes (single or double quotes), whitespace characters are considered separators between words, words are typically considered separate arguments. So you can specify multiple arguments for a single command. If that is not desired then you can use quote characters to group multiple words separated by whitespace characters into a single argument, for example a folder name containing a space character. This works because now the shell knows that you want everything following the quote character to be considered as a single argument up to the next matching quote character (actually except escaped ones...).
It's used to escape spaces in file names, otherwise, a backslash is needed. For instance:
$ rm spaces\ in\ file\ name
$ rm 'spaces in file name'
If your file path does not have spaces, it's probably safe to omit the quotes.

Putting spaces in file directory string

I am new to file I/O, and I am writing a program in C to read a file that I already created. The examples in the book I have do not use literals with spaces. I was wondering if:
#define kErrorLog "/Dropbox/Dev/Learn%20C%20on%20Mac/Error%20Log"
would give me the appropriate path that corresponds to user/dropbox/dev/Learn C on Mac/Error Log.
No, you should just use spaces:
#define kErrorLog "/Dropbox/Dev/Learn C on Mac/Error Log"
The %20 escape is interpreted by web servers. Filenames are just character strings.
No; the file name need not be URL-encoded like that. You can include spaces normally:
#define kErrorLog "/Dropbox/Dev/Learn C on Mac/Error Log"
In general, there is no need to escape file names in C. If you're putting a file name directly in your code, you might need to escape problematic characters inside a string literal (for example, backslashes), but once you have it in a string, no modifications need to be made to that string to use it as a file name.

^M^Mtypedef enum - Stray characters in Header file

I am using Vim editor and when I open the my file, I get some stray characters as shown below:
^M^Mtypedef enum
This is not giving any compilation problem. But it looks absurd while I look at code.
Before I deliver the code, I want to remove these stray characters. Please suggest a way.
Additional INFO:
When I am using source insight to open the files, the special characters are not displayed, but the the colours of the variables are being shown wrong, e.g. for an enum type variable, generally source insight shows blue, but because of these stray chars, which are shown as a space, the colour is being shown as green. If I remove the extra space (actually an ^M,) the colour is properly displayed.
Get rid of them them with the substitute command in vim:
:%s/^V^M//g
^V^M means: type Control+v - hold down the Control key and type v, then Control+m.
Pls use the follwoing command.
dos2unix *
This will remove those special chars.
^M is a DOS Line Break Character which shows up in Linux, if you download a file from Windows.
Try the below:
:%s/(ctrl+v)(ctrl+m)//g
Replace \r with \n. Try sed. ^M is actually \r
dos2unix as suggested by most, did not work for me. Finally i did a simple thing. Copied the whle code, pasted inside a text file. Copied it from there once agin and replaced it again in my source code. This did work..!!

usage of string tokenizer

i have a string like this..
/home/Abcd/Pradeep/Jack.sh
/home/Abcd/Pradeep/Paul/Kill.sh
I need to take Jack.sh and Kill.sh alone from these strings. there can be many / in the string.
How to do this using strtok ?
You don't need strtok for this. Just use strrchr to find the last '/' character. Your filename starts one character after that.
From the path name style, it looks like it is a *nix system.
You can use the command basename which does the same thing.
If you want to use it in a c program, try man 3 basename in your system to get the documentation.

system() copy fails, while cmd copy works

In cmd.exe, I can execute the command "copy c:\hello.txt c:\hello2.txt" and it worked fine.
But in my C program, I ran this piece of code and got the following error:
#include <iostream>
using namespace std;
int main()
{
system("copy c:\hello.txt c:\hello2.txt");
system("pause");
return 0;
}
Output:
The system cannot find the file specified.
Anybody know what is going on here?
Inside C strings (and quite a few other languages that use the same escaping rules), \ should be \\ since it's the escape character. It allows you to enter, in normal text, non-printable characters such as:
the tab character \t.
the carriage-return character \r.
the newline character \n.
others which I won't cover in detail.
Since \ is used as the escape character, we need a way to put an actual '\' into a string. This is done with the sequence \\.
Your line should therefore be:
system("copy c:\\hello.txt c:\\hello2.txt");
This can sometimes lead to obscure errors with commands like:
FILE *fh = fopen ("c:\text.dat", "w");
where the \t is actually the tab character and the file that you're trying to open is:
c:TABext.dat.
Alternatively, all the Windows functions support Unix style slashes
system("copy c:/hello.txt c:/hello2.txt");
Some people prefer this since it's easier to spot an odd '\'.
But it might confuse Windows users if you display this path in a message.

Resources