question about file i/o in C 
Author Message
 question about file i/o in C


Quote:
>   FILE *fp;
>   fp=fopen("C:\test.txt","r");
> This program will not find the file unless it is run from the same
> directory the file test.txt is in.  Say for example test.txt is in the
> root directory, and i run this program from C:\work
> It will exit and fp is null.

You need to change C:\test.txt to C:\\test.txt. When you use \ in a string
literal (chars inside " "), the first one is an escape character. The second
one is then interpreted as a single backslash.


Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C

Hello,

I am new to programming in C and especially using files.  I've been
reading some books on file i/o in C and I have a problem.

Say for example the program below is run in one of my subdirectories
#include <stdio.h>
main ()
{
  FILE *fp;
  fp=fopen("C:\test.txt","r");
  if (fp == NULL)
       exit(0);
else
putchar(getc(fp));

Quote:
}

This program will not find the file unless it is run from the same
directory the file test.txt is in.  Say for example test.txt is in the
root directory, and i run this program from C:\work
It will exit and fp is null.

can anyone tell me how to get C to actually access the file no matter
where on my hard drive the program is run from?  In other words is it
possible for me to access any files that are not in the same directory
as the program??

Thanks for your, I would prefer an e-mail response.

rr



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C

Quote:
> You need to change C:\test.txt to C:\\test.txt. When you use \ in a
string
> literal (chars inside " "), the first one is an escape character. The
second
> one is then interpreted as a single backslash.

Or you could use c:/test.txt which works in DOS as well (and UNIX, I
believe?)

John



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C

Quote:

>Hello,

>I am new to programming in C and especially using files.  I've been
>reading some books on file i/o in C and I have a problem.

>Say for example the program below is run in one of my subdirectories
>#include <stdio.h>
>main ()
>{
>  FILE *fp;
>  fp=fopen("C:\test.txt","r");
>  if (fp == NULL)
>       exit(0);
>else
>putchar(getc(fp));
>}

>This program will not find the file unless it is run from the same
>directory the file test.txt is in.

In a string literal \ is an escape character and \t represents a tab
character so unless your filename contains a tab character the system won't
find it. To get an actual \ character in a string literal you need to escape
it i.e. use \\. Another possibility on DOS/Windows based systems is to
use / instead since the OS also accepts that as a path separator.

--
-----------------------------------------


-----------------------------------------



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C


Quote:
>   fp=fopen("C:\test.txt","r");

Welcome to MS-DOS! Take out your manual and look up "escape sequences (or
codes)"... you need this:

fp = fopen( "C:\\test.txt", "r" );

        -- AK

--



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C



Quote:
> > You need to change C:\test.txt to C:\\test.txt. When you use \ in a
> string
> > literal (chars inside " "), the first one is an escape character. The
> second
> > one is then interpreted as a single backslash.
> Or you could use c:/test.txt which works in DOS as well (and UNIX, I
> believe?)

In this context, yes, but not always... DOS accepts the forward slash
only when it expects a path and nothing else is acceptable; if passing
something that may or may not represent a path, you must use the standard
DOS backslash.

        -- AK

--



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C



Quote:
> > You need to change C:\test.txt to C:\\test.txt. When you use \ in a
> string
> > literal (chars inside " "), the first one is an escape character. The
> second
> > one is then interpreted as a single backslash.

> Or you could use c:/test.txt which works in DOS as well (and UNIX, I
> believe?)

A quick test with DJGPP (a DOS GCC port) and Borland shows that they
both take / slashes in paths.

I would imagine that quite a few other compilers do,  since there is a lot
of code sharing between platforms, and / is present in some code.
( No, I am not positive, I don't have access to every compiler and platform
in existance :)



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C


Quote:

>> You need to change C:\test.txt to C:\\test.txt. When you use \ in a
>string
>> literal (chars inside " "), the first one is an escape character. The
>second
>> one is then interpreted as a single backslash.

>Or you could use c:/test.txt which works in DOS as well (and UNIX, I
>believe?)

As long as you have a directory within the current directory called "c:".

John
--
John Winters.  Wallingford, Oxon, England.



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C


Quote:



>> > You need to change C:\test.txt to C:\\test.txt. When you use \ in a
>> string
>> > literal (chars inside " "), the first one is an escape character. The
>> second
>> > one is then interpreted as a single backslash.

>> Or you could use c:/test.txt which works in DOS as well (and UNIX, I
>> believe?)

>In this context, yes, but not always... DOS accepts the forward slash
>only when it expects a path and nothing else is acceptable; if passing
>something that may or may not represent a path, you must use the standard
>DOS backslash.

This is a trifle misleading.  COMMAND.COM (the MS-DOS shell) will not
accept '/' as a path separator, and insists on '\'.  The underlying
filing system and system calls are perfectly happy with '/' or '\'.

John

(Follow-ups set.)
--
John Winters.  Wallingford, Oxon, England.



Wed, 05 Jan 2000 03:00:00 GMT  
 question about file i/o in C



Quote:
>A quick test with DJGPP (a DOS GCC port) and Borland shows that they
>both take / slashes in paths.

>I would imagine that quite a few other compilers do,  since there is a lot
>of code sharing between platforms, and / is present in some code.
>( No, I am not positive, I don't have access to every compiler and platform
>in existance :)

The particular compiler is not impotant, DOS system calls are quite happy to
accept either \ or / as a path separator. It is simply COMMAND.COM which
doesn't accept / because it uses it for command switches. The main
implication for a DOS C program is that since the system() function calls
the command interpreter (which is typically COMMAND.COM under DOS) you can't
use / as a path separator in command lines you pass to system(). It is fine
just about everywhere else though.

--
-----------------------------------------


-----------------------------------------



Thu, 06 Jan 2000 03:00:00 GMT  
 question about file i/o in C

Quote:


> >   fp=fopen("C:\test.txt","r");

> Welcome to MS-DOS! Take out your manual and look up "escape sequences (or
> codes)"... you need this:

> fp = fopen( "C:\\test.txt", "r" );

or this:
  fp = fopen( "c:/test.txt","r" );

--
********************************************

********************************************
I've never had a humble opinion in my life.
If you're going to have one,
why bother to be humble about it?
                                Joan Baez



Thu, 06 Jan 2000 03:00:00 GMT  
 question about file i/o in C



Quote:

> >   fp=fopen("C:\test.txt","r");

> Welcome to MS-DOS! Take out your manual and look up "escape sequences (or
> codes)"... you need this:

> fp = fopen( "C:\\test.txt", "r" );

>    -- AK

The DOS compilers I have (DJGPP,BC++) accept:

        fp=fopen("C:/test.txt","r");

as well.



Mon, 10 Jan 2000 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. resx files needed for cs - files ??

3. CS files display in VS6 like CPP files ?

4. Include code in other Cs files

5. Reuse of cs files, namespace, arch advice pls

6. How to Generate .cs file at Runtime

7. Two CS files (using namespaces)

8. My .cs files were deleted!?

9. manipulating server controls in the Codebehind .cs file

10. *.~cs files

11. Custom Class.cs default file

12. Printing .cs files

 

 
Powered by phpBB® Forum Software