[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:04: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...




5 Answers

Eric Sosman

6/13/2011 12:06:00 PM

0

On 6/13/2011 5:03 AM, Robert Bralic wrote:
> #include<stdio.h>
>
>
> int main(int argc, char *argv[]){
> FILE *fp;
> fp=fopen("LPT1:", "w");

You should check here to see whether fopen() succeeded. If
it failed, fp will be NULL indicating that "LPT1:" could not be
opened (for whatever reason). If fp is non-NULL, fopen() succeeded
and you can go ahead and use it.

> fprintf(fp,"Hello World");

On some systems, a "line of text" isn't complete until you've
written a newline character at the end.

> return 1;

On most systems, a non-zero exit status means "I failed."

> }

There are other, probably less important flaws in the code,
but I'm not going to bother with them until and unless you explain
just what you mean by "doesen't work." In what way does it fail
to work? What were you expecting the program to do, and what did
it do instead?

--
Eric Sosman
esosman@ieee-dot-org.invalid

Kenneth Brody

6/13/2011 4:08:00 PM

0

Question asked in subject: "Why this doesn't work...?"

On 6/13/2011 5:03 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;
> }
>
> Thanks in advance, Robert...;)

Does it fail to compile? Does it crash when you run it? Did the fopen()
return a non-NULL value? (Always check the return from fopen.) Assuming
that the name "LPT1:" is supposed to be a printer, does it fail to print
anything? Continuing that assumption, what type of printer does "LPT1:"
supposedly point to? Further continuing that assumption, as you sure that
"LPT1:" does, in fact, represent that printer?

In short, define "doesn't work".

(Or, as I've decided to say in another venue, "DDW".)

--
Kenneth Brody

J. J. Farrell

6/13/2011 6:49:00 PM

0

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

Works fine for me. Perhaps you should tell us in what way it doesn't
work for you.

Bartc

6/13/2011 10:15:00 PM

0

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

Looks like Windows.

In that case, first establish that you have a working printer attached, by
typing this in a console:

copy filename LPT1:

(with and without the colon, and where filename refers to an existing text
file). If that works then you can go back to the code.

If the copy command doesn't work, there could be a million reasons why not.

--
Bartc

Kenneth Brody

6/14/2011 3:16:00 PM

0

On 6/13/2011 6:15 PM, BartC wrote:
> "Robert Bralic" <robert.bralic@si.t-com.hr> wrote in message
> news:it4jp9$gth$1@ss408.t-com.hr...
>> #include<stdio.h>
>>
>>
>> int main(int argc, char *argv[]){
>> FILE *fp;
>> fp=fopen("LPT1:", "w");
>> fprintf(fp,"Hello World");
>> return 1;
>> }
>
> Looks like Windows.
>
> In that case, first establish that you have a working printer attached, by
> typing this in a console:
>
> copy filename LPT1:
>
> (with and without the colon, and where filename refers to an existing text
> file). If that works then you can go back to the code.
>
> If the copy command doesn't work, there could be a million reasons why not.

While our assumption that the OP is on Windows is probably correct,
everything else is just guesswork until he defines "doesn't work".

Also, you are assuming that the "copy" command treats "LPT1:" the same as
the C runtime library. This is not necessarily the case. For example, this
command displays the file in the console window:

copy usenet.c con

yet this does not:

cp usenet.c con

(Where "cp" is a utility I have which performs the same as the *nix "cp"
command.) In this case, I get the error:

cp: Error copying file usenet.c to con: The handle is invalid.

Which, strangely enough, probably means that the open succeeded, but the
write failed. The OP needs to check the return from fprintf() as well as
fopen().

--
Kenneth Brody