Putting spaces in file directory string - c

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.

Related

Issue with file names and content that includes foreign characters

I'm trying to write a program that fetches the name and contents of a text file (UTF-8).
I use
system("chcp 65001 > nul");
to be able to properly read foreign characters in the text and copy them to a string one character at a time. The characters get properly copied to the string.
I also want to get the name of the file which includes Turkish characters such as İ, Ö, Ç, Ü etc.
For this I use strcpy with dir->d_name
The issue is when I print them to the terminal text contents are fine but file name is missing afromentioned foreign characters.
"TRKE.txt" instead of "TÜRKÇE.txt"
I must be able to accurately compare the file contents with file name using strcmp but it's not possible since the name is missing characters.
Using
system("chcp 1254 > nul");
instead makes it possible to get the file name correctly but then the contents of the file are not represented correctly. Alternating between the two lines doesn't work since I need both to be working at the same time to use strcmp().
setlocale(LC_ALL, "Turkish");
doesn't fix it either. What do I do?

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.

system() not working

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.

differences in size of two

i am having a directory which contains 4 files namely 1.c,2.c,3.c and 4.c.i am reading the file names present under this directory by using readdir system call which returns to some structure variable namely myStruct.
2)I am having another open file namely a.txt file which contains file names like 1.c,2.c,3.c,4.c etc...
My intention is to compare the files present in a.txt with the files presen in the directory(just the name comparison is enough..not checking its contents).
when i do the comparison,even though the names present in the directory matches with those present in the a.txt file,they dont show equal comparison and then when i printed the lenghths they are unequal.
Can anyone please let me know any solution to this problem
thanks
maddy
When you read from the file, there is an extra null character at the end of the line you have read, so the comparison will show that they are unequal. So after reading the line, trim off the \n and then try.
EDIT
This discussion tells you about how to trim whitespaces in a string using C - Painless way to trim leading/trailing whitespace in C?

What are reserved filenames for various platforms?

I'm not asking about general syntactic rules for file names. I mean gotchas that jump out of nowhere and bite you. For example, trying to name a file "COM<n>" on Windows?
From: http://www.grouplogic.com/knowledge/index.cfm/fuseaction/view_Info/docID/111.
The following characters are invalid as file or folder names on Windows using NTFS: / ? < > \ : * | " and any character you can type with the Ctrl key.
In addition to the above illegal characters the caret ^ is also not permitted under Windows Operating Systems using the FAT file system.
Under Windows using the FAT file system file and folder names may be up to 255 characters long.
Under Windows using the NTFS file system file and folder names may be up to 256 characters long.
Under Window the length of a full path under both systems is 260 characters.
In addition to these characters, the following conventions are also illegal:
Placing a space at the end of the name
Placing a period at the end of the name
The following file names are also reserved under Windows:
aux,
com1,
com2,
...
com9,
lpt1,
lpt2,
...
lpt9,
con,
nul,
prn
Full description of legal and illegal filenames on Windows: http://msdn.microsoft.com/en-us/library/aa365247.aspx
A tricky Unix gotcha when you don't know:
Files which start with - or -- are legal but a pain in the butt to work with, as many command line tools think you are providing options to them.
Many of those tools have a special marker "--" to signal the end of the options:
gzip -9vf -- -mydashedfilename
As others have said, device names like COM1 are not possible as filenames under Windows because they are reserved devices.
However, there is an escape method to create and access files with these reserved names, for example, this command will redirect the output of the ver command into a file called COM1:
ver > "\\?\C:\Users\username\COM1"
Now you will have a file called COM1 that 99% of programs won't be able to open, and will probably freeze if you try to access.
Here's the Microsoft article that explains how this "file namespace" works. Basically it tells Windows not to do any string processing on the text and to pass it straight through to the filesystem. This trick can also be used to work with paths longer than 260 characters.
The boost::filesystem Portability Guide has a lot of good info.
Well, for MSDOS/Windows, NUL, PRN, LPT<n> and CON. They even cause problems if used with an extension: "NUL.TXT"
Unless you're touching special directories, the only illegal names on Linux are '.' and '..'. Any other name is possible, although accessing some of them from the shell requires using escape sequences.
EDIT: As Vinko Vrsalovic said, files starting with '-' and '--' are a pain from the shell, since those character sequences are interpreted by the application, not the shell.

Resources