what about this new way of opening file

M

Madhur

Hello
what about this nice way to open a file in single line rather than using
if and else.

#include<stdio.h>
void main()
{
FILE *nd;
clrscr();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
file")&&exit(1);
}

if the file exists condition to right of OR will not be executed and if it
doesnt exists it returns 0 and cause the second
condition to be executed.
printf always returns nonzero on success.


--
Winners dont do different things, they do things differently.

Madhur Ahuja
India
email : madhur<underscore>ahuja<at>yahoo<dot>com
 
A

Arthur J. O'Dwyer

Hello
what about this nice way to open a file in single line rather than using
if and else.

What about it? I'll point out the obvious errors, but I'm not
sure what it is on which you really want comments.
#include<stdio.h>
void main()

int main(void)
{
FILE *nd;
clrscr();

No such function -- luckily, this line doesn't seem to do
anything critical anyway, and can safely be removed entirely.
fopen("c:\\autoexec.bat","r")

(nd = fopen("c:\\autoexec.bat", "r")
&&printf("success")

&& puts("success")!=EOF
|| printf("error opeing file")

|| puts("error opening file")!=EOF
&&exit(1);

&& exit(EXIT_FAILURE);

fclose(nd);
}

if the file exists condition to right of OR will not be executed and
if it doesnt exists it returns 0 and cause the second condition to
be executed.

Correct.
printf always returns nonzero on success.

Not correct, but harmless in this case. Consider the
trivial

printf("");

-Arthur
 
R

Robert Stankowic

Madhur said:
Hello
what about this nice way to open a file in single line rather than using
if and else.

#include<stdio.h>
void main()
{
FILE *nd;
clrscr();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
file")&&exit(1);
}

if the file exists condition to right of OR will not be executed and if it
doesnt exists it returns 0 and cause the second
condition to be executed.
printf always returns nonzero on success.

Well, as soon as all the UB is fixed and you find a good way to attempt
recovery in a sensible way...
However, even then I'd beat the creator if I had to maintain such code..

No offense intended :)
Robert
 
M

Martin Ambuhl

Madhur said:
Hello
what about this nice way to open a file in single line rather than using
if and else.

#include<stdio.h>
(including said:
void main()
^^^^
illiterate stupidity
{
FILE *nd;
clrscr();
^^^^^^^^
non-standard garbage
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think you mean (nd = fopen("c:/autoexec.bat","r"). Open without the
assignment is as close to useless as you can get. [Spelling flame for
"opeing" supressed.]
file")&&exit(1);
^^^^^^^
Non-portable argument to exit() function.

Returning a value from a function that returns a value (as does main(), if
the code is written in C) is always a good idea, although not required to
be explicit in C99. I'll bet you don't have a C99 compiler, though.
}

if the file exists condition to right of OR will not be executed and if it
doesnt exists it returns 0 and cause the second
condition to be executed.
printf always returns nonzero on success.

Learn to write C before trying stupid compiler tricks.

Losers write broken code like yours.
 
C

CBFalconer

Madhur wrote:
.... snip ...
#include<stdio.h>
void main()
*** ^^ ERROR illegal declaration of main
{
FILE *nd;
clrscr();
*** ^^^^ ERROR undeclared non-standard function.
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
file")&&exit(1);
*** ^^^ ERROR undeclared standard function
*** ^^ ERROR undefined argument to exit.
}

if the file exists condition to right of OR will not be executed
and if it doesnt exists it returns 0 and cause the second
condition to be executed.
printf always returns nonzero on success.

*** ERROR. The following defines the return value of printf.

[#14] The fprintf function returns the number of characters
transmitted, or a negative value if an output or encoding
error occurred.

Note: zero is a valid number of characters to transmit.
 
N

nrk

Martin said:
Madhur said:
Hello
what about this nice way to open a file in single line rather than
using
if and else.

#include<stdio.h>
(including said:
void main()
^^^^
illiterate stupidity
{
FILE *nd;
clrscr();
^^^^^^^^
non-standard garbage
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think you mean (nd = fopen("c:/autoexec.bat","r"). Open without the
assignment is as close to useless as you can get. [Spelling flame for
"opeing" supressed.]
file")&&exit(1);
^^^^^^^
Non-portable argument to exit() function.

Returning a value from a function that returns a value (as does main(), if
the code is written in C) is always a good idea, although not required to
be explicit in C99. I'll bet you don't have a C99 compiler, though.
}

if the file exists condition to right of OR will not be executed and if
it doesnt exists it returns 0 and cause the second
condition to be executed.
printf always returns nonzero on success.

Learn to write C before trying stupid compiler tricks.

Losers write broken code like yours.

Sorry, we've temporarily run out of the milk of human kindness. Please
check back later. :)

-nrk.
 
J

Joona I Palaste

What about it? I'll point out the obvious errors, but I'm not
sure what it is on which you really want comments.

No such function -- luckily, this line doesn't seem to do
anything critical anyway, and can safely be removed entirely.

I think many newbies simply think that all programs *must* start by
clearing the screen, whatever they do. No matter whether they're
calculating a sum or a GCD, or implementing an e-mail client - clearing
the screen is essential for doing anything at all. It's starting to
annoy me.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
 
A

Arthur J. O'Dwyer

fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I think you mean (nd = fopen("c:/autoexec.bat","r"). Open without the
assignment is as close to useless as you can get. [Spelling flame for
"opeing" supressed.]
^^^^^^^^^

I know it's an unwritten rule that all spelling flames must contain
at least one spelling error, but is it proper for explicit *non-spelling*
flames to contain spelling errors? ;)

-Arthur
 
B

Bruno Desthuilliers

Madhur said:
Hello
what about this nice way to open a file in single line rather than using
if and else.

Err... Nice ?
#include<stdio.h>
void main()
{
FILE *nd;
clrscr();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
file")&&exit(1);
}


What does your compiler think of it ? Here's what mine complains about :

[laotseu@localhost fclc]$ gcc -Wall -ansi -pedantic -ostupid stupid.c
stupid.c:3: warning: return type of `main' is not `int'
stupid.c: In function `main':
stupid.c:5: warning: implicit declaration of function `clrscr'
stupid.c:6:61: warning: multi-line string literals are deprecated
stupid.c:6: warning: implicit declaration of function `exit'
stupid.c:6: void value not ignored as it ought to be
stupid.c:4: warning: unused variable `nd'

Before trying to do things differently, you'd better start with trying
to do them correctly...

Bruno
 
M

Mark McIntyre

I think many newbies simply think that all programs *must* start by
clearing the screen, whatever they do. No matter whether they're
calculating a sum or a GCD, or implementing an e-mail client - clearing
the screen is essential for doing anything at all. It's starting to
annoy me.

Wait 20 years. If its still only /starting/ to annoy you, then you're
a saint. :-(
 
M

Madhur

i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at all.
* OK u all tell me which compiler should i use as i dont know much abt them
..

* whats wrong in using clrscr, if the code is written for DOS, cant we clear
the DOS screen.

* OK, i know void main should be int main, but i have habits of programming
in this compiler , which doesnt gives errors or warnings on it. what should
i do.

--
Winners dont do different things, they do things differently.

Madhur Ahuja
India
email : madhur<underscore>ahuja<at>yahoo<dot>com

Bruno Desthuilliers said:
Madhur said:
Hello
what about this nice way to open a file in single line rather than using
if and else.

Err... Nice ?
#include<stdio.h>
void main()
{
FILE *nd;
clrscr();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
file")&&exit(1);
}


What does your compiler think of it ? Here's what mine complains about :

[laotseu@localhost fclc]$ gcc -Wall -ansi -pedantic -ostupid stupid.c
stupid.c:3: warning: return type of `main' is not `int'
stupid.c: In function `main':
stupid.c:5: warning: implicit declaration of function `clrscr'
stupid.c:6:61: warning: multi-line string literals are deprecated
stupid.c:6: warning: implicit declaration of function `exit'
stupid.c:6: void value not ignored as it ought to be
stupid.c:4: warning: unused variable `nd'

Before trying to do things differently, you'd better start with trying
to do them correctly...

Bruno
 
A

A. Sinan Unur

i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at all.
* OK u all tell me which compiler should i use as i dont know much abt
them .

Please type properly.

There is DJGPP for DOS (which also works in all versions Windows) and if
you want to do Windows programming as well, you can get Cygwin.
* whats wrong in using clrscr, if the code is written for DOS, cant we
clear the DOS screen.

What gives you the stupid programmer of a stupid program the right to
clear my screen which may have output from another program which I would
like to keep?
* OK, i know void main should be int main,

Then use int main ... save a keystroke.
but i have habits of programming in this compiler , which doesnt gives
errors or warnings on it. what should i do.

Hmmm ... Learn to read, quote, and type properly.

By the way, what you came up with looks like a poor imitation of Perl.
Well, C is not Perl.

Sinan.
 
R

Richard Heathfield

Madhur said:
i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at all.

Fantastic! Here's some code that works fine on my compiler. My compiler gave
me no errors at all! What does Turbo C++ 3.0 think of it?

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
pid_t pid = fork();
if(pid == 0)
{
puts("I'm a child process!");
printf("Child Process ID is %d\n", getpid());
}
else
{
printf("I'm the parent process! My child process is %d\n", pid);
}
return 0;
}
* OK u all tell me which compiler should i use as i dont know much abt
them .

Turbo C++ 3.0 is fine (if invoked as a C compiler), but it shouldn't be your
/only/ compiler if your goal is to learn C itself, rather than "C + Borland
extensions".
* whats wrong in using clrscr, if the code is written for DOS, cant we
clear the DOS screen.

If you are writing code for DOS, you're in the wrong newsgroup. This
newsgroup discusses C, not DOS.
* OK, i know void main should be int main, but i have habits of
programming in this compiler , which doesnt gives errors or warnings on
it. what should i do.

Get at least one other compiler, and write your code so that it compiles
cleanly on both. I would suggest a Linux compiler, or gcc under Cygwin,
since this is the best way for you to learn that the <conio.h> model is not
quite as ubiquitous as you might have thought.
 
R

Richard Heathfield

A. Sinan Unur said:
Please type properly.

Yes, that would be more pleasant to read.
There is DJGPP for DOS (which also works in all versions Windows) and if
you want to do Windows programming as well, you can get Cygwin.

Good advice. But he shouldn't give up using Turbo C++ 3.0, or he will simply
fall into the same trap as before. The more compilers (of different
provenance) that you use, the more lessons you will learn about writing
portable code.
What gives you the stupid programmer of a stupid program the right to
clear my screen which may have output from another program which I would
like to keep?

This is over the top. The guy is clearly not stupid, merely ignorant.
Ignorance can be cured. The program is pretty stupid, yes; but if you use
that program whilst you have irreplaceable data on the screen, what does
that say about you?

Any programmer must decide whether a given program's need for full-screen
addressability outweighs the desirability of a keeping the program portable
and non-irritating.
Then use int main ... save a keystroke.

No, don't use int main to save a keystroke; use int main because it's the
Right Thing. For a start, using int main /doesn't/ save a keystroke, as it
obliges you to add a return statement that the incorrect void main does not
require.

<snip>
 
D

donLouis

i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at all.
* OK u all tell me which compiler should i use as i dont know much abt >them

tcc is fine, but you should use the ansi flag. i believe -a should help.
also, if there are any extra warning or error flags, use those as well.
start with reading the output of tcc -?.
* whats wrong in using clrscr, if the code is written for DOS, cant we >clear
the DOS screen.

clearing the screen isn't the problem per se, but when you post code,
you should stick with standard c.
* OK, i know void main should be int main, but i have habits of >programming
in this compiler , which doesn't gives errors or warnings on it. what >should
i do.

read the documentation for tcc on how to configure it for ansi/iso
conformance.

you may be interested in this function, courtesy of kernighan & pike:

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

FILE *efopen(char *file, char *mode) {
FILE *nd;
if ((nd = fopen(file, mode)) != NULL)
return nd;
printf("error opening file\n");
exit(EXIT_FAILURE); }

i took out some of the fancier stuff, but you might like the usage:

fp = efopen("c:\\autoexec.bat", "r");

my personal favorite, anyway.
 
M

Mark McIntyre

i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at all.

You probably have warnings turned off. Read the compiler manual, and
turn warnings up to the maximum level possible. Ideally set it to
consider warnings as errors. Then get rid of all the warnings.
* OK u all tell me which compiler should i use as i dont know much abt them

TC3 will do fine.
Also, please don't use txtspk in CLC, people prefer proper english ie
"you" not u, about not abt etc.
* whats wrong in using clrscr, if the code is written for DOS, cant we clear
the DOS screen.

Many people consider it rude, and its always unnecessary. Why do you
need to get rid of whats there before? Maybe the user wanted to see
it. Maybe he needs it for something.
* OK, i know void main should be int main, but i have habits of programming
in this compiler , which doesnt gives errors or warnings on it.


Thats a very very bad excuse. "OK, I know I should not drink and play
with guns, but I never had an accident yet, so..."
what should i do.

use the correct idiom,
 
C

CBFalconer

Madhur said:
i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at
all.
* OK u all tell me which compiler should i use as i dont know
much abt them

* whats wrong in using clrscr, if the code is written for DOS,
cant we clear the DOS screen.

* OK, i know void main should be int main, but i have habits
of programming in this compiler , which doesnt gives errors
or warnings on it. what should i do.

Stop the rude and evil top-posting. Answers belong after (or
intermixed with) the material to which you are replying, with
non-germane portions snipped out. Stop using the silly "u"
abbreviation, the lower case "i", and other abbreviations, which
serve no purpose other than to make you look ignorant and make
your posting hard to read. Capitalize the beginning of sentences.

Don't use anything (in this newsgroup) that is not a part of ISO
standard C. Abandon your evil habits at the door before entering
here. Adjust the preferences/options whatever on the TC compiler
for ANSI C, and the maximal warning level. You are perfectly free
to write DOS only code, but keep it to yourself, or post it to a
newsgroup with msdos in it's name.
 
J

Joona I Palaste

Madhur said:
* whats wrong in using clrscr, if the code is written for DOS, cant we clear
the DOS screen.

Because clearing the screen has *NOTHING WHATSOEVER* to do with opening
files! There are about a million other, practical purposes for C code
that don't require clearing the screen either!
Contrary to what you might think, C constructs such as functions,
variables, and loops work *just as well* even if you *don't* clear the
screen. Your instructor might have told you to always clear the screen,
but he might not have explained why. (Does he even know himself?) =)
 
B

Bruno Desthuilliers

Madhur wrote:
(please don't top-post - corrected)
Madhur said:
Hello
what about this nice way to open a file in single line rather than
using
if and else.

Err... Nice ?

#include<stdio.h>
void main()
{
FILE *nd;
clrscr();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error
opeing
file")&&exit(1);
}


What does your compiler think of it ? Here's what mine complains about :

[laotseu@localhost fclc]$ gcc -Wall -ansi -pedantic -ostupid stupid.c
stupid.c:3: warning: return type of `main' is not `int'
stupid.c: In function `main':
stupid.c:5: warning: implicit declaration of function `clrscr'
stupid.c:6:61: warning: multi-line string literals are deprecated
stupid.c:6: warning: implicit declaration of function `exit'
stupid.c:6: void value not ignored as it ought to be
stupid.c:4: warning: unused variable `nd'

Before trying to do things differently, you'd better start with trying
to do them correctly...
> i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at all.

Because you didn't ask your compiler to warn you about what's wrong.
You'll find how to do it with TurboC++ if you RTFM.
> * OK u all tell me which compiler should i use as i dont know much
abt > them

It's a FAQ. RTFF !-)
>
> * whats wrong in using clrscr, if the code is written for DOS, cant we clear
> the DOS screen.

The first thing that is wrong is that clrscr() is not defined by the ISO
standard.

The second thing that is wrong is that stdout may be connected to
anything (a printer, a modem, a file, a socket, etc..., and even,
*eventually*, a screen). What would 'clear screen' means for a file, a
socket or printer ?

The third thing that is wrong is that, even if stdout is connected to a
screen, the user may not want to have it's screen cleared.

If your program is intended to have a 'text-mode GUI' (like the TurboC
IDE or like), that's ok, but that's not standard C (and as such is OT
here).
> * OK, i know void main should be int main, but i have habits of
> programming

Bad habits. Change habits.
> in this compiler , which doesnt gives errors or warnings on it. what
> should i do.

RTFM and learn how to turn warnings on with your compiler ?-)

HTH
Bruno
 
M

Madhur

Hello
Allright! I have learnt my mistakes. I will be downloading a new compiler
and avoid the use of shortcuts in my posts.
Many thanks to you All.

--
Winners dont do different things, they do things differently.

Madhur Ahuja
India
email : madhur<underscore>ahuja<at>yahoo<dot>com

Bruno Desthuilliers said:
Madhur wrote:
(please don't top-post - corrected)
Madhur wrote:

Hello
what about this nice way to open a file in single line rather than
using

if and else.

Err... Nice ?


#include<stdio.h>
void main()
{
FILE *nd;
clrscr();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error
opeing

file")&&exit(1);
}


What does your compiler think of it ? Here's what mine complains about :

[laotseu@localhost fclc]$ gcc -Wall -ansi -pedantic -ostupid stupid.c
stupid.c:3: warning: return type of `main' is not `int'
stupid.c: In function `main':
stupid.c:5: warning: implicit declaration of function `clrscr'
stupid.c:6:61: warning: multi-line string literals are deprecated
stupid.c:6: warning: implicit declaration of function `exit'
stupid.c:6: void value not ignored as it ought to be
stupid.c:4: warning: unused variable `nd'

Before trying to do things differently, you'd better start with trying
to do them correctly...
i used Turbo C++ 3.0 compiler for DOS. It gave me no errors at all.

Because you didn't ask your compiler to warn you about what's wrong.
You'll find how to do it with TurboC++ if you RTFM.
* OK u all tell me which compiler should i use as i dont know much
abt > them

It's a FAQ. RTFF !-)
* whats wrong in using clrscr, if the code is written for DOS, cant we clear
the DOS screen.

The first thing that is wrong is that clrscr() is not defined by the ISO
standard.

The second thing that is wrong is that stdout may be connected to
anything (a printer, a modem, a file, a socket, etc..., and even,
*eventually*, a screen). What would 'clear screen' means for a file, a
socket or printer ?

The third thing that is wrong is that, even if stdout is connected to a
screen, the user may not want to have it's screen cleared.

If your program is intended to have a 'text-mode GUI' (like the TurboC
IDE or like), that's ok, but that's not standard C (and as such is OT
here).
* OK, i know void main should be int main, but i have habits of
programming

Bad habits. Change habits.
in this compiler , which doesnt gives errors or warnings on it. what
should i do.

RTFM and learn how to turn warnings on with your compiler ?-)

HTH
Bruno
 

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,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top