Thread: OUTPUT TO PRINTER using c++ ???

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    Question OUTPUT TO PRINTER using c++ ???

    This is my program for printing a file using c++ program.
    This gets complied and linked properly.
    but when i run it , then it does not print anything.

    its OUTPUT

    Input file opened
    OUTPUT file opened
    in of loop
    in of loop
    in of loop
    in of loop
    in of loop (it gets printed equal to the number of chars in the file)

    out of loop

    then it gets hanged...


    where is the problem ????

    Code:
    #include<iostream>
    #include<fstream>
    #include<conio.h>
    
    void main()
    {
        char ch;
        
        ifstream infile("tem.txt");
        if(!infile)
        {
            cout<<"error in opening the input file";
            getch();
        }
        else
        {
            cout<<"Input file opened\n";
        }
    
        ofstream outfile("PRN");
        if(!outfile)
        {
            cout<<"error in opening the output file";
            getch();
        }
        else
        {
            cout<<"OUTPUT file opened\n";
        }
        
    
        while(infile.get(ch)!=0)
        {
            cout<<" \n in of loop"
            outfile.put(ch);
        }
    
        cout<<" out of loop";
    
    }

    where is the problem???

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    try !outfile.is_open()


    and

    you can use


    char ch;
    infile >> ch; //where infile is the file your reading from
    outfile << ch; //to write to the outfile

    in the while loop to get one character at a time.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    !outfile.is_open() tried

    same ouput





    ???????????????

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    did you try closing the streams and returning something from main?

    and how do you know it's "hanging"? printing "out of loop" is the last thing you told it to do...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    new lines of code added in bold

    Code:
    void main()
    {
    ...................
    .................
    ................
    ..................
    
    while(infile.get(ch)!=0)
        {
            cout<<" \n in of loop"
            outfile.put(ch);
        }
    
        cout<<" out of loop";
    
       // i added these addtional lines 
        getch();
        cout<<"\n out of loop 44";
        cout<<"\n  out of loop 55";
    
        return;
    }


    its OUTPUT

    Input file opened
    OUTPUT file opened
    in of loop
    in of loop
    in of loop
    in of loop
    in of loop (it gets printed equal to the number of chars in the file)

    out of loop
    (stops for a char input)
    out of loop 44
    out of loop 55

    then it got hanged....
    and i had to terminate the execution.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Seems to be a lot of void main programmers about at the moment.
    main returns int.

    > then it does not print anything.
    I'm assumine that you mean there is no piece of paper with your text on it?

    > ofstream outfile("PRN");
    Check that you didn't actually create a file called "PRN". Microsoft's ideas on device names sucks.
    Sometimes, "PRN:" seems to be a better idea.

    Also, just before you close the output file, do this
    Code:
    outfile.put('\f');
    This is a form-feed character to tell the printer the current page is now complete, and to print what it's buffered up so far.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Salem says
    Code:
    outfile.put('\f');
    This is a form-feed character to tell the printer the current page is now complete, and to print what it's buffered up so far
    yeah, that was imp,
    thanks for reminding.

    salem
    ofstream outfile("PRN");
    Check that you didn't actually create a file called "PRN". Microsoft's ideas on device names sucks.
    Sometimes, "PRN:" seems to be a better idea.
    i checked it, it does not matter.




    now....
    what should i do..........
    output still the same..........

    ??????
    ????????????

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Repost your latest code and describe what you see / expect.

    Lot's of ... and ??? do not constitute a decent description.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Latest Code:
    Code:
    #include<iostream>
    #include<fstream>
    #include<conio.h>
    
    void main()
    {
        char ch;
        
        ifstream infile("tem.txt");
        if(!infile)
        {
            cout<<"error in opening the input file";
            getch();
        }
        else
        {
            cout<<"Input file opened\n";
        }
    
        ofstream outfile("PRN");
        if(!outfile)
        {
            cout<<"error in opening the output file";
            getch();
        }
        else
        {
            cout<<"OUTPUT file opened\n";
        }
        
    
        while(infile.get(ch)!=0)
        {
            cout<<" \n in of loop"
            outfile.put(ch);
        }
        outfile.put('\f');
    
        cout<<" out of loop";
        getch();
        cout<<"\n out of loop 44";
        cout<<"\n  out of loop 55";
    
        return;
    }
    its OUTPUT

    Input file opened
    OUTPUT file opened
    in of loop
    in of loop
    in of loop
    in of loop
    in of loop (it gets printed equal to the number of chars in the file)

    out of loop
    (stops for a char input)
    out of loop 44
    out of loop 55

    then it got hanged....
    and i had to terminate the execution.

    nothing is printed .

    now where is the problem ??

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Dunno for sure, but here's some points

    > #include<iostream>
    Where is the
    using namespace std;

    > void main
    main returns an int.

    > outfile.put('\f');
    After this, why not have say
    Code:
        outfile.close();
        cout<<" out of loop";
        getch();
    It would be useful to know whether it hangs on the actual close (which it will do for you anyway).

    > nothing is printed .
    Did you switch the printer on?
    Try printing something using say notepad, both BEFORE and AFTER your program is run. It would also be interesting to see if your output appears in between printing two other things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is there a C++ equivalent of this?
    Code:
    fprintf(stdprn, "Error\n");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    stdprn << "Error!!\n";

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    Salem
    > #include<iostream>
    Where is the
    using namespace std;

    > void main
    main returns an int.
    done, but no difference in output.


    > outfile.put('\f');
    After this, why not have say

    Code:
    outfile.close();
    cout<<" out of loop";
    getch();
    i think c++ will close the outfile automatically when the dtor will be called.
    any way i tried it, but no difference in output.



    > nothing is printed .
    Did you switch the printer on?
    Try printing something using say notepad, both BEFORE and AFTER your program is run. It would also be interesting to see if your output appears in between printing two other things.
    printer is on and ok
    it prints my other documents from notepad,Mword etc..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  3. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM