Thread: Help on printing files

  1. #1
    Unregistered
    Guest

    Question Help on printing files

    I was wondering how i could print out a file, like send document1.txt to the printer in c++
    if anyone can help me thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try this to start:
    Code:
    file *printer = fopen ( "LPT1", "w" );
    .
    .
    .
    fprintf ( printer, "%s\f", string );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    It'd probably be like
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
     FILE *printer = fopen("LPT1","w");
     FILE *fp;
     char str[256];
     char buf[BUFSIZ];
     printf("File name? ");
     scanf("%s",str);
     fp = fopen(str,"r");
     if(!fp)
     {
      printf("File does not exist\n");
      return -1;
     }
     fgets(buf,BUFSIZ,fp);  // this may not be the correct syntax
     while(buf[0])
     {
      fprintf(printer,"%s",buf);
      fgets(buf,BUFSIZ,fp);  // this may not be the correct syntax
     }
     fprintf(printer,"\f");
     return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fgets(buf,BUFSIZ,fp); // this may not be the correct syntax
    It is, but what if the size of buf is changed? You don't want to go through the program changing every fgets call, this works better:
    fgets ( buf, sizeof buf, fp );

    -Prelude
    My best code is written with the delete key.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Why does everyone use the C style streams instead of C++, is it better than fstream?

  6. #6
    jloyd01
    Guest

    Works?

    I tried this code. It seems to have an infinte loop. The program created a huge file to the printer that kept printing the last part of the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. printing txt files
    By baphomet in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-10-2002, 09:53 PM