Why this doesen't work

R

Robert Bralic

#include<stdio.h>


int main(int argc, char *argv[]){
FILE *fp;
fp=fopen("LPT1:", "w");
fprintf(fp,"Hello World");
return 1;
}

Thanks in advance, Robert...;)
(e-mail address removed)-com.hr



__________ Information from ESET NOD32 Antivirus, version of virus signature database 6201 (20110612) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
K

Kleuskes & Moos

#include<stdio.h>

int main(int argc, char *argv[]){
   FILE *fp;
   fp=fopen("LPT1:", "w");
   fprintf(fp,"Hello World");
   return 1;
}

Two questions:
1) What doesn't work? Printing? Does the program crash? Return an
error?
2) Are you sure "LPT1:" is a valid stream-name? I'm not into dos/
windows myself, so i don't know.
 
J

Joachim Schmitz

Robert said:
Why this doesen't work
#include<stdio.h>


int main(int argc, char *argv[]){
FILE *fp;
fp=fopen("LPT1:", "w");
fprintf(fp,"Hello World");
return 1;
}

Define "doesn't work".
It seems to work just fine here:

$ cat hello.c
#include<stdio.h>


int main(int argc, char *argv[]){
FILE *fp;
fp=fopen("LPT1:", "w");
fprintf(fp,"Hello World");
return 1;
}
$ make hello
c89 -o hello hello.c
$ ./hello
$ echo $?
1
$ cat LPT1:
Hello World$


Bye, Jojo
 
I

Ike Naar

It would be good explain a bit what exactly doesn't work.
Like, what did you expect to happen, and what actually happened.

#include<stdio.h>

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

Not wrong, but since you don't use argc and argv, this would be a bit simpler:

int main(void) {
FILE *fp;
fp=fopen("LPT1:", "w");

It is always good to check whether fopen succeeds
(it returns a null pointer if it fails).
fprintf(fp,"Hello World");

Some environments require a newline character at the end of the file.
Try

fprintf(fp,"Hello World\n");
return 1;

The value 1 is not portable; if you want to indicate succesful termination,
 
H

Heinrich Wolf

If you mean to write to a printer - what type of printer is it? Some modern
printers do no more support text mode. Instead they need a driver, which
converts text to graphic.
 
A

Alan Curry

It is always good to check whether fopen succeeds
(it returns a null pointer if it fails).


Some environments require a newline character at the end of the file.

If it's a printer, better include "\f" too.
 
M

Mark Bluemel

#include<stdio.h>


int main(int argc, char *argv[]){
FILE *fp;
fp=fopen("LPT1:", "w");
fprintf(fp,"Hello World");
return 1;
}

You are Bill Cunningham and I claim my 5 gratuitous bugs.
 
A

Angel

#include<stdio.h>


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

Since you don't use argc and argv, replace this with:

int main(void) {
FILE *fp;
fp=fopen("LPT1:", "w");

You really should check the return value of fopen(). Also, this method
of writing to a printer will likely only work in MS-DOS. It's not gonna
work as expected on any brand of *nix. (You'll just create a file.)
fprintf(fp,"Hello World");

Again, check the return value for errors. Also, you should add \n to
indicate the line is complete, or it may not be printed.
return 1;

The standard says that returning a non-zero value from main() indicates
unsuccesful completion to the environment.

To avoid confusion, you might want to use the macros EXIT_SUCCESS and
EXIT_FAILURE (from stdlib.h) for your program exit codes.
 
K

Keith Thompson

Angel said:
#include<stdio.h>
int main(int argc, char *argv[]){

Since you don't use argc and argv, replace this with:

int main(void) {
FILE *fp;
fp=fopen("LPT1:", "w");

You really should check the return value of fopen(). Also, this method
of writing to a printer will likely only work in MS-DOS. It's not gonna
work as expected on any brand of *nix. (You'll just create a file.)
fprintf(fp,"Hello World");

Again, check the return value for errors. Also, you should add \n to
indicate the line is complete, or it may not be printed.
return 1;

The standard says that returning a non-zero value from main() indicates
unsuccesful completion to the environment.

To avoid confusion, you might want to use the macros EXIT_SUCCESS and
EXIT_FAILURE (from stdlib.h) for your program exit codes.

Every one of those points has already been made. Did the followups
not appear on your news server?
 
A

Angel

Every one of those points has already been made. Did the followups
not appear on your news server?

They probably did, but I don't have a running archive in my head about
what's been said and what has not. Besides, they say redundancy is the
key to learning. :)
 
M

Morris Keesan

The standard says that returning a non-zero value from main() indicates
unsuccesful completion to the environment.

No, it doesn't. See 5.1.2.2.3 and 7.20.4.3. A return value of zero
indicates success, but the meaning of any other return value, except
EXIT_SUCCESS and EXIT_FAILURE, is implementation-defined. It's
acceptable for "return 1" to mean "success" in a particular
implementation.
To avoid confusion, you might want to use the macros EXIT_SUCCESS and
EXIT_FAILURE (from stdlib.h) for your program exit codes.

Agreed.
 
D

Dr Nick

Mark Bluemel said:
#include<stdio.h>


int main(int argc, char *argv[]){
FILE *fp;
fp=fopen("LPT1:", "w");
fprintf(fp,"Hello World");
return 1;
}

You are Bill Cunningham and I claim my 5 gratuitous bugs.

I notice we've had no followups. I wondered if it's the occasional troll
(well, I assume troll) who posts very old questions for no obvious
reason but there's no sign of this in the Google archives (but we know
searching is pretty poor). More critically, I think it would have had a
conio header and a clrscrn as well!
 

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
474,342
Messages
2,571,409
Members
48,798
Latest member
massivestack

Latest Threads

Top