I have my answer, thanks to all who responded. I forgot that
I need to use "\\" to get a \ in there.
Bill
Remember that the backslash charachter (\), besides being a directory separator
in MS-DOS, is a special character in C strings - it gives special meaning to
the following character. To get an actual '\' in a string, you need _two_ of
them, like '\\', so the above statement should be:
fp = fopen("C:\\results\\data.txt", "w");
You can instead, use a single forward slash '/' - it's only the command line
interpreter that insists on backslashes.
Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
Internet: ben...@triumf.ca | of one another only when one can be
Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
> Problem is that when I try to open a file by specifying a full path
> name, e.g.
>
> fp = fopen("C:\results\data.txt","w");
>
> fopen returns a 0 (indicating failure).
Remember, the backslash is the escape character in string literals. You
should use instead:
fp = fopen("C:\\results\\data.txt","w");
Actually, only the DOS command shell requires the use of backslashes. DOS
itself will accept forward slashes as well, so you can also use:
fp = fopen("C:/results/data.txt","w");
-------------------------------------------------------------
MicroFirm: Down to the C in chips...
FidoNet 1:106/2000.6
Internet bobs...@neosoft.com
Home of SNIPPETS - Current release:
oak.oakland.edu:/SimTel/msdos/c/snip9503.zip
juge.com:/c/file/c/snip9503.lzh
Anyway, I'm using a Borland Turbo C++ compiler but with the
source option set to ANSI C, so I'm effectively writing ANSI
C code. It's running under Windows 3.1. I want to open a file
using fopen. If I say something like
fp = fopen("data.txt","w");
it works fine, creating the file data.txt in the directory
C:\tcwin\bin
Problem is that when I try to open a file by specifying a full path
name, e.g.
fp = fopen("C:\results\data.txt","w");
fopen returns a 0 (indicating failure). If I make the call as
fp = fopen("C:data.txt","w");
the file data.txt is created in the same directory as before,
C:\tcwin\bin. I should mention that the C source file itself also resides
in this directory, as does any other source, object or executable
I create under Windows.
I would like to be able to open a file in another directory,
or even on another disk. Is there any way to do this?
Thank you very much.
Bill
you have to use double \\'s... like this: fp =
fopen("C:\\results\\data.txt","w"); otherwise, the compiler recognizes the
\ as the start of an escape sequence i.e \n, \a, \t etc...
You need to use:
fp = fopen("C:/results/data.txt","w");
-or-
fp = fopen("C:\\results\\data.txt","w");
The fact that PC's have their slashes the wrong way does not
change their meaning in literal strings.
This post would fare better in comp.lang.c or
comp.msdos.programmer; this group is for questions about the
standard.
Shaun
You posted to comp.std.c so I will give the standard answer but also have
a question of my own. (As well, if you wonder how to use your vendor's
C implementation to specify a file name, you should look in your vendor's
manuals.)
In C, a string can contain characters other than double quote and backslash
and newline, and can contain escape sequences. One example of an escape
sequence is \r which represents a carriage return. (Another example is
\" which represents a double quote, and there are many others.) Now, the
disk file probably doesn't have a carriage return in the middle of its name,
so that is one likely reason for fopen not to find the file.
Now, your string also contains \d in the middle. This is not an escape
sequence according to the syntax of the C language. In the standard, a
"Description" clause says that if any other escape sequence is encountered
the behavior is undefined. However, if I understand correctly, an answer
to a defect report once answered that if a diagnostic is required due to
violation of a "Syntax" or "Constraints" clause then the diagnostic cannot
be omitted even if another clause says behavior is undefined. So I think
that every conforming implementation must issue a diagnostic for this
program. Do other experts agree with me?
--
<< If this were the company's opinion, I would not be allowed to post it. >>
"I paid money for this car, I pay taxes for vehicle registration and a driver's
license, so I can drive in any lane I want, and no innocent victim gets to call
the cops just 'cause the lane's not goin' the same direction as me" - J Spammer
In article <3stbao$g...@usenet.pa.dec.com>, dia...@jrd.dec.com (Norman
Diamond) writes:
<snip>
|> Now, your string also contains \d in the middle. This is not an escape
|> sequence according to the syntax of the C language. In the standard, a
|> "Description" clause says that if any other escape sequence is encountered
Precisely: how can you encounter any `escape sequence' other than the `escape
sequences' defined by the standard? (Notice that the language `are
representable by escape sequence consisting of \ followed by a lower
case letter' doesn't seem to be defining escape sequences, for it clearly
talks about \? etc. as escape sequences two paras before: it is
talking about those escape sequences which have that form.) However, 6.9.2
does make the intent of the standard clear: it would be nice if one did not
have to talk about intents of non-sentient objects in the first place :-)
|> the behavior is undefined. However, if I understand correctly, an answer
|> to a defect report once answered that if a diagnostic is required due to
|> violation of a "Syntax" or "Constraints" clause then the diagnostic cannot
|> be omitted even if another clause says behavior is undefined. So I think
|> that every conforming implementation must issue a diagnostic for this
|> program. Do other experts agree with me?
I guess the standard just shouldn't do things like this: on one hand say it
puts absolutely no requirement on the implementation, and on the other hand
it insists that the no requirement is only after the requirement of
diagnostic is satisfied. What would have been wrong if \d were only a syntax
violation? What would have been wrong if these were implementation defined?
or plain undefined? (As to your question posed to other experts, I will let
other experts reply: but what I have read here supports your conclusion.)
Cheers
Tanmoy
--
tan...@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tan...@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
Have a look in the manual to find out what \r and \d mean :)
That is, try something like:
fp = fopen("C:\\results\\data.txt","w");
Claude.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vv vv David Claude Brown
||----|| * br...@falam.ch
|| | /
/\------/
(oo)
(~~)
Because, '\' is the escape character, in your string the preprocessor
will scan the string literal and replace your \results with <CR>esults
and your \data with <implementation defined>ata.
You're call to fopen with a filename of
"C:<CR>esultsdata" fails because a file of
that name doesn't exist.
>
>Bill
John.
--
_______________________________________________________________________________
John Roy | Cambridge Technology Partners | Phone: (617) 374-8375
jr...@ctp.com | 304 Vassar Street | Fax: (617) 374-8300
| Cambridge, MA 02139 |
fp = fopen("C:\\results\\data.txt","w");
^^
||
Use \\ for each directory slash, the first one is a special character
that allows the second character to be used in a literal string.
Look at printf formats with your help utility it should explain
things like \n \r \f \\ \t etc.
Mike
Someone probably already answered your question, but I'll answer it just
in case. :)
When using the fopen() function and specifying a path name you need to
use '/' instead of '\'. Your function call should then look like:
fp = fopen("C:/results/data.txt","w");
This way the fopen command will be protable to most systems, including a PC.
See 'Ya
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
This message has been brought to
you by:
####################################
# Lawrence M. O'Leary #
# (aka: Burns) #
# Internet Mail: #
# ole...@freenet.calgary.ab.ca #
# (198.161.243.11) #
# Thank You!!! #
####################################
>Hi, I have a question about using fopen. I'm using a 486,
>and if there's a better place to post this I would appreciate
>any pointers, and I apologize if this is a bad place to post.
possibly comp.os.msdos.something but your problem is kinda a result
of C anyway... although if DOS didn't use stooopid backslash.. :)
>fp = fopen("C:\results\data.txt","w");
In C string literals, \ is used to initiate an escape code (or whatever
they're called.. eg \n). use
fp = fopen("C:\\results\\data.txt","w");
HTH
Russell
--
Russell Edwards -- voo...@yoyo.cc.monash.edu.au -- IRC: VoodChile
Student of Computer Science and Electrical and Computer Systems Engineering
-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-\|/-
Woohoo!! I mean 'DOH!'
> Lawrence M. O'Leary (Burns) (ole...@freenet.calgary.ab.ca) wrote:
> >On 28 Jun 1995, Bill Moore wrote:
> >> fp = fopen("C:\results\data.txt","w");
> > fp = fopen("C:/results/data.txt","w");
>
> But do be aware that this won't work with the system() call on DOS
> systems since while DOS will deal with forward slashes, COMMAND.COM
> barfs on it. In other words, "system("DIR C:/results/data.txt");
> will fail to display the file.
This is very true... remember that DOS uses both the / and \ while DOS's
command interpreter (COMMAND.COM) will only use \. There for only I/O
function calls can use / or \ but any interpreter function calls (such as
system()) can only use \.
It can get quite confusing when you are tyring to write portable code.
But do be aware that this won't work with the system() call on DOS
systems since while DOS will deal with forward slashes, COMMAND.COM
barfs on it. In other words, "system("DIR C:/results/data.txt");
will fail to display the file.
--
D'Arcy J.M. Cain (darcy@{planix|druid}.com) | Democracy is three wolves
Planix, Inc., Toronto, Canada | and a sheep voting on
+1 416 424 2871 (DoD#0082) (eNTP) | what's for dinner.
-- Info on Planix can be found at http://www.planix.com --
But, even under DOS, system() might not invoke COMMAND.COM. It is entirely
possible that the user has decided that COMMAND.COM is an inadequate shell and
is using another command processor.
Bottom-line: system() can call a command processor in an implementation-defined
manner. Many implementations give the user control of the command processor
invoked by system(), so it is hard to rely upon any particular behavior.
-ccwf
>It can get quite confusing when you are tyring to write portable code.
But, just about every possible use for system() is not portable anyway.
There is no guarantee external commands are going to be available:
For example the call:
system("ls -l");
on any system may or may not work, depending on if 'ls' exists, is on the
path, and if it's indeed a executable binary/script. Note that it depends on
configuration, not on platform. Even on DOS, we can create an executable
called 'ls.{exe|com|bat}' and the program will work. For unix, we can just
erase/rename the binary or remove the directory 'ls' is in from the path,
and the program will fail.
: > Lawrence M. O'Leary (Burns) (ole...@freenet.calgary.ab.ca) wrote:
-discusion on / and \ with system
: This is very true... remember that DOS uses both the / and \ while DOS's
: command interpreter (COMMAND.COM) will only use \. There for only I/O
: function calls can use / or \ but any interpreter function calls (such as
: system()) can only use \.
: It can get quite confusing when you are tyring to write portable code.
: See 'Ya
:
: -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
-overlong signature deleted-
If you are truely writing portable code you will only use system() to execute
commands provided by the user, and it is up to the user to decide what the
expected results are. The ANSI C standard does not require system() to use any
specific command interpretor do to anything particually useful. So your
portable code can't rely on it.
[However it is confusing if you are trying to be a truly transportable
programer.]
--
Stephen Baynes bay...@mulsoc2.serigate.philips.nl
Philips Semiconductors Ltd
Southampton My views are my own.
United Kingdom
On 28 Jun 1995, Bill Moore wrote:
> fp = fopen("C:\results\data.txt","w");
>
> fopen returns a 0 (indicating failure). If I make the call as
>
fp = fopen("C:\\results\\data.txt","w");
Seth D. Osher SE/I Department Computer Analysis _/_/ _/_/ _/_/
Internet: sos...@plexus.wsoc.com _/_/ _/_/ _/
Tel. (703) 883-8573 Fax. (703) 883-8788 _/ _/ _/ _/_/
PRC Inc., Mail Stop TM 431, 1505 PRC Drive, McLean, VA 22102