Thread: Printing in XP using C

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Printing in XP using C

    Hi all

    I just started my C programming lessons and have this doubt. Assume that I have the typical ‘hello world’ program. How can I programmatically redirect the output to a printer using DOS interrupts? I have XP with sp2 and a Canon PIXMA iP1000 USB printer. The main problem is I cant do any printing from the command prompt. I tried both sharing the printer and spooling it with LPT1 but of no use. Does it mean that using C, we can not do any serious programming (that includes printing) in XP? If we can, plz help me with code...Seniors and C Gurus plz help……..thanks in advance

    - jay

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Sorry, this makes me chuckle, a little...

    I get amused by people that still write console programs and get concerned about the capabilities of C because they don't know the code.

    Yes, there is ways to send output to the printer, but it's very OS specific. For windows, I would check out http://www.msdn.com they would most likely give you an idea of what to do. And don't worry about the capabilities of C, there is plenty more to it that you don't know.
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    I started learning C only in Feb 2006 and so far, I asked this question to more than 10 pple, including my faculty. Everyone say C is very powerful, it can do this it can do that ( I agree to and I DON’T DISAGREE, and that’s why I started learning it) but not even a single person gave me a specific answer though they claim many years of programming experience. Of course they have answers like, ‘if I were you…….’, or ‘ it should be possible……’, ……’there must be away……’ I posted my doubt to get some help and I don’t have any shame to admit that , till date I don’t know how to do it. And I believe that it is better than bluffs and boasting………..sorry if you don’t have a specific answer, forget it, but don’t put water on others enthusiasm.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What specific answer? I told you it was OS specific, and I gave you a link to a site that will have your answer.

    You have to understand something... years of programming experience doesn't mean the person can pull any library function off the top of their head without having to think about it. Especially any OS specific, non standard function such as what you're talking about. Programming experience means they know structure, logic, and perhaps more importantly, how to do their own research when they need syntax.
    Sent from my iPad®

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    It's an old thread, but my code should still work.

    It's C++ BTW, but can be quickly converted

    http://cboard.cprogramming.com/showt...ighlight=print

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You can learn VB. I think that will be better for you. It is simple and you can do everything you want with it quickly. The term powerful that is usually used for C is sometimes misleading. Think if C is the most powerful, why others exist?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    printing to laser printers can get very very complex in any language. In C on MS-Windows os, if you have a printer attached to your local printer's LPT1 port, you could just simply open the file named "LPT1" and write ascii text to it. The output on the printer will not be very pretty, but it will work.
    Code:
    int main()
    {
       FILE * fp = fopen("LPT1:","w");
       if(fp)
       {
             fprintf(fp,"Hello World\n");
             fclose(fp);
       }
    }

    If your computer is on a network and you want to write to a network printer, then it gets pretty complex. use win32 api function OpenPrinter() to get a handle to the printer. See MSDN for other assiciated functions.

    Then if you want to draw lines, bitmaps, pictures, or pretty fancy reports, even the most experienced programmers will have a difficult time in just C. There are c++ classes that simplify the work, but its still not an easy task.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by Ancient Dragon
    Then if you want to draw lines, bitmaps, pictures, or pretty fancy reports, even the most experienced programmers will have a difficult time in just C. There are c++ classes that simplify the work, but its still not an easy task.
    On windows it's far easier than you think. The GDI functions are part of the windows API and work fine with C. Fonts, bitmaps, 2D shapes, text - all need a standard device context & a few API calls any your away.

    Elaborate classes will end up calling the GDI functions to do the work

    (The device context for the screen can also apply to the printer)

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>Elaborate classes will end up calling the GDI functions to do the work

    Yup -- which is why I used them, so that I don't have to reinvent the wheel All I did was tell the class what to print where and it did its magic -- I don't have to know the first thing about win32 api gdi functions.
    Last edited by Ancient Dragon; 03-01-2006 at 04:25 PM.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Thanks

    Finally, I feel like I am in the right place. And if I am with people of this caliber, around me, I don’t have any doubt that I can go ahead with my C learning. Thanks to all who replied, including SlyMaelstrom.

    I don’t believe in ‘just arguments’ but still cant avoid a comment to SlyMaelstrom. You asked ‘what specific?’ did you read my doubt, in the first place. I was very specific about what OS (and what printer) I have. I am aware that had I been working in DOS, this doubt wouldn’t have arisen in the first place. I am aware that things will be different with UNIX (which I don’t know). I am very much aware that NT based OS wont allow to access its parallel ports just like that.

    And you want to know what ‘specific answers are? Just read the replies I got from others. Programming experience has more to do with what you think. Yes, I am here coz of my ‘research’ to find out the syntax, and I got it., as you can see. Otherwise I would have just sat thinking ‘C is really stupid’.

    But after reading the experiences of senior programmers, I got a clear idea that, though I asked a very silly looking doubt, it has its depth (thanks to Ancient Dragon). Siavoshkc’s reply wasn’t to the point at all (by suggesting to learn VB) but he raised a very brilliant question – if C is the most powerful, why others exist?.

    MSDN is meant for developers and not for those learning the basics (like me). MSDN might have answers for almost all doubts which had been posted here, earlier. Then why do we need such a group?

    When I registered with this Forum, I considered this as my study group and I am not here to insult / underestimate someone nor get insulted/underestimated by someone. And moreover we don’t know the credentials of others.

    Thanks a ton Fordy, for the code. I was sure that somebody might have already come out with a solution.
    Last edited by j2005; 03-02-2006 at 01:49 AM.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Don't get unhappy because of the answers you may get here. This forum is full of people with different minds and thinkings. Sometimes the forum is more like a battle front. But interestingly it is possible that in your next thread the only one who gives you the right answer be SlyMaelstrom! SlyMaelstorm just wanted to point that knowing a function to do something is nothing to do with programming experience. For example the programmer who writes the code for F-22 fighter is very experienced (in Ada language). But she/he may not be able to write the code for playing music in windows.
    I started learning C only in Feb 2006 and so far, I asked this question to more than 10 people, including my faculty. Everyone say C is very powerful, it can do this it can do that ( I agree to and I DON’T DISAGREE, and that’s why I started learning it)
    The language we use depends on what we want to write with it. Almost all modern general-purpose languages (Delphi, Perl, Eiffel, C#, VB, Python, Java, Ada, Smalltalk, Assembly, etc) can do the same things, it is nothing to do with the language (though they do the same things in different ways). The C++ has these advantages among others:
    - It generates fast code (only hand generated assembly code can be faster).
    - It generates the smallest code with minimum overhead.
    - Its code is portable.
    - Gives you more control over hardware and the way your code executes.
    If your programs should meet the above criteria, stick with C++ else I strongly recommend go for another.
    I said VB because it won't bother you with functions you should use for various purposes. Because it has many of them included. Debugging is much easier with it because it can execute your code lines one by one before compile (C++ can't) and trace the program so easily. Many of the programs you see can be written by VB. Delphi is another good language.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    'Experience' form experience

    Thank a lot siavoshkc, for your suggestions. It was useful. But I differ a lil bit wrt the concept of ‘experienced programmer’. I know this is not a place to post my bio-data or any family matters, but forgive me for this time.

    I don’t know the age of any of the members here. Also I don’t have any idea about their programming experience. I am 39 years old. Was mostly in the field of Accounts and then business. I did my first computer course in an IBM PC 286XT (dBase III Plus -a package beautifully written in C), about 15 years back. And I have done many simple programs in dbase (which included lots of printing jobs) to help my own work, in the initial years. Then I lost touch for a very long period, during which I was using custom made applications and I learnt more hardware and networking stuff than programming.

    Now I took up a course where C is a must as per the syllabus (so, I don’t have any choice). My wife is a very experienced database (Oracle/SQL) programmer who works with one of the top IT companies in the world. I am telling this just to make it clear that I know what experience means.

    When this doubt came, I first asked my wife. She said she doesn’t know.. I didn’t ask her ‘then what the hell you people are doing there? Cant you even write a silly program in C which prints hello world?’. Why? Coz, her core skill is Oracle and not C. I asked her coz, ‘just in case’ she knew it, my problem is solved.

    And that’s why I came to this place, where the meeting of exclusive C pple happens. That means, it is understood that anybody who gives an answer has knowledge in C. When I use the word ‘experience’ in this section of the forum, it becomes ‘C experience’ by default. If any doubt in other topics arises, I won’t ask it here.

    From my experience, it is easy for an experienced programmer to find out a solution (even if it is finding out a ‘not standard function’) than a beginner. It also has something to do with the knowledge about the functions (in this context) that are available, else he will try to write own library, instead of using the existing one (a possibility). I don’t mean that one should always remember all those things, but the knowledge of the possible functions comes through experience (eg: when looking for a particular one, one might have come across 10 others and this comes as a ‘flash’ when you actually need one of them from that 10, at a later period) He/she can find it very fast too. This ‘quality’ is additional to his/her other skills mentioned by others here. One develops this ‘quality’ by experience and not by mugging up all functions or syntaxes.

    Whenever I needed some ideas/suggestions in connection with electrical installation, I used to consult one of my relatives who is in the field from his teens, though he is very younger to me. I too was dealing with electrical stuff (but products) and hence I could really make out what his ‘experience’ counts.

    So, programming experience is not just knowing the structure , logic and the ability to do own research, but it ALSO IS. SlyMaelstorm was very much right when he said “….and perhaps more importantly, how to do their own research when they need syntax.” . When somebody asks a doubt in a forum, it becomes ‘own’ problem for everyone who really want to help by giving a reply. Thus they come out with solutions faster than a beginner. (At least I have done my own research just to give a reply, in some other place and context).Otherwise why should we waste our time and money? Nobody in this world do anything just to satisfy others.

    This is a place where people share their knowledge and experience unlike in many other places where people hesitate to share their knowledge due to professional jealousy and internal politics (which I observed from my practical experience).

    What if only SlyMaelstorm can give the right answer to my next thread? I will be very happy. I just expressed my disagreement with his opinion this time (or better say, I added some more points ). It wasn’t personal or permanent. I would like consider this place as a place where friends (with different opinions and attitudes) meet and not as a battle field where only one can win.
    Last edited by j2005; 03-03-2006 at 05:00 AM.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    j2005,keep writing more like those,i find your writings very entertaining.
    Last edited by qqqqxxxx; 03-03-2006 at 03:01 AM.

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The problem is when somebody first asks a question in forum, sometimes it is hard to determine his/her level of knowledge. Sly said don't worry about the language, it just needs a function that you can find in MSDN. I think all programmers in the whole world know that the resource of Windows programming is MSDN. So giving it as an answer is useless and Sly can explain his answer himself.

    What I meant was that don't take somebody's answer as an absolute answer, this is a forum, and you can wait for others.

    If your course includes C as the programming languages, there is no way to escape from it. But learning any structured language means learning structured programming and it will be easy to learn a new structured language quickly.
    Once I migrate from QBASIC to C, I was really confused: Why they say this is a powerful language? It can't even do simple things. Compare:
    Code in QBASIC:
    Code:
    INPUT "Enter your name: ", name$
    INPUT "Enter your age: ", age
    Var$ = name$ + " is " + STR$(age) + "years old."
    PRINT var$
    The equal code in C:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char name[20]="";
    	char var[50]="";
    	char age[6]="";
    	printf("%s","Enter your name: ");
    	gets(name);
    	printf("%s","Enter your age: ");
    	gets(age);
    	strcat(var, name);
    	strcat(var, " is ");
    	strcat(var, age);
    	strcat(var, " years old.");
    	printf("%s\n", var);
    	return 0;
    }
    C eats more code and if your name be more that 20 chars program crashes. If you enter the age "none of your business" it again crashes. Even if you enter "tt" for age it will accept it because age is an array of chars. But none of these is the case in QBASIC.
    It is possible to correct these errors of course (with use of scanf() maybe), but it is not easy and takes much time for somebody that wants the computer to do a job and don't want to know how it will be done by it.
    The problem with C is that you have to know many non-relevant things, to do a particular job.

    This is a place where people share their knowledge and experience unlike in many other places where people hesitate to share their knowledge due to professional jealousy and internal politics (which I observed from my practical experience).
    I agree, all people here should try hard to keep it right.

    Sorry, my composition is not as good as you.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by siavoshkc
    Once I migrate from QBASIC to C, I was really confused: Why they say this is a powerful language? It can't even do simple things.
    So you are saying that because BASIC allows you to write in 4 lines what you wrote in 17 that BASIC is more powerful? And you use a well known dangerous function that's easily avoided and won't crash as a language problem?

    And let's optimize your code:

    Code:
    #include <stdio.h>    // Necessary default, not really part of code
    
    int main()
    {
    	char name[20];  // define your variables
    	char age[6]="";
    
    	printf("%s","Enter your name: ");
    	fgets(name, 20, stdin);
    	printf("%s","Enter your age: ");
    	fgets(age, 6, stdin);
    	printf("%s is %s years old.\n", name, age);
    	return 0;
    }
    If you consider a better language combines functionality into fewer statements, you should look into RPG II. It's much more efficient by that definition.

    Your objections to C obviously come from a programmer's standpoint who is very new to C and well versed in BASIC. I actually use BASIC today and you can take it from a programmer of over 30 years (if you wish) that:
    1) BASIC is easier to write fast code in, but has extreme problems with readability and modularity in large tasks.
    2) C has more capabilities and can handle huge systems without the overhead and difficulties than BASIC.

    BASIC simply does half the work for you. C makes you do the work. This is why BASIC stands for Beginner's All-purpose Symbolic Instruction Code. There's a place for both. If you wish to really compare them, learn to program C properly before making claims as you have.

    It's kind of like cooking. You can make a cake using flour, eggs, etc. (the C concept) or open a box of cake mix (the BASIC concept).
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. Windows XP regression over time
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-17-2002, 10:49 AM