Thread: Error in code - please help

  1. #1
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68

    Error in code - please help

    The purpose of the code is to print something
    Question: what is wrong in the error line?
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    main()
    {
        char ch;
        FILE *fp;
        puts("1\n");
        fp = fopen("test.txt","r");
        if(fp == NULL)puts("File not found.");
        puts("2\n");
        while(ch == fgetc(fp))// error line
        {
            puts("3\n");
            fputc(ch,stdout);
        }
            puts("4\n");
            fclose(fp);
            getch();
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    ch should be an int (fgetc returns an int so that EOF can be distinguished from all possible char bit patterns).
    You are using == (equality comparison) when you mean to use = (assignment).
    You need to compare to EOF to test for the end of the file.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        FILE *fin = fopen("test.txt","r");
     
        if (!fin)
        {
            puts("File not found.");
            exit(EXIT_FAILURE);
        }
     
        for (int ch; (ch = fgetc(fin)) != EOF; )
        {
            putchar(ch);
        }
     
        fclose(fin);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    [QUOTE=john.c;1303848]ch should be an int (fgetc returns an int so that EOF can be distinguished from all possible char bit patterns).
    You are using == (equality comparison) when you mean to use = (assignment).
    You need to compare to EOF to test for the end of the file.
    [code]

    Thank you.

    How do you get that code to output to the printer?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by Paderi View Post
    How do you get that code to output to the printer?
    Assuming you're on Windows, I don't know exactly.
    Either you can open up some special "file" as the printer and output to that.
    Or you can redirect the output to the printer on the command line.
    Or you can output to a file and use system() to run a program to print the file.
    Hopefully someone else can fill you in on the details.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by john.c View Post
    Assuming you're on Windows, I don't know exactly.
    Either you can open up some special "file" as the printer and output to that.
    Or you can redirect the output to the printer on the command line.
    Or you can output to a file and use system() to run a program to print the file.
    Hopefully someone else can fill you in on the details.
    This is very useful, thank you.

    Given: in fputc(ch,stdout);

    I am wanting to use the code on a W10 box. I was hoping that it would be possible to redefine "stdout" as the default printer. Else stdout leads to the display.
    Last edited by Paderi; 12-14-2021 at 07:09 PM. Reason: better

  6. #6
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68

    Further searching for answer

    I found the code below upon further searching for information, however, the code does not work: "error opening printer". Please help.

    Code:
    int main(void) 
    { 
      FILE *prnt;
      
      prnt = fopen("/dev/lpt1", "w"); 
      if (prnt)
        fprintf(prnt, "This is a test.\n"); 
      else
        printf ("error opening printer\n");
      fclose(prnt); 
      return 0;
    }
    Last edited by Paderi; 12-15-2021 at 03:56 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well in DOS/Windows there is no /dev, that comes from Unix/Linux land.

    Which compiler are you using?

    Also, where is your printer plugged into your PC?
    - a real printer port (like your machine is 20 years old, and so is your printer)
    - a USB port
    - some other machine on the local network, or is a network attached printer.

    If it's a network/USB printer, then you may need to use the 'net use' command to associate the new device with the old DOS-style device name.
    Net use | Microsoft Docs

    You could try one of these to begin with.
    Code:
    prnt = fopen("PRN", "w"); 
    prnt = fopen("PRN:", "w"); 
    prnt = fopen("LPT", "w"); // and the appended : variant
    prnt = fopen("LPT1", "w"); // and the appended : variant
    In each case, see if you end up with a text file if you don't see printer output, and you don't see "error opening printer".

    > fprintf(prnt, "This is a test.\n");
    You should also finish by sending a \f (form-feed) to cause the printer to complete the page.
    Eg.
    fprintf(prnt, "This is a test.\n\f");
    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.

  8. #8
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by Salem View Post
    Well in DOS/Windows there is no /dev, that comes from Unix/Linux land.

    <snipped>;
    Thank you, Salem. for responding.

    I tried via network printing, and by connecting the printer to the PC. It looks like LPT is being accepted, nevertheless the printer isn't responding. I am no longer expecting to get to the bottom of this. (As to my PC and printer. they are both recent enough. (An I7 processor.)
    Last edited by Paderi; 12-15-2021 at 06:28 AM.

  9. #9
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    If you don't mind using the Win API then have a look here: c - Printing a file in Windows - Stack Overflow
    and scroll down down to 2 Answers.

  10. #10
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    Quote Originally Posted by thmm View Post
    If you don't mind using the Win API then have a look here: c - Printing a file in Windows - Stack Overflow
    and scroll down down to 2 Answers.
    Wanting to print to the printer from a c application seems to end you up in a deep Microsoft hole.

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    What is the output of the command: net use
    How about: wmic printer list brief
    A little inaccuracy saves tons of explanation. - H.H. Munro

  12. #12
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    In the past, when Microsoft didn't yet have its claws on everything between the toilet of the programmer and the toilet of the end user, you could simply send something from the program you were writing, to any old printer printer.

  13. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    It's a matter of how to refer to the printer in C code. In the past you could say stdprn or "LPT1". If you answer my questions (by typing the commands into a console and posting the output) we might be able to do something similar.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  14. #14
    Registered User Paderi's Avatar
    Join Date
    Apr 2020
    Posts
    68
    I apologize for not responding to your previous post. I did look up your reference and was overwhelmed.
    Net use produced all sorts of results like e.g., viz:
    What is the use of NET USE command?
    Net use. Connects a computer to or disconnects a computer from a shared resource, or displays information about computer connections. The command also controls persistent net connections. Used without parameters, net use retrieves a list of network connections. For examples of how this command can be used, see Examples.
    Net use | Microsoft Docs
    It is 0100H here, I would like to get back to you tomorrow.





  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Forget it.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Blocks showing error in code! Don't know what's it.
    By kdushyant297 in forum C Programming
    Replies: 2
    Last Post: 09-08-2017, 09:59 AM
  2. code error
    By nadji in forum C Programming
    Replies: 4
    Last Post: 06-25-2012, 10:03 AM
  3. Is there an off-by-one error in this example code?
    By edw211 in forum C Programming
    Replies: 3
    Last Post: 06-14-2011, 07:21 PM
  4. Help with VC++ 6.0 (or my code) error
    By theodoreius2000 in forum Game Programming
    Replies: 7
    Last Post: 07-13-2004, 09:48 PM
  5. Need help w/code error
    By LouB in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2003, 06:23 PM

Tags for this Thread