[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Why this doesen't work

Robert Bralic

6/13/2011 9:02:00 AM

#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...;)
robert.bralic@si.t-com.hr



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

The message was checked by ESET NOD32 Antivirus.

http://ww...




12 Answers

Kleuskes & Moos

6/13/2011 9:12:00 AM

0

On Jun 13, 11:01 am, "Robert Bralic" <robert.bra...@si.t-com.hr>
wrote:
> #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.

Joachim Schmitz

6/13/2011 9:14:00 AM

0

Robert Bralic wrote:
> 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

Ike Naar

6/13/2011 9:42:00 AM

0

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

On 2011-06-13, Robert Bralic <robert.bralic@si.t-com.hr> wrote:
> #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,
you should return 0 or return EXIT_SUCCESS (defined in <stdlib.h>).

> }

Dave \Crash\ Dummy

6/13/2011 12:54:00 PM

0

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.

Keith Thompson

6/13/2011 3:30:00 PM

0

"Robert Bralic" <robert.bralic@si.t-com.hr> writes:
> #include<stdio.h>
>
>
> int main(int argc, char *argv[]){
> FILE *fp;
> fp=fopen("LPT1:", "w");
> fprintf(fp,"Hello World");
> return 1;
> }

It's up to your operating system to decide whether writing to LPT1:
will work, and if it does work, what it does. On many systems,
your program will simply create a file called "LPT1:". (Possibly it
did that on your system and you didn't notice.)

Both fopen() and fprintf() return results that tell you whether they
succeeded or failed. (On many systems, they can also set errno,
though the standard doesn't require this.)

Also, you should fclose() any file(s) that you fopen(). All open
files are automatically closed when the program terminates, but if
you close it explicitly you can check the value returned by fclose().
(Some errors don't show up until you close the file; for example,
fprintf() might write to an internal buffer which isn't immediately
written to disk.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

pacman

6/13/2011 8:04:00 PM

0

In article <slrn3vfsivbmrc.qv4.ike@iceland.freeshell.org>,
Ike Naar <ike@iceland.freeshell.org> wrote:
>
>On 2011-06-13, Robert Bralic <robert.bralic@si.t-com.hr> wrote:
>> 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.

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

--
Alan Curry

Mark Bluemel

6/14/2011 7:49:00 AM

0

On 06/13/2011 10:01 AM, Robert Bralic wrote:
> #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.

Angel

6/14/2011 8:07:00 AM

0

On 2011-06-13, Robert Bralic <robert.bralic@si.t-com.hr> wrote:
> #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.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Keith Thompson

6/14/2011 12:10:00 PM

0

Angel <angel+news@spamcop.net> writes:
> On 2011-06-13, Robert Bralic <robert.bralic@si.t-com.hr> wrote:
>> #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?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Angel

6/14/2011 1:44:00 PM

0

On 2011-06-14, Keith Thompson <kst-u@mib.org> wrote:
>
> 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. :-)


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c