Opening a file in C

R

Raj

Following is a code to open a file & print its contents on screen. But
im getting the message "file can't be opened". The thing is, that if i
create a file separately in a particular folder in C drive, only then
my file programs are working, i.e. even after providing the full path
name for the location of the file whose contents i want to read, if
the file is not present in the above specified folder, im still not
able to open the file. This particular file also resides in C drive,
but not in the folder i mentioned above.. This same code works fine
for any file in that folder and that folder only.... So my question
is, how can i read a file that resides in a different folder??

Code:
#include<stdio.h>
int main(void)
{
FILE *f;
char ch;
clrscr();
f=fopen("C:\manni\PLACE4.C","r");
if(f==NULL){
printf("File can't be opened");
getch();
exit(1);
}
while(!feof(f)){
ch=fgetc(f);
printf("%c",ch);
}
fclose(f);
getch();
return 0;
}
 
R

Robert Gamble

Following is a code to open a file & print its contents on screen. But
im getting the message "file can't be opened". The thing is, that if i
create a file separately in a particular folder in C drive, only then
my file programs are working, i.e. even after providing the full path
name for the location of the file whose contents i want to read, if
the file is not present in the above specified folder, im still not
able to open the file. This particular file also resides in C drive,
but not in the folder i mentioned above.. This same code works fine
for any file in that folder and that folder only.... So my question
is, how can i read a file that resides in a different folder??

Code:
#include<stdio.h>
int main(void)
{
FILE *f;
char ch;
clrscr();
f=fopen("C:\manni\PLACE4.C","r");

Backslashes have a special meaning in string literals in C, to specify
a literal backslash prefix it with another backslash:

f=fopen("C:\\manni\\PLACE4.C","r");

Robert Gamble
 
S

santosh

Raj said:
Following is a code to open a file & print its contents on screen. But
im getting the message "file can't be opened". The thing is, that if i
create a file separately in a particular folder in C drive, only then
my file programs are working, i.e. even after providing the full path
name for the location of the file whose contents i want to read, if
the file is not present in the above specified folder, im still not
able to open the file. This particular file also resides in C drive,
but not in the folder i mentioned above.. This same code works fine
for any file in that folder and that folder only.... So my question
is, how can i read a file that resides in a different folder??

Code:
#include<stdio.h>
int main(void)
{
FILE *f;
char ch;
clrscr();

Non-standard function. Remove or reimplement in ISO C for portability.
f=fopen("C:\manni\PLACE4.C","r");

The backslash signals the start of an escape sequence in C. To represent the
backslash itself, follow with another backslash, i.e:

f = fopen("C:\\manni\\PLACE.C", "r");
if(f==NULL){
printf("File can't be opened");

Terminate printf's output with a newline character to ensure that the output
is immediately written to the associated device.

Another non-standard function. Just use the standard getchar for this.

(void)getchar();

One is not a portable return value. Portable values are 0, EXIT_SUCCESS and
EXIT_FAILURE. The macros are defined in stdlib.h.

Also you not included the proper prototype for exit. It is in stdlib.h.
}
while(!feof(f)){

In C, you can test for end-of-file or an I/O error only _after_ an atttempt
to read or write.

Thus you should probably do:

while((ch = getc(f)) != EOF) {
printf("%c", ch);
}
/* Now test for end-of-file or an error. */
ch=fgetc(f);
printf("%c",ch);
}
fclose(f);
getch();

Replace with getchar.
 
R

Raj

Backslashes have a special meaning in string literals in C, to specify
a literal backslash prefix it with another backslash:

f=fopen("C:\\manni\\PLACE4.C","r");

Robert Gamble- Hide quoted text -

- Show quoted text -

Thanx thanx thanx!!!
 
K

Keith Thompson

Raj said:
Following is a code to open a file & print its contents on screen. But
im getting the message "file can't be opened". [...]
f=fopen("C:\manni\PLACE4.C","r");
[...]

The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've just asked
question 19.17.
 
M

Martin Ambuhl

Raj said:
Following is a code to open a file & print its contents on screen. But
im getting the message "file can't be opened". The thing is, that if i
create a file separately in a particular folder in C drive, only then
my file programs are working, i.e. even after providing the full path
name for the location of the file whose contents i want to read, if
the file is not present in the above specified folder, im still not
able to open the file. This particular file also resides in C drive,
but not in the folder i mentioned above.. This same code works fine
for any file in that folder and that folder only.... So my question
is, how can i read a file that resides in a different folder??

Code:
#include<stdio.h>
int main(void)
{
FILE *f;
char ch;
^^^^
This is the wrong type. fgetc() returns an int. When
ch=fgetc(f) returns EOF, the contents of ch, if declared as a char, may
well not equal EOF.
clrscr();

In posting to this newsgroup, attempt to avoid non-standard functions.
"clrscr()" is not a standard function, you have provided no code for it,
and there is no prototype or declaration (it can't be in <stdio.h>,
since it has a name not in the implementation's namespace). There is a
separate argument that, assuming clrscr() has a name suggesting its
function, that its use is often antisocial.
f=fopen("C:\manni\PLACE4.C","r");
^^^^^^^^^^^^^^^^
This call uses the escape characters '\m' and '\P'. The portable escape
characters are '\n', '\t', '\b', '\r'. '\f', '\v'. '\\'. '\''. '\"',
'\a', and '\?'.

If you insist on using the dyslexic MS-DOS form, the above should be
f=fopen("C:\\manni\\PLACE4.C","r");
but even MS-DOS knows how to handle
f=fopen("C:/manni/PLACE4.C","r");
if(f==NULL){
printf("File can't be opened");
getch();
^^^^^^^
Another non-standard function. Again, there is no prototype or
declaration, since it can't have one in <stdio.h>. If you need
something like this, the standard function getchar() probably will do as
well.

You should #include <stdlib.h> if you use exit(). Without it, not only
is there no declaration or prototype of exit, but the only value for its
argument that has a portable meaning is 0. With <stdlib.h> you gain
EXIT_SUCCESS, which means the same as 0, and EXIT_FAILURE. I would
guess that you mean
exit(EXIT_FAILURE);
}
while(!feof(f)){
^^^^^^^^
This is not the way to read characters through the end of file.
See below.
ch=fgetc(f);
printf("%c",ch);
}

This loop could easily be written,
while ((ch = fgetc(f)) != EOF)
putchar(ch);
fclose(f);
getch();
^^^^^
see above
 
C

CBFalconer

Raj said:
Following is a code to open a file & print its contents on screen. But
im getting the message "file can't be opened". The thing is, that if i
create a file separately in a particular folder in C drive, only then
my file programs are working, i.e. even after providing the full path
name for the location of the file whose contents i want to read, if
the file is not present in the above specified folder, im still not
able to open the file. This particular file also resides in C drive,
but not in the folder i mentioned above.. This same code works fine
for any file in that folder and that folder only.... So my question
is, how can i read a file that resides in a different folder??

Code:
#include<stdio.h>
int main(void)
{
FILE *f;
char ch;
clrscr();

No such routine. If it does what it sounds like, you don't want
it.
f=fopen("C:\manni\PLACE4.C","r");

Replace all '\' with '/'. Even if you are using Windoze, it should
work. Consider actually using some blanks in your lines.
if(f==NULL){
printf("File can't be opened");
getch();

No such routine in standard C. What do you want a char for?

Illegal value. And why use exit? Just return EXIT_FAILURE (after
}
while(!feof(f)){

This loop is nonsense. Look up what feof actually does. See
below.
ch=fgetc(f);
printf("%c",ch);
}
fclose(f);
getch();

See above. What do you want an input char. for?
return 0;
}

Suggested one line simple loop:

while (EOF != (ch = getc(f))) putchar(ch);

When all that is working consider changing things to make a filter
that copies. Much more flexible, no direct interaction, and usable
in other things. It would be exercized as: "fcopy <inputfile
 
C

Chris Dollin

Raj said:
Following is a code to open a file & print its contents on screen. But
im getting the message "file can't be opened".
f=fopen("C:\manni\PLACE4.C","r");
if(f==NULL){
printf("File can't be opened");

You've likely seen the other answers, so instead here's a tip.

Messages that say "file can't be opened" drive people bats. They
say, not unreasonably, "/what/ file can't be opened?". Messages
should be as helpful as possible (but no helpfuller). If you'd
done something like:

char* fileName = "C:\manni\PLACE4.C";
f = fopen( fileName, "r" );
if (f == NULL) printf( "File %s can't be opened\n", fileName );

your message would have carried significantly helpful information;
you'd have said "what the /frell/ happend to my filename?" and maybe
suddenly remembered that \ is special.
 
K

Keith Thompson

CBFalconer said:
Raj wrote: [...]

Illegal value. And why use exit? Just return EXIT_FAILURE (after
#including <stdlib.h>). In addition the exit() doesn't exist if
you haven't done that #include.

It's not illegal, just non-portable. Other than that you're correct,
of course.
 
K

Kenneth Brody

Robert said:
Backslashes have a special meaning in string literals in C, to specify
a literal backslash prefix it with another backslash:

f=fopen("C:\\manni\\PLACE4.C","r");

Or simply forget this Windows peculiarity and use the correct slashes
in the first place:

f = fopen("C:/manny/PLACE4.C","r");

Ever since Microsoft invented[1] subdirectories in MS-DOS 2.0, you
have been able to use slashes in pathnames.


[1] :) :) :)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top