how to use fopen()?

T

Taras

I wrote following program:

#include <stdio.h>

main()
{
FILE *file;

file=fopen(d:\readme.txt,"r");
if (file==NULL)
printf("**** it's not working");
else
printf("finaly");
}


My problem is that the only programs' result is : "**** it's not working"
What do I wrong?
 
A

Andreas Kahari

I wrote following program:

#include <stdio.h>

main()

int main(void)
{
FILE *file;

file=fopen(d:\readme.txt,"r");
if (file==NULL)
printf("**** it's not working");
else
printf("finaly");

fclose(file);
return 0;
}


My problem is that the only programs' result is : "**** it's not working"

I doubt it's even that, you have a syntax error on the line

file=fopen(d:\readme.txt,"r");

Try writing it as

file = fopen("d:/readme.txt", "r");

instead, or escape the backslash:

file = fopen("d:\\readme.txt", "r");
 
F

Fao, Sean

Taras said:
I wrote following program:

#include <stdio.h>

main()
{
FILE *file;

file=fopen(d:\readme.txt,"r");
if (file==NULL)
printf("**** it's not working");
else
printf("finaly");
}

Ok, this is the last time that I fix code when I've only had 2 hours of
sleep. Ignore everything I've written up to this point and read what the
other poster wrote.
 
A

August Derleth

11:21p:
Do we *have* to point out the int main(void) return 0 etc things ?

Yes. Any other declaration for main() is nonstandard and is erroneous. For
example, void main() is incorrect despite its popularity among the less
intelligent of the book and tutorial writers.
 
M

Mike Wahler

Do we *have* to point out the int main(void) return 0 etc things ?

No, of course you don't have to.
But those who want to convey correctness will.
I seems that many instructors don't, through
ignorance, apathy, or whatever.


-Mike
 
T

Taras

Andreas Kahari said:
int main(void)


fclose(file);
return 0;


I doubt it's even that, you have a syntax error on the line

file=fopen(d:\readme.txt,"r");

Try writing it as

file = fopen("d:/readme.txt", "r");

instead, or escape the backslash:

file = fopen("d:\\readme.txt", "r");


Thank you , now it at last writes finally!
 
J

Joona I Palaste

Reads, I hope.

No, "writes" is correct. The OP's code:
---------------------------------------------------------------
#include <stdio.h>

main()
{
FILE *file;

file=fopen(d:\readme.txt,"r");
if (file==NULL)
printf("**** it's not working");
else
printf("finaly");
}
 
M

Micah Cowan

Andreas Kahari said:
Andreas Kahari said:
file = fopen("d:/readme.txt", "r");
Thank you , now it at last writes finally!
Reads, I hope.
No, "writes" is correct. The OP's code: [cut]
See the last printf() statement.


No, that is possibly "displays", but I was concentrating more on
the fopen(), with the "r" mode.

You were concentrating wrong. And recall that the C standard has
no notion of "displays". He said what he meant, but he should
have used quotations, and the code's spelling should have been
fixed.

...now it at last writes, "finally"!

-Micah
 
P

Peter Nilsson

....
You were concentrating wrong. And recall that the C standard has
no notion of "displays".

5.2.2 Character display semantics

It does have *some* notion.
 
G

goose

August Derleth said:
Yes. Any other declaration for main() is nonstandard and is erroneous.

unless, of course, its documented by the implementation.
For
example, void main() is incorrect despite its popularity among the less
intelligent of the book and tutorial writers.


goose,
sometimes it is documented ;-)
 
D

Dan Pop

In said:
unless, of course, its documented by the implementation.

And if the implementation in question claims C99 conformance. In C89,
it doesn't make any difference whether the implementation documents other
main intefaces or not.

Dan
 
G

goose

And if the implementation in question claims C99 conformance. In C89,
it doesn't make any difference whether the implementation documents other
main intefaces or not.

I'm not sure i follow. the c99 standard says "or some other
implementation-defined manner" after stating the legal return
values and arguments. does the c89 standard say differently with
regard to this ? I would like to see a quote (or perhaps a link).


tia
goose,
(I dont have the c89 standard, i *do* have c99 and the n869 file)
 
Z

Zygmunt Krynicki

I wrote following program:

#include <stdio.h>

main()
{
FILE *file;

file=fopen(d:\readme.txt,"r");
if (file==NULL)
printf("**** it's not working");
else
printf("finaly");
}

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

int main(void)
{
FILE *file;
file = fopen ("d:/reame.txt", "r");
if (file == NULL)
printf ("f*** it's not working\n");
else {
printf ("finally\n");
fclose (f);
}
return EXIT_SUCCESS;
}

I'd say \n is nice if you wish to have portable output, also EXIT_SUCCESS
is far better than guessing some popular value.

Regards

Zygmunt Krynicki
 
D

Dan Pop

In said:
I'm not sure i follow. the c99 standard says "or some other
implementation-defined manner" after stating the legal return
values and arguments. does the c89 standard say differently with
regard to this ?

Yes. Otherwise, I wouldn't have mentioned C99 conformance.
I would like to see a quote (or perhaps a link).

2.1.2.2 Hosted environment

A hosted environment need not be provided, but shall conform to the
following specifications if present.

"Program startup"

The function called at program startup is named main. The
implementation declares no prototype for this function. It can be
defined with no parameters:

int main(void) { /*...*/ }

or with two parameters (referred to here as argc and argv, though any
names may be used, as they are local to the function in which they are
declared):

int main(int argc, char *argv[]) { /*...*/ }

If they are defined, the parameters to the main function shall obey
the following constraints:

Dan
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top