File Handling (Newbie)

K

Kelly B

#include<stdio.h>
#include<stdlib.h>

int main(void)
{

FILE *fp;
char ch;

fp=fopen("C:\abc.txt","r");
if(fp==NULL)
exit(0);

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
printf("%c",ch);
}
fclose(fp);
return 0;
}

fopen() always returns NULL although the file abc.txt is present
can anyone please suggest me why am i not able to open the file..i
believe i am missing out on something?

(I am using Visual Studio 2005 on Windows)
 
M

Martin Ambuhl

Kelly B wrote:
[...]
fp=fopen("C:\abc.txt","r");
fopen() always returns NULL although the file abc.txt is present
can anyone please suggest me why am i not able to open the file..i
believe i am missing out on something?

You are trying to open "C:<alarm>bc.txt".
Dyslexic PC-DOS command line syntax notwithstanding, open either
"C:/abc.txt" or (if you insist) "C:\\abc.txt".
And check the FAQ before posting.
 
G

Gregor H.

fp=fopen("C:\abc.txt","r");
While the whole UNIX word used / in pathnames, the software geniuses at MS
decided to use \. This means that you have to modify your path string to
"C:\\abc.txt" (or "C:/abc.txt") befor you can use it in fopen.


G. H.
 
F

Flash Gordon

Kelly B wrote, On 29/03/07 06:50:

FILE *fp;
char ch;

ch=fgetc(fp);
if(ch==EOF)

<snip>

Your next problem, after the one that others have pointed out, is that
you need to define ch as an int rather than a char. Check your
documentation for the definition of what fgetc returns, and the
comp.lang.c FAQ, for more information.
 
M

Mark McIntyre

While the whole UNIX word used / in pathnames, the software geniuses at MS
decided to use \.

actually, they decided to use "/" as the switch-char, which left them
with no choice. I recall that this was because of some feature of its
CP/M heritage.
Interestingly you can actually change the switchar back to "-" if you
want, and then automagically paths separated by / will work.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Joined
Mar 29, 2007
Messages
5
Reaction score
0
Kelly B said:
#include<stdio.h>
#include<stdlib.h>

int main(void)
{

FILE *fp;
char ch;

fp=fopen("C:\abc.txt","r");
if(fp==NULL)
exit(0);

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
printf("%c",ch);
}
fclose(fp);
return 0;
}

fopen() always returns NULL although the file abc.txt is present
can anyone please suggest me why am i not able to open the file..i
believe i am missing out on something?

(I am using Visual Studio 2005 on Windows)

you can try this
#include<stdio.h>
#include<stdlib.h>

main(void)
{

FILE *fp;
char ch;

fp = fopen("\\abc.txt","r");
if(fp == NULL) { printf("File not found\n"); exit(1); }

while( (ch = fgetc(fp)) != EOF)
putchar(ch);

system("pause");
fclose(fp);
}
 
K

Kelly B

Flash said:
Kelly B wrote, On 29/03/07 06:50:





<snip>

Your next problem, after the one that others have pointed out, is that
you need to define ch as an int rather than a char. Check your
documentation for the definition of what fgetc returns, and the
comp.lang.c FAQ, for more information.

Thank you sir.
I did get the warning and i changed the declaration to
int ch; after i noticed that fgetc returned int.
The code is working fine now.
 
F

Flash Gordon

Kelly B wrote, On 30/03/07 16:36:
Flash Gordon wrote:

Thank you sir.

Way hey! I've been knighted!
I did get the warning and i changed the declaration to
int ch; after i noticed that fgetc returned int.
The code is working fine now.

One of many reasons you should always pay attention to warnings :)
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top