10 Replies - 5585 Views - Last Post: 10 April 2011 - 11:07 AM Rate Topic: -----

#1 sfontana  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 16-March 11

Print The Output Of C++ Program to Printer

Posted 09 April 2011 - 10:59 PM

hei i need a help to make my display records direct to printer...
here my code
im using C++ win32, visual basic 2008

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // system()
#include <conio.h> // getch()
#include <sys\stat.h> // stat(status of a file)
#include <iomanip>
#include <ctime>
using namespace std;
 
 
//Data types
struct SUPERMARKET_Record // item record
{  
	int productid;
	 string barcodenum;
	 string nameofitem;
	 string amountofitem;
	 string itemdetails;
	 string date;
	 string price;
	 bool deleted;
};
 
 
//Variable Declarations & Constants
#define SUPERMARKET_FILE_NAME "SUPERMARKET.txt" // name of the database file to store SUPERMARKET informations

SUPERMARKET_Record SUPERMARKET;
int Auto = 1000;
char     choice; // for choice in menu
fstream *fs = NULL, *fs1 = NULL;// file streams for files : fs -> 'SUPERMARKET', fs1 -> 'temp'
bool     deletion = false; // if any record has been deleted

//Functions 
void closeFile(fstream *); // closes a file with its pointer, then deletes the file pointer
bool isFileExist(const char *); // check if a file exists
 
 
//Main
int main()
{
	double total;
	ifstream input;
	input.open("id.txt"),ios::in;
	input >> Auto;
	ofstream output;
	cout << "\n\n\n\n\n\n";
	cout << "           #===============================================================#   \n";
	cout << "           #          H    H  EEEE  L     L     OOOO      !!               #   \n";
	cout << "           #          H    H  E     L     L     O  O      !!               #   \n";
	cout << "           #          H HH H  EEE   L     L     O  O      !!               #   \n";
	cout << "           #          H    H  E     L     L     O  O                       #   \n";
	cout << "           #          H    H  EEEE  LLLL  LLLL  OOOO      @@               #   \n";
	cout << "           #===============================================================#   \n";
	cout << "           #  W           W  EEEE  L     CCCC  OOOO  MM   MM  EEEE         #   \n";
	cout << "           #   W         W   E     L     C     O  O  M M M M  E            #   \n";
	cout << "           #    W   W   W    EEEE  L     C     0  O  M  M  M  EEEE         #   \n";
	cout << "           #     W W W W     E     L     C     0  O  M     M  E            #   \n";
	cout << "           #      W   W      EEEE  LLLL  CCCC  0000  M     M  EEEE         #   \n";
	cout << "           #===============================================================#   \n\n";
	cout << "                        "; system("pause");
	
	while (true)
   
      {
            do
		
            {
                  system( "cls" );
				  cout<<endl;
                  cout << "\t*************************************************************\n" ;    
                  cout << "\t*         .:: Welcome To SuperMarket Database Program::.    *\n";
				  cout << "\t*                    By : SERGIO FONTANA                    *\n";
				  cout << "\t*************************************************************\n"<<endl;
                  cout<<endl<<endl;
				  cout << "\t\t\t#########################\n";
				  cout << "\t\t\t# 1. > Add Record    <   #\n";
				  cout << "\t\t\t# 2. > Search Record <   #\n";
				  cout << "\t\t\t# 3. > Update Record <   #\n";
				  cout << "\t\t\t# 4. > Delete Record <   #\n";
				  cout << "\t\t\t# 5. > Display Record<   #\n";
				  cout << "\t\t\t# 0. > Exit          <   #\n";
				  cout << "\t\t\t#########################\n";
				  cout << "\n";
				  cout << "\t\t\t ->Enter a choice<-" << flush;
                  choice = _getch();
            } while ( choice < '0' || choice > '5'); // choice not between 1 and 5, show menu again
 
            system( "cls" );
 
            // to modify, delete or display records, database file should exist, then we have some records           
            if (choice == '2' || choice == '3' || choice == '4')
            {
                  if (!isFileExist(SUPERMARKET_FILE_NAME)) // if database file doesn't exist
                  {
                        cout << "\nDatabase file ('" << SUPERMARKET_FILE_NAME << "') doesn't exist, then there are no records." << endl;
                        system("pause");
                        continue; // show the menu again
                  }
            }
 
            switch ( choice )
            {
            int recs_num; // number of records before the record for modifying(deletion)
            int id;
			

 
            case '1' : //Add Record

				cout << "\n\n\n";
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###                Display records                      ###" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
 
                  cout << "\n\t\t < Entering a new record > ";
				  cout << "\n";
                  cout << "\n   Enter the following informations for the new record : ";
				  cout << "PRODUCT ID:";
				  SUPERMARKET.productid = Auto++;
				  cout << SUPERMARKET.productid << endl;

				  if ( output.open("id.txt"), ios::out)
				  {
					  output << Auto;
				  }
				  else
					  cout << "the File is empty" ;
					  output.close();
					
						cout << "\n" ;
					  cout<<"Please Enter Barcode Number";
                     cout << "\nCode - Only Numbers \n(Ex: 8988): ";
					 getline(cin,SUPERMARKET.barcodenum);
					while ((SUPERMARKET.barcodenum.size()<=3 ) || ( SUPERMARKET.barcodenum.size()>10) || ( SUPERMARKET.barcodenum.find_first_of( "1234567890" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Item Code Again !!" << endl;
								cout << "Please Enter Your Item Code : " ;
								getline (cin,SUPERMARKET.barcodenum);

							}
				

					
						cout << "\n" ;
					cout << "\n Name of Item \n(Ex: Milo): ";
					getline(cin, SUPERMARKET.nameofitem);
					while ((SUPERMARKET.nameofitem.size()<=0 ) || ( SUPERMARKET.nameofitem.size()>80) || ( SUPERMARKET.nameofitem.find_first_of( "qwertyuioplkjhgfdsazxcvbnm" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Name of Item Again !! " << endl;
								cout << "Please Enter Name of Item: " ;
								getline (cin,SUPERMARKET.nameofitem);
					}
					
					cout<<"\n";
					cout << "Enter Amount of Item\n (Ex: 12): ";
					getline (cin,SUPERMARKET.amountofitem);


					while ( SUPERMARKET.amountofitem.find_first_of( "0123456789" ) == string::npos)
							{

								cout << "Invalid ! Please Re-Enter Amount of Item !! " << endl;
								cout << "Please Enter Amount of Item : " ;
								getline (cin, SUPERMARKET.amountofitem);

							}



						cout << "\n" ;
						cout << "\nItem Details \n(Ex: 1 liter): ";
					getline(cin, SUPERMARKET.itemdetails);
					while ( SUPERMARKET.itemdetails.find_first_of( "1234567890qwertyuioplkjhgfdsazxcvbnm" ) == string::npos)
							{

								cout << "Invalid ! Please Re-Enter Your Item Details Again !! " << endl;
								cout << "Please Enter Your Item Details: " ;
								getline (cin,SUPERMARKET.itemdetails);
					}

					cout<<"\n";
					cout << "Enter The Price\n(Ex: RM 50.00)  : RM ";
					getline (cin,SUPERMARKET.price);


					while ( SUPERMARKET.price.find_first_of( "0123456789RM." ) == string::npos)
							{

								cout << "Invalid ! Please Re-Enter Price again !! " << endl;
								cout << "Please Enter a Price : " ;
								getline (cin, SUPERMARKET.price);
							}
					
					
				 
					char date [10];
					_strdate(date);
					SUPERMARKET.date=date;
                  
				  SUPERMARKET.deleted = 0;
 
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::out | ios::app | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file" << endl;
                        system("pause");
                        break;
                  }
                 
                  fs->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
                             
                  closeFile(fs);
 
                  cout << "\nRecord added." << endl;
                  system("pause");
                 
                  break;

				  
			case '2' : //Search Record

				cout << "\n\n\n";
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###                Search Records                       ###" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
 
                  cout << "\n Enter Product ID, that you want to search : ";
                  cin >> id;
				  cin.ignore();
				   
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file" << endl;
                        system("pause");
 
                        break;
                  }

				  recs_num = -1;
 
                  while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                  {
                        recs_num++;
 
                        if ( SUPERMARKET.productid == id && !SUPERMARKET.deleted ) // if user deleted an SUPERMARKET then added another one with the same ID in the same instance of program runs, deleted SUPERMARKET is still there, then we should go through all the file
                              break;
                  }

				  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified SUPERMARKET doesn't exist in file." << endl;
                        closeFile(fs);
                        system("pause");
                        break;
                  }

				  fs->seekp ( sizeof(SUPERMARKET) * recs_num, ios::beg ); // go to the searched record
				  fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ); // display the searched record
                  
                  if ( !SUPERMARKET.deleted )
				  {
					  cout << "\n\n";
					  cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
					  cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
					  cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
					  cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
					  cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
					  cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
					  cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';
					  
				  }
                              
                  cout << "\nTo return to menu, "; system("pause");
 
                  closeFile(fs);
 
                  break;




				  case '3' : // Update Record
 
					  cout << "\n\n\n";
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###                Update Records                       ###" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;

                  cout << "\n Enter Product ID, that you want update its information : ";
                  cin >> id;
				  cin.ignore();
 
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::out | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file" << endl;
                        system("pause");
 
                        break;
                  }
                 
                  recs_num = -1;
 
                  while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                  {
                        recs_num++;
 
                        if ( SUPERMARKET.productid == id && !SUPERMARKET.deleted)
                              break;
                  }
 
                  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified SUPERMARKET doesn't exist in file." << endl;
                        closeFile(fs);
                        system("pause");
                        break;
                  }

				  
				  cout << "\n   Enter new informations for this record : ";
                  
				 cout <<"\n";
						
						cout << "\nBarcode Number       : ";
					 cin >> SUPERMARKET.barcodenum;
					 cout << "\n Name of Item \t: ";
					getline(cin, SUPERMARKET.nameofitem);
					cout << "\n Amount of Item\t: ";
					getline(cin, SUPERMARKET.amountofitem);
					cout << "\n Item details\t:";
					getline(cin, SUPERMARKET.itemdetails);
					cout << "\nPrice \t: RM ";
					getline(cin, SUPERMARKET.price);
					

				  fs->seekp ( sizeof(SUPERMARKET) * recs_num, ios::beg ); // go to the first of the record to be modified
                  fs->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
                 
                  closeFile(fs);
 
                  cout << "\nRecord is modified." << endl;
                  system("pause");
                 
 
                  break;
 
 
            case '4' : //Delete Record
 
                  cout << "\n Enter Product ID, for deletion : ";
                  cin >> id;
				  cin.ignore();
 
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::out | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file." << endl;
                        system("pause");
                        break;
                  }
                 
                  recs_num = -1;
 
                  while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                  {
                        recs_num++;
 
                        if ( SUPERMARKET.productid == id && !SUPERMARKET.deleted ) // if user deleted an SUPERMARKET then added another one with the same ID in the same instance of program runs, deleted SUPERMARKET is still there, then we should go through all the file
                              break;
                  }
 
                  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified Item doesn't exist in database file." << endl;
                        closeFile(fs);
                        system("pause");                   
                        break;
                  }
 
                 SUPERMARKET.deleted = 1;
 
                  fs->seekp ( sizeof(SUPERMARKET) * recs_num, ios::beg );
                  fs->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
                 
                  closeFile(fs);
 
                  deletion = true; // we have some deleted records
 
                  cout << "\nRecord is deleted." << endl;
                  system("pause");
 
                  break;


				  
            case '5' : // Display Records
 
						cout << "\n\n\n";
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###                Display Records                      ###" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;

		fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
		if (!fs)
		{
			cout << "Can not open" <<SUPERMARKET_FILE_NAME<< "file." << endl;
			system("pause");
			break;
		}
		while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
		{
			if ( !SUPERMARKET.deleted )
			{
					  cout << "\n\n\n";
					  cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
					  cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
					  cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
					  cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
					  cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
					  cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
					  cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';
					
			}
		}

		closeFile(fs);
		system ("pause");
		
		
		break;
                 
            case '0' : // Exit
 
                 if (deletion) // if there is any deletion, then update database file (create a new temp file that doesn't have deleted records, then remove the old database file and rename temp file to database file name)
                  {
                        cout << "\nUpdating '" << SUPERMARKET_FILE_NAME << "' File..." << endl;
 
                        fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
                        if (!fs)
                        {
                              cout << "\nCan't open '" << SUPERMARKET_FILE_NAME << "' file, then Updating is incomplete." << endl;
                              system("pause");
 
                              system( "cls" );
                              return 1;
                        }
 
                        fs1 = new fstream( "temp", ios::out | ios::binary);
                        if (!fs1)
                        {
                              cout << "\nCan't create temp file, then Updating is incomplete." << endl;
                              system("pause");
 
                              closeFile(fs);
                              system( "cls" );
                              return 1;
                        }
 
                        // write nondeleted records to the temp file
                        while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                              if ( !SUPERMARKET.deleted )
                                    fs1->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
 
                        closeFile(fs);
                        closeFile(fs1);

						 if( remove( SUPERMARKET_FILE_NAME ) == -1 ) // if there is an error
                        {
                            cout << "\nCan't delete '" <<SUPERMARKET_FILE_NAME << "' file, then Updating is incomplete." << endl;
                              system("pause");
                             
                              system( "cls" );
                              return 1;
                        }

						   struct stat st; // to check size of the temp file
                        int res = stat( "temp", &st );
                       
                        if (st.st_size == 0) // if all of records are deleted then the temp file size is zero                        
                              remove( "temp" ); // we have no records, then no database file is needed, just delete the temp file
                        else
                              if ( rename ("temp", SUPERMARKET_FILE_NAME) )
                              {
                                    cout << "\nCan't rename temp file, then Updating is incomplete." << endl;
                                    system("pause");
                                   
                                    system( "cls" );
                                    return 1;
                              }
 
                        cout << "\nUpdating database file completed." << endl;
                        system("pause");
                  }
 
                  system( "cls" );


				  //Exit Splash Screen
				  cout << "\n\n\n\n\n\n";
				  cout << "                                                                              \n";
				  cout << "           GGGGGG    OOOOO  OOOOO  DDD       BBBB   Y      Y  EEEEE           \n";
				  cout << "           G         O   O  O   O  D   D     B   B   Y    Y   E               \n";
				  cout << "           G         O   O  O   O  D   D     B   B    Y  Y    E               \n";
				  cout << "           G    GGG  O   O  O   O  D   D     BBBB      YY     EEEE            \n";
				  cout << "           G     G   O   O  O   O  D   D     B   B     YY     E               \n";
				  cout << "           G     G   O   O  O   O  D   D     B   B     YY     E               \n";
				  cout << "           GGGGGG    OOOOO  OOOOO  DDD       BBBB      YY     EEEEE           \n";
				  cout << "                                                                              \n";
	
				  cout << " \n\n\n\n\n\n\nThank You!!!\n"; system("pause");

                  return 0;
 
                  break;

            } // end 'switch'
      } // end 'while'
 
      return 0;
} // end 'main()'
 
 
//Function Modules
 void closeFile(fstream *fs)
{
      fs->close(); // close the file
      delete fs;
      fs = NULL;
}
 
bool isFileExist(const char * file_name)
{    
      struct stat st; // to check status of file
      int res = stat( file_name, &st );
      return (res == 0); // if file exists     
}


i want to print it in case 5, in display record,,
help please

This post has been edited by sfontana: 09 April 2011 - 11:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Print The Output Of C++ Program to Printer

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1654
  • View blog
  • Posts: 3,133
  • Joined: 30-May 10

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 01:18 AM

First, I would suggest you add a lot more functions. A 500+ line main() is awful.

For example,
printBanner();
while ( true ) {
  printMenu();
  choice = getChoice();
  switch ( choice ) {
    case 1:
      doAddRecord();
      break;
    case 2:
      doSearchRecord();
      break;
  }
}



As for printing, then try opening one of the DOS device names for the printer
http://en.wikipedia....le#Device_files
say
print = new fstream ( "PRN", ios::out | ios::binary );
Was This Post Helpful? 0
  • +
  • -

#3 sfontana  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 16-March 11

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 01:33 AM

i just want to print following
cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';


where the exactly i need put the function?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1654
  • View blog
  • Posts: 3,133
  • Joined: 30-May 10

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 03:32 AM

Well it would be something like
print << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
Was This Post Helpful? 0
  • +
  • -

#5 sfontana  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 16-March 11

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 08:07 AM

its doesnt work

 case '6' : // Print Records
 
						cout << "\n\n\n";
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###                Print Records                        ###" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;

		fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
		if (!fs)
		{
			cout << "Can not open" <<SUPERMARKET_FILE_NAME<< "file." << endl;
			system("pause");
			break;
		}
		while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
		{
			if ( !SUPERMARKET.deleted )
			{
					  cout << "\n\n\n";
					  cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
					  cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
					  cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
					  cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
					  cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
					  cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
					  cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';
			}

			cout << "Initialising..." << endl; 
  
           ofstream print; 
		  new fstream ( "PRN", ios::out | ios::binary );
          print.open(("USB001"), ios::out); 
  
          cout << "Stream opened, sending text..." << endl; 

					  print << "\n\n\n";
					  print << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n'<<endl;
					  print << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n'<<endl;
					  print << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n'<<endl;
					  print << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n'<<endl;
					  print << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n'<<endl;
					  print << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n'<<endl;
					  print << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n'<<endl;

		  print << "f"; 
  
          print.close();

		  
			}

		

		closeFile(fs);
		system ("pause");
		
		
		break;


where exactly i need to put the more function?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 08:20 AM

I think the first thing you should do is research using the C++ fstream class. There is absolutely no reason to be using new in this line:
fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );.

You may want to look at this page on Basic file IO in C++.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 sfontana  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 16-March 11

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 08:59 AM

it still cant work man..

case '6' : // Print Records
 
						cout << "\n\n\n";
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###                Print Records                        ###" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;

		fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
		if (!fs)
		{
			cout << "Can not open" <<SUPERMARKET_FILE_NAME<< "file." << endl;
			system("pause");
			break;
		}
		while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
		{
			if ( !SUPERMARKET.deleted )
			{
					  cout << "\n\n\n";
					  cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
					  cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
					  cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
					  cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
					  cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
					  cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
					  cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';
			}

			cout << "Initialising..." << endl; 
  
           ofstream print;
		   fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
		   print.open("SUPERMARKET.txt", ios::out);;
          print.open(("USB001"), ios::out); 
  
          cout << "Stream opened, sending text..." << endl; 

					  print << "\n\n\n";
					  print << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n'<<endl;
					  print << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n'<<endl;
					  print << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n'<<endl;
					  print << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n'<<endl;
					  print << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n'<<endl;
					  print << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n'<<endl;
					  print << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n'<<endl;

		  print << "f"; 
  
          print.close();

		  
			}

		

		closeFile(fs);
		system ("pause");
		
		
		break;


is that any mistake what i put?
Was This Post Helpful? -1
  • +
  • -

#8 sfontana  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 16-March 11

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 09:12 AM

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // system()
#include <conio.h> // getch()
#include <sys\stat.h> // stat(status of a file)
#include <iomanip>
#include <ctime>
using namespace std;
 
 
//Data types
struct SUPERMARKET_Record // item record
{  
	int productid;
	 string barcodenum;
	 string nameofitem;
	 string amountofitem;
	 string itemdetails;
	 string date;
	 string price;
	 bool deleted;
};
 
 
//Variable Declarations & Constants
#define SUPERMARKET_FILE_NAME "SUPERMARKET.txt" // name of the database file to store SUPERMARKET informations

SUPERMARKET_Record SUPERMARKET;
int Auto = 1000;
char     choice; // for choice in menu
fstream *fs = NULL, *fs1 = NULL;// file streams for files : fs -> 'SUPERMARKET', fs1 -> 'temp'
bool     deletion = false; // if any record has been deleted

//Functions 
void closeFile(fstream *); // closes a file with its pointer, then deletes the file pointer
bool isFileExist(const char *); // check if a file exists
 
 
//Main
int main()
{
	double total;
	ifstream input;
	input.open("id.txt"),ios::in;
	input >> Auto;
	ofstream output;
	cout << "\n\n\n\n\n\n";
	cout << "           #===============================================================#   \n";
	cout << "           #          H    H  EEEE  L     L     OOOO      !!               #   \n";
	cout << "           #          H    H  E     L     L     O  O      !!               #   \n";
	cout << "           #          H HH H  EEE   L     L     O  O      !!               #   \n";
	cout << "           #          H    H  E     L     L     O  O                       #   \n";
	cout << "           #          H    H  EEEE  LLLL  LLLL  OOOO      @@               #   \n";
	cout << "           #===============================================================#   \n";
	cout << "           #  W           W  EEEE  L     CCCC  OOOO  MM   MM  EEEE         #   \n";
	cout << "           #   W         W   E     L     C     O  O  M M M M  E            #   \n";
	cout << "           #    W   W   W    EEEE  L     C     0  O  M  M  M  EEEE         #   \n";
	cout << "           #     W W W W     E     L     C     0  O  M     M  E            #   \n";
	cout << "           #      W   W      EEEE  LLLL  CCCC  0000  M     M  EEEE         #   \n";
	cout << "           #===============================================================#   \n\n";
	cout << "                        "; system("pause");
	
	while (true)
   
      {
            do
		
            {
                  system( "cls" );
				  cout<<endl;
                  cout << "\t*************************************************************\n" ;    
                  cout << "\t*         .:: Welcome To SuperMarket Database Program::.    *\n";
				  cout << "\t*                    By : SERGIO FONTANA                    *\n";
				  cout << "\t*************************************************************\n"<<endl;
                  cout<<endl<<endl;
				  cout << "\t\t\t#########################\n";
				  cout << "\t\t\t# 1. > Add Record    <  #\n";
				  cout << "\t\t\t# 2. > Search Record <  #\n";
				  cout << "\t\t\t# 3. > Update Record <  #\n";
				  cout << "\t\t\t# 4. > Delete Record <  #\n";
				  cout << "\t\t\t# 5. > Display Record<  #\n";
				  cout << "\t\t\t# 6. > Print Record  <  #\n";
				  cout << "\t\t\t# 0. > Exit          <  #\n";
				  cout << "\t\t\t#########################\n";
				  cout << "\n";
				  cout << "\t\t\t ->Enter a choice<-" << flush;
                  choice = _getch();
            } while ( choice < '0' || choice > '6'); // choice not between 1 and 6, show menu again
 
            system( "cls" );
 
            // to modify, delete or display records, database file should exist, then we have some records           
            if (choice == '2' || choice == '3' || choice == '4')
            {
                  if (!isFileExist(SUPERMARKET_FILE_NAME)) // if database file doesn't exist
                  {
                        cout << "\nDatabase file ('" << SUPERMARKET_FILE_NAME << "') doesn't exist, then there are no records." << endl;
                        system("pause");
                        continue; // show the menu again
                  }
            }
 
            switch ( choice )
            {
            int recs_num; // number of records before the record for modifying(deletion)
            int id;
			

 
            case '1' : //Add Record

				cout << "\n\n\n";
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###                Display records                      ###" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
 
                  cout << "\n\t\t < Entering a new record > ";
				  cout << "\n";
                  cout << "\n   Enter the following informations for the new record : ";
				  cout << "PRODUCT ID:";
				  SUPERMARKET.productid = Auto++;
				  cout << SUPERMARKET.productid << endl;

				  if ( output.open("id.txt"), ios::out)
				  {
					  output << Auto;
				  }
				  else
					  cout << "the File is empty" ;
					  output.close();
					
						cout << "\n" ;
					  cout<<"Please Enter Barcode Number";
                     cout << "\nCode - Only Numbers \n(Ex: 8988): ";
					 getline(cin,SUPERMARKET.barcodenum);
					while ((SUPERMARKET.barcodenum.size()<=3 ) || ( SUPERMARKET.barcodenum.size()>10) || ( SUPERMARKET.barcodenum.find_first_of( "1234567890" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Item Code Again !!" << endl;
								cout << "Please Enter Your Item Code : " ;
								getline (cin,SUPERMARKET.barcodenum);

							}
				

					
						cout << "\n" ;
					cout << "\n Name of Item \n(Ex: Milo): ";
					getline(cin, SUPERMARKET.nameofitem);
					while ((SUPERMARKET.nameofitem.size()<=0 ) || ( SUPERMARKET.nameofitem.size()>80) || ( SUPERMARKET.nameofitem.find_first_of( "qwertyuioplkjhgfdsazxcvbnm" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Name of Item Again !! " << endl;
								cout << "Please Enter Name of Item: " ;
								getline (cin,SUPERMARKET.nameofitem);
					}
					
					cout<<"\n";
					cout << "Enter Amount of Item\n (Ex: 12): ";
					getline (cin,SUPERMARKET.amountofitem);


					while ( SUPERMARKET.amountofitem.find_first_of( "0123456789" ) == string::npos)
							{

								cout << "Invalid ! Please Re-Enter Amount of Item !! " << endl;
								cout << "Please Enter Amount of Item : " ;
								getline (cin, SUPERMARKET.amountofitem);

							}



						cout << "\n" ;
						cout << "\nItem Details \n(Ex: 1 liter): ";
					getline(cin, SUPERMARKET.itemdetails);
					while ( SUPERMARKET.itemdetails.find_first_of( "1234567890qwertyuioplkjhgfdsazxcvbnm" ) == string::npos)
							{

								cout << "Invalid ! Please Re-Enter Your Item Details Again !! " << endl;
								cout << "Please Enter Your Item Details: " ;
								getline (cin,SUPERMARKET.itemdetails);
					}

					cout<<"\n";
					cout << "Enter The Price\n(Ex: RM 50.00)  : RM ";
					getline (cin,SUPERMARKET.price);


					while ( SUPERMARKET.price.find_first_of( "0123456789RM." ) == string::npos)
							{

								cout << "Invalid ! Please Re-Enter Price again !! " << endl;
								cout << "Please Enter a Price : " ;
								getline (cin, SUPERMARKET.price);
							}
					
					
				 
					char date [10];
					_strdate(date);
					SUPERMARKET.date=date;
                  
				  SUPERMARKET.deleted = 0;
 
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::out | ios::app | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file" << endl;
                        system("pause");
                        break;
                  }
                 
                  fs->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
                             
                  closeFile(fs);
 
                  cout << "\nRecord added." << endl;
                  system("pause");
                 
                  break;

				  
			case '2' : //Search Record

				cout << "\n\n\n";
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###                Search Records                       ###" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
 
                  cout << "\n Enter Product ID, that you want to search : ";
                  cin >> id;
				  cin.ignore();
				   
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file" << endl;
                        system("pause");
 
                        break;
                  }

				  recs_num = -1;
 
                  while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                  {
                        recs_num++;
 
                        if ( SUPERMARKET.productid == id && !SUPERMARKET.deleted ) // if user deleted an SUPERMARKET then added another one with the same ID in the same instance of program runs, deleted SUPERMARKET is still there, then we should go through all the file
                              break;
                  }

				  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified SUPERMARKET doesn't exist in file." << endl;
                        closeFile(fs);
                        system("pause");
                        break;
                  }

				  fs->seekp ( sizeof(SUPERMARKET) * recs_num, ios::beg ); // go to the searched record
				  fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ); // display the searched record
                  
                  if ( !SUPERMARKET.deleted )
				  {
					  cout << "\n\n";
					  cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
					  cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
					  cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
					  cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
					  cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
					  cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
					  cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';
					  
				  }
                              
                  cout << "\nTo return to menu, "; system("pause");
 
                  closeFile(fs);
 
                  break;




				  case '3' : // Update Record
 
					  cout << "\n\n\n";
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###                Update Records                       ###" << endl;
				cout << "\t###                                                     ###" << endl; 
				cout << "\t###########################################################" << endl;
				cout << "\t###########################################################" << endl;

                  cout << "\n Enter Product ID, that you want update its information : ";
                  cin >> id;
				  cin.ignore();
 
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::out | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file" << endl;
                        system("pause");
 
                        break;
                  }
                 
                  recs_num = -1;
 
                  while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                  {
                        recs_num++;
 
                        if ( SUPERMARKET.productid == id && !SUPERMARKET.deleted)
                              break;
                  }
 
                  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified SUPERMARKET doesn't exist in file." << endl;
                        closeFile(fs);
                        system("pause");
                        break;
                  }

				  
				  cout << "\n   Enter new informations for this record : ";
                  
				 cout <<"\n";
						
						cout << "\nBarcode Number       : ";
					 cin >> SUPERMARKET.barcodenum;
					 cout << "\n Name of Item \t: ";
					getline(cin, SUPERMARKET.nameofitem);
					cout << "\n Amount of Item\t: ";
					getline(cin, SUPERMARKET.amountofitem);
					cout << "\n Item details\t:";
					getline(cin, SUPERMARKET.itemdetails);
					cout << "\nPrice \t: RM ";
					getline(cin, SUPERMARKET.price);
					

				  fs->seekp ( sizeof(SUPERMARKET) * recs_num, ios::beg ); // go to the first of the record to be modified
                  fs->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
                 
                  closeFile(fs);
 
                  cout << "\nRecord is modified." << endl;
                  system("pause");
                 
 
                  break;
 
 
            case '4' : //Delete Record
 
                  cout << "\n Enter Product ID, for deletion : ";
                  cin >> id;
				  cin.ignore();
 
                  fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::out | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << SUPERMARKET_FILE_NAME << "' file." << endl;
                        system("pause");
                        break;
                  }
                 
                  recs_num = -1;
 
                  while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                  {
                        recs_num++;
 
                        if ( SUPERMARKET.productid == id && !SUPERMARKET.deleted ) // if user deleted an SUPERMARKET then added another one with the same ID in the same instance of program runs, deleted SUPERMARKET is still there, then we should go through all the file
                              break;
                  }
 
                  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified Item doesn't exist in database file." << endl;
                        closeFile(fs);
                        system("pause");                   
                        break;
                  }
 
                 SUPERMARKET.deleted = 1;
 
                  fs->seekp ( sizeof(SUPERMARKET) * recs_num, ios::beg );
                  fs->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
                 
                  closeFile(fs);
 
                  deletion = true; // we have some deleted records
 
                  cout << "\nRecord is deleted." << endl;
                  system("pause");
 
                  break;


				  
            case '5' : // Display Records
 
						cout << "\n\n\n";
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###                Display Records                      ###" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;

		fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
		if (!fs)
		{
			cout << "Can not open" <<SUPERMARKET_FILE_NAME<< "file." << endl;
			system("pause");
			break;
		}
		while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
		{
			if ( !SUPERMARKET.deleted )
			{
					  cout << "\n\n\n";
					  cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
					  cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
					  cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
					  cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
					  cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
					  cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
					  cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';
					
			}

		}

		closeFile(fs);
		system ("pause");
		
		
		break;

case '6' : // Print Records
 
						cout << "\n\n\n";
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###                Print Records                        ###" << endl;
						cout << "\t###                                                     ###" << endl; 
						cout << "\t###########################################################" << endl;
						cout << "\t###########################################################" << endl;

		fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
		if (!fs)
		{
			cout << "Can not open" <<SUPERMARKET_FILE_NAME<< "file." << endl;
			system("pause");
			break;
		}
		while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
		{
			if ( !SUPERMARKET.deleted )
			{
					  cout << "\n\n\n";
					  cout << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n';
					  cout << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n';
					  cout << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n';
					  cout << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n';
					  cout << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n';
					  cout << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n';
					  cout << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n';
			}

			cout << "Initialising..." << endl; 
  
           ofstream print;
		   fs = new fstream ( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
		   print.open("SUPERMARKET.txt", ios::out);;
          print.open(("USB001"), ios::out); 
  
          cout << "Stream opened, sending text..." << endl; 

					  print << "\n\n\n";
					  print << "Product ID\t\t\t: " << SUPERMARKET.productid << '\n'<<endl;
					  print << "Barcode Number\t\t\t: " << SUPERMARKET.barcodenum << '\n'<<endl;
					  print << "Name of Item\t\t\t: " << SUPERMARKET.nameofitem << '\n'<<endl;
					  print << "Amount of Item\t\t\t: " << SUPERMARKET.amountofitem << '\n'<<endl;
					  print << "Item Details\t\t\t: " << SUPERMARKET.itemdetails<< '\n'<<endl;
					  print << "Date\t\t\t\t: " << SUPERMARKET.date<< '\n'<<endl;
					  print << "Price\t\t\t\t: " << "RM "<<SUPERMARKET.price << '\n'<<endl;

		  print << "f"; 
  
          print.close();

		  
			}

		

		closeFile(fs);
		system ("pause");
		
		
		break;
                 
            case '0' : // Exit
 
                 if (deletion) // if there is any deletion, then update database file (create a new temp file that doesn't have deleted records, then remove the old database file and rename temp file to database file name)
                  {
                        cout << "\nUpdating '" << SUPERMARKET_FILE_NAME << "' File..." << endl;
 
                        fs = new fstream( SUPERMARKET_FILE_NAME, ios::in | ios::binary );
                        if (!fs)
                        {
                              cout << "\nCan't open '" << SUPERMARKET_FILE_NAME << "' file, then Updating is incomplete." << endl;
                              system("pause");
 
                              system( "cls" );
                              return 1;
                        }
 
                        fs1 = new fstream( "temp", ios::out | ios::binary);
                        if (!fs1)
                        {
                              cout << "\nCan't create temp file, then Updating is incomplete." << endl;
                              system("pause");
 
                              closeFile(fs);
                              system( "cls" );
                              return 1;
                        }
 
                        // write nondeleted records to the temp file
                        while (fs->read( (char *) &SUPERMARKET, sizeof(SUPERMARKET) ))
                              if ( !SUPERMARKET.deleted )
                                    fs1->write( (char *) &SUPERMARKET, sizeof(SUPERMARKET) );
 
                        closeFile(fs);
                        closeFile(fs1);

						 if( remove( SUPERMARKET_FILE_NAME ) == -1 ) // if there is an error
                        {
                            cout << "\nCan't delete '" <<SUPERMARKET_FILE_NAME << "' file, then Updating is incomplete." << endl;
                              system("pause");
                             
                              system( "cls" );
                              return 1;
                        }


						   struct stat st; // to check size of the temp file
                        int res = stat( "temp", &st );
                       
                        if (st.st_size == 0) // if all of records are deleted then the temp file size is zero                        
                              remove( "temp" ); // we have no records, then no database file is needed, just delete the temp file
                        else
                              if ( rename ("temp", SUPERMARKET_FILE_NAME) )
                              {
                                    cout << "\nCan't rename temp file, then Updating is incomplete." << endl;
                                    system("pause");
                                   
                                    system( "cls" );
                                    return 1;
                              }
 
                        cout << "\nUpdating database file completed." << endl;
                        system("pause");
                  }
 
                  system( "cls" );


				  //Exit Splash Screen
				  cout << "\n\n\n\n\n\n";
				  cout << "                                                                              \n";
				  cout << "           GGGGGG    OOOOO  OOOOO  DDD       BBBB   Y      Y  EEEEE           \n";
				  cout << "           G         O   O  O   O  D   D     B   B   Y    Y   E               \n";
				  cout << "           G         O   O  O   O  D   D     B   B    Y  Y    E               \n";
				  cout << "           G    GGG  O   O  O   O  D   D     BBBB      YY     EEEE            \n";
				  cout << "           G     G   O   O  O   O  D   D     B   B     YY     E               \n";
				  cout << "           G     G   O   O  O   O  D   D     B   B     YY     E               \n";
				  cout << "           GGGGGG    OOOOO  OOOOO  DDD       BBBB      YY     EEEEE           \n";
				  cout << "                                                                              \n";
	
				  cout << " \n\n\n\n\n\n\nThank You!!!\n"; system("pause");

                  return 0;
 
                  break;

            } // end 'switch'
      } // end 'while'
 
      return 0;

} // end 'main()'
 
//Function Modules
 void closeFile(fstream *fs)
{
      fs->close(); // close the file
      delete fs;
      fs = NULL;
}
 
bool isFileExist(const char * file_name)
{    
      struct stat st; // to check status of file
      int res = stat( file_name, &st );
      return (res == 0); // if file exists     
}


Was This Post Helpful? 0
  • +
  • -

#9 Tantalum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-April 11

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 10:17 AM

When it comes to printing, you might want to (assuming you are using Windows) take a look at the Windows Spooling API, Spooling API at MSDN, it describes the functions needed to print to a printer.

For printing plain text you might want to look into sending a RAW print job to the printer, which should just print out plain text.
Was This Post Helpful? 0
  • +
  • -

#10 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1654
  • View blog
  • Posts: 3,133
  • Joined: 30-May 10

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 10:42 AM

It would be a good idea if you wrote a 10-line test program to open the printer, try and print something, then close it.

We don't want/need to wade through 600+ lines of code which has no relevance to the problem at hand.

Now the first thing is, do you suddenly have a mysterious file called "PRN" in your project directory structure?
One thing to try (if you do have such a file), is to try and open "PRN:" (note the trailing colon).

Next, try this at the command prompt
C:\> PRINT TEST1.TXT
where TEST1.TXT is a suitably short text file.
If that doesn't work, then it is unlikely that trying to print to PRN: or LPT1: or any other DOS device is going to work in your program code.

http://ss64.com/nt/net_use.html

Quote

Join a Printer Share
NET USE [LPTx:] \\ComputerName\printer_share /PERSISTENT:YES

Create a "share" for your printer using the control panel, then use this command on the command line to associate the share with say LPT1.
Then try using LPT1: in your program.

The next question is, what sort of printer do you have? I see USB001 mentioned, is it plugged into the USB by any chance (it shouldn't matter if you can use "net use").
The only thing which might be an issue is if you have WinPrinter. I don't know whether you could actually ever print to one of those from anything other than a Win32 GUI program.
Was This Post Helpful? 1
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Print The Output Of C++ Program to Printer

Posted 10 April 2011 - 11:07 AM

Read this, learn it, live it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1