File I/o

This is a discussion on File I/o within the C++ Programming forums, part of the General Programming Boards category; Dear Friends, I have written the code below in an effort of laerning File I/O and port programming. I am ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    File I/o

    Dear Friends,

    I have written the code below in an effort of laerning File I/O and port programming. I am coding using the VC++ 6.0 compiler. The code runs well. Pliz someone help me modify the code so that the program output is captured and saved to a file. The program is to prompt a user for a filename.

    My code is:

    Code:
    /* program to read and write to and from the printer port.*/
    /* the three port registers are used*/
    
    #include <iostream>
    #include <string>
    #include <time.h>
    #include <conio.h>
    
    #define DATA	 0x378  /*Data Register base address*/
    #define STATUS   DATA+1 /* Status Register address*/
    #define CONTROL  DATA+2  /*Control Register address*/
    
    
    using namespace std;
    
    int main() 
    {
    
    //prepare for processing text
    	bool MoreLinesToProcess=true;
    	
    		while (MoreLinesToProcess){
    		//process next line
    		bool MoreCharactersOnCurrentLine=true;
    
    		cout<<"please type a line of text:"<<endl;
    
    		while (MoreCharactersOnCurrentLine){
    
    		/*process next character on current line*/
    			char s,n;  //CurrentCharacter;
    			if (cin.get(s)){
    		/*process current character on current line*/
    				if(s=='\n')
    		/*found newline character that ends line*/
    					MoreCharactersOnCurrentLine=false;
    				else  {
    					
    _outp(CONTROL,_inp(CONTROL)&0xF0|0x04); /*initialise 				the CONTROL port for data input*/
    
    _outp(DATA, s);   /*write c to the DATA register*/
    
    	clock_t start_time;
    					start_time=clock();
    while ((clock()-start_time)<0.25*CLK_TCK);/*propagation 					delay time*/
    
    	n=(_inp(STATUS)&0xF0);  /* read MSnibble*/
    
    	n=n|(_inp(CONTROL)&0x0F);/*Read LSnibble*/
    
    					n=n^0x84; /*toggle bit 2 and 7 */STROBE pin is     hardware inverted.*/
    					
    					
    	cout<<n;
    	}
                }
             }
    
           }
          cout<<"I am doing fine";
    
    return 0;
    }
    Last edited by Salem; 03-19-2005 at 12:55 AM. Reason: unbolden the code tags

  2. #2
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    Code:
     cout.sync_with_stdio(true); //Sync cout with the C io functions
    freopen("file.txt", "w", stdout); //stdin for cin
    Put that before you use cout.
    You'll need <cstdio> (maybe <cstdlib>)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21