473,466 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what about this new way of opening file

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

Nov 14 '05 #1
28 3229

"Madhur" <a_******@vsnl.net> schrieb im Newsbeitrag
news:bs************@ID-81175.news.uni-berlin.de...
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
Nov 14 '05 #2

On Sat, 20 Dec 2003, Madhur wrote:

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

Nov 14 '05 #3
Madhur wrote:
Hello
what about this nice way to open a file in single line rather than using
if and else.

#include<stdio.h> (including <stdlib.h> would have been nice)
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.
--
Winners dont do different things, they do things differently.


Losers write broken code like yours.


--
Martin Ambuhl

Nov 14 '05 #4
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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
nrk
Martin Ambuhl wrote:
Madhur wrote:
Hello
what about this nice way to open a file in single line rather than
using
if and else.

#include<stdio.h>

(including <stdlib.h> would have been nice)
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.
--
Winners dont do different things, they do things differently.


Losers write broken code like yours.


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

-nrk.
Nov 14 '05 #6
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> scribbled the following:
On Sat, 20 Dec 2003, Madhur wrote:
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. clrscr();

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 (pa*****@cc.helsinki.fi) ------------- 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
Nov 14 '05 #7

On Sat, 20 Dec 2003, Martin Ambuhl wrote:

Madhur wrote:

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

Nov 14 '05 #8
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...

Bruno

Nov 14 '05 #9
On 20 Dec 2003 18:28:46 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
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. :-(

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #10
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 <bd***********@tsoin-tsoin.free.fr> wrote in message
news:3f***********************@news.free.fr...
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...

Bruno

Nov 14 '05 #11
"Madhur" <a_******@vsnl.net> wrote in
news:bs************@ID-81175.news.uni-berlin.de:
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.
--
A. Sinan Unur
1u**@llenroc.ude (reverse each component for email address)
Nov 14 '05 #12
Madhur wrote:
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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #13
A. Sinan Unur wrote:
"Madhur" <a_******@vsnl.net> wrote in
news:bs************@ID-81175.news.uni-berlin.de:
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.


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.
* 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?


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.
* OK, i know void main should be int main,


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>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #14
On Sun, 21 Dec 2003 10:31:22 +0530
"Madhur" <a_******@vsnl.net> wrote:
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.

--
donLouis
Nov 14 '05 #15
On Sun, 21 Dec 2003 10:31:22 +0530, in comp.lang.c , "Madhur"
<a_******@vsnl.net> wrote:
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,
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #16
Madhur wrote:
Bruno Desthuilliers <bd***********@tsoin-tsoin.free.fr> wrote
Madhur wrote:

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


Err... Nice ?
....snip evil code ...> >
What does your compiler think of it ? Here's what mine
complains about :
.... snip extensive list ...


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #17
Madhur <a_******@vsnl.net> scribbled the following:
* 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?) =)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #18
Madhur wrote:
(please don't top-post - corrected)
Bruno Desthuilliers <bd***********@tsoin-tsoin.free.fr> wrote in message
news:3f***********************@news.free.fr...
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

Nov 14 '05 #19
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 <bd***********@tsoin-tsoin.free.fr> wrote in message
news:3f***********************@news.free.fr...
Madhur wrote:
(please don't top-post - corrected)
Bruno Desthuilliers <bd***********@tsoin-tsoin.free.fr> wrote in message
news:3f***********************@news.free.fr...
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

Nov 14 '05 #20
nrk
Madhur wrote:
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.


You're definitely showing the right attitude. Good for you. If you
disregard the harsher words of the regulars who responded, but pay
attention to the technical details that they point out, you are bound to
learn a lot of things that will stand you in good stead for the future.
Before you post further questions to this group, please do take the time to
read through the fine FAQ, available at:

http://www.eskimo.com/~scs/C-faq/top.html

One thing that you've not yet learnt is the art of bottom-posting. What
this means is that when you reply to an article, your reply goes beneath
what you quote. For instance, I am replying to you here, and my reply is
below what you said before. This way, someone who reads my response can
proceed logically from the top and not lose track of the conversation.
Most of the regulars prefer this form of posting and IMHO, it is good
usenet etiquette to do so. Along with that, learn to snip out parts of the
article you're quoting that's not relevant to your response. This keeps
the lengths of your articles to manageable limits.

-nrk.

<snip>

Nov 14 '05 #21
"Madhur" <a_******@vsnl.net> wrote...
what about this nice way to open a file in single line rather than using
if and else. 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.

Hi there,

This is indeed a neat little trick. However, what do you gain from it?
The answer is practically nothing, however you do loose syntax
clarity, readability, etc.

I think one of the most important principles in programming is
maintaining clarity in your expressions in preference to speed or
time. In other words, it might take you a few more seconds to type the
full if statement, but instantly other C programmers can understand
your code, and if you need to expand the 'failure' printf to two
statements in the future, you can do so easily.

Consider the readability of:

if (some_op() == FAIL)
{
/* handle failure. */
}
else
{
/* handle success. */
}
versus:

(some_op() == FAIL) &&
(/* handle failure */) ||
(/* handle_success */)
The only benefit I can imagine in the last example is that your
compiler might somehow output improved (or different) asm for the
expression, but this is unlikely. It is also a very bad strategy to
take whilest writing portable code. More importantly, it may screw up
optimisations your compiler could have otherwised performed if the
statement was written in the more traditional style.
David.
Nov 14 '05 #22
> > 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.]


[Spelling flame for "supressed" suppressed.]
file")&&exit(1);

^^^^^^^
Non-portable argument to exit() function.


What's all this cruft about EXIT_SUCCESS and EXIT_FAILURE anyhow? if
you have a program that can return N different error codes to the
operating system (eg: many unix tools), then EXIT_SUCCESS isn't
going to help much.

As far as "writing portable code" goes, unless there is an OS
standard that the C standard is aligning itself with, the return
values for main() or exit() are system-dependent (ie. nonportable)
anyway.
Nov 14 '05 #23
Old Wolf wrote:

What's all this cruft about EXIT_SUCCESS and EXIT_FAILURE anyhow? if
you have a program that can return N different error codes to the
operating system (eg: many unix tools), then EXIT_SUCCESS isn't
going to help much.
True. So those programs can't be written in strictly portable C. That's
not a huge surprise.

As far as "writing portable code" goes, unless there is an OS
standard that the C standard is aligning itself with, the return
values for main() or exit() are system-dependent (ie. nonportable)
anyway.


All exit codes other than 0, EXIT_SUCCESS, and EXIT_FAILURE are
non-portable. Those three are portable, and the standard defines their
meaning.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #24
Old Wolf wrote:
.... snip ...
file")&&exit(1);

^^^^^^^
Non-portable argument to exit() function.


What's all this cruft about EXIT_SUCCESS and EXIT_FAILURE anyhow?
if you have a program that can return N different error codes to
the operating system (eg: many unix tools), then EXIT_SUCCESS
isn't going to help much.

As far as "writing portable code" goes, unless there is an OS
standard that the C standard is aligning itself with, the return
values for main() or exit() are system-dependent (ie.
nonportable) anyway.


No, EXIT_SUCCESS and EXIT_FAILURE (and 0) have already taken into
account any OS peculiarities, and are thus portable. Anything
else is not.

If you need other values to interface with "unix tools" then your
program isn't portable, and should not be discussed here. What
could be simpler>

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #25
David M. Wilson wrote:
"Madhur" <a_******@vsnl.net> wrote...

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


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.


Hi there,

This is indeed a neat little trick.


Not even.

Nov 14 '05 #26
Richard Heathfield <do******@address.co.uk.invalid> wrote in
news:bs**********@sparta.btinternet.com:
A. Sinan Unur wrote:
"Madhur" <a_******@vsnl.net> wrote in
news:bs************@ID-81175.news.uni-berlin.de:
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 .
....
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.


The key point I should have made is to read one's compiler's
documentation of various warning levels, and turn them on.
* 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?


This is over the top.


It definitely is. My apologies.
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?
Nothing good. :)
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.


Definitely.

Sinan.

--
A. Sinan Unur
1u**@llenroc.ude (reverse each component for email address)
Nov 14 '05 #27
ol*****@inspire.net.nz (Old Wolf) writes:
[...]
[attributions missing]
file")&&exit(1);

^^^^^^^
Non-portable argument to exit() function.


What's all this cruft about EXIT_SUCCESS and EXIT_FAILURE anyhow? if
you have a program that can return N different error codes to the
operating system (eg: many unix tools), then EXIT_SUCCESS isn't
going to help much.

As far as "writing portable code" goes, unless there is an OS
standard that the C standard is aligning itself with, the return
values for main() or exit() are system-dependent (ie. nonportable)
anyway.


If you need to return more than two different status values, you can't
do it portably. But if all you need to indicate is whether the
program succeeded or failed, EXIT_SUCCESS and EXIT_FAILURE are
perfectly portable. exit(1) is not; in VMS, for example, odd status
results denote success.

Go ahead and write non-portable code if you need to, but *only* if you
need to.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #28
Bruno Desthuilliers <bd***********@tsoin-tsoin.free.fr> wrote...
This is indeed a neat little trick.


Not even.


Unless you have something to add, may I suggest you do not post? My
reply was designed to encourage and direct. Yours was spirit-crushing
condescension.
David.
Nov 14 '05 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greener | last post by:
Hi, I need help badly. Sorry for my ignorance! What's the comparable file in .js to Global.asa in ASP to capture the browser version (IE vs. Netscape)? Any existing code to share or if you have a...
2
by: Fraser Ross | last post by:
I want to prevent std::basic_ifstream from opening a file for reading when it is passed the file name with any wrong case of a letter. If that can't be done is there anything else that could be...
5
by: Dennis | last post by:
if you have the headers and main in below code snippet, the below ios::noreplace creates an error: error C2039: 'noreplace' : is not a member of 'basic_ios<char,struct std::char_traits<char> >'...
21
by: tyler_durden | last post by:
hi there peeps... like I say in the topic, I need to do an e-mail program in C language, and has to be made until the 3th of january..the problem is I'm having some problems with it. I was...
4
by: Satya | last post by:
I am trying to display a PDF file (which I am being passed from a web service as a binary stream) in a browser, but I am being prompted to save the file instead. I don't want the user to be...
1
by: Tim Marshall | last post by:
In my not too successful attempts to get an OLE chart object (Graph 11.0) that has been manipulated on a form to be reproduced on a report, I am considering the following procedure. First copy the...
2
by: Craig | last post by:
Hi there, I'm trying to open colour BMPs using PIL and I'm getting the following errors. Opening a 16 colour BMP I get: Traceback (most recent call last): File "<pyshell#3>", line 1, in...
15
by: Umesh | last post by:
how to open a website like yahoo.com and use it as input? The following code doesn't work. #include"stdio.h" int main() { FILE *f; f=fopen("http://www.yahoo.com","r"); if(f==NULL)
1
by: mohanprasadgutta | last post by:
Hi, I need help to open a password protected excel file in perl using Win32:OLE. when I tried to open file in normal way at the time of program execution it is prompting me to enter password. I am...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.