Phil Carmody said:
I just inserted the text 'c:/a.txt' into a MS Paint image, but
MS Paint didn't open the file. Therefore we have another example
of where '/' fails! Maybe I should have used a heavier font?
Oh no, how silly of me - MS Paint isn't the operating system,
MS Paint is just a user space program.
[snip]
I think there's been some confusion of terminology in this thread.
It's quite possible that I've unintentionally contributed to it.
I'll try to summarize the facts of the matter, without necessarily
trying to analyze who made what mistake when.
The \ character can be used as a directory delimiter in Windows.
For example,
C:\foo.txt
is a valid file name, where C is the name of the drive, \ separates
the drive name from the file name (so C:\ denotes the root directory
of the C drive), and foo.txt is the name of the file (in a form
that's useful only if you know where it is).
The use of the \ character can sometimes cause confusion in
C programs. The naively obvious way to write the full name of
the file in C as a string literal is "C:\foo.txt" -- but that's
incorrect, because \ has a special meaning in C string literals.
The way to specify the name as C:\foo.txt is to double the backslash
in the string literal: "C:\\foo.txt".
It's often pointed out here that the operating system will also
accept the / character as a synonym for the \ character, so you
can avoid doubling the \ by writing "C:/foo.txt" rather than
"C:\\foo.txt".
As I understand it, this advice is valid if the string is only going
to be used as a file name to be passed to fopen(). There are other
contexts in a Windows environment, and in a C program compiled for a
Windows environment, where "C:/foo.txt" is *not* equivalent to
"C:\\foo.txt".
For example, if the name is going to be displayed to a user, the
replacement will cause the displayed text to be different, and is
likely to be confusing to Windows users who don't happen to be aware
that the OS allows /.
Another example is when the file name is incorporated into a string
passed to the system() function. system() invokes the command
processor; on Windows, this is usually cmd.exe. The / character is
treated specially by cmd.exe.
So, in my opinion, the advice to use / rather than \ as the directory
delimiter in C programs for Windows is valid *only* if the strings
containing the delimiter will be used only as arguments to fopen
(an assumption that can easily become incorrect as the program is
maintained). The better solution (again, this is just my opinion)
is to use the \ character consistently, and to deal with the fact
that \ is special in C string literals by doubling it when necessary.
There are some other issues that have come up in this discussion that,
in my opinion, are not relevant either to this discussion or to this
newsgroup:
Is cmd.exe part of the operating system?
Is cmd.exe part of Windows?
Are "Windows" and the operating system the same thing, or does the
term "Windows" refer to the OS plus other things?
These might all be interesting questions, but I don't think any of
them are relevant to the issue of a C program running in a Windows
environment. A more relevant question is whether cmd.exe is part of
the C implementation. If the implementation's system() function
invokes cmd.exe, then I would argue that yes, cmd.exe is part of the C
implementation (or at the very least that the C implementation depends
on it).
Joe Wright's question was:
Please elucidate. In what cases will the use of '/' fail?
I believe I answered that question. If the question were meant to be
limited to fopen, Joe should have said so.
Now let's see if the 'type' program opens the file 'c:/a.txt'.
It doesn't. So finally we have an actual example of '/' failing.
But of course, it's only a valid example because 'type' is part
of the operating system and MS Paint isn't.
That's apparently that's your stance. Strangely you seem so proud
of it that you're prepared to repeat it.
But back to your question - why is the distinction important -
because the only thing that matters when it comes to the results
of an fopen is the interface defined by the OS, and not the
interfaces and behaviour of external programs such as the shell,
type, and MS Paint.
Yes, it's what matters when it comes to the result of an fopen.
I wasn't aware that the discussion had been restricted to just the
fopen function.
This is such a black and white issue, I'm perturbed you can't see
the distinction. Maybe too much DOS or Symbian has rotted your brain,
or something.
[snip]
Maybe we could have a civilized discussion without personal insults.