low level program in C

This is a discussion on low level program in C within the C Programming forums, part of the General Programming Boards category; Hi , I have to write a low level program in C to simulate the TYPE command in dos. I ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    low level program in C

    Hi ,
    I have to write a low level program in C to simulate the TYPE command in dos.

    I got half way through, but i am not able to print the contents of the file. also i tried to incorporate the feature that if the file contents are too big to be printed on one screen. the user is prompted to press any key to continue and the rest is printed.


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<fcntl.h>
    #include<string.h>
    void main(char *argv[])
    {
    	int inhandle, bytes,x;
    	char buffer[512],ch;
    	int i,j;
    	inhandle = open(argv[1],O_RDONLY|O_BINARY);
    
    		while(1)
    		{
    			bytes=read(inhandle,buffer,512);
    
    			if(bytes>0)
    			{
    
    				j=strlen(buffer);
    				write(stdout,buffer,bytes);
    			                for(i=0;i<j;i++)
    				{
    			    	x=wherex();
    			    	if(x==23)
    					{
    				    	printf(" END OF PAGE : PRESS ENTER ");
    				    	getch();
        					clrscr();
    					}
    				   else
    				             {
    			
                                                                                 }
    				}
    			}
    			else
    			       break;
    		}
    	close (inhandle);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > void main(char *argv[])
    Wrong in 2 places

    int main(int argc, char *argv[])

    I've no idea what you're opening, but is sure isn't the filename you typed in.

    > inhandle = open(argv[1],O_RDONLY|O_BINARY);
    Check for success/failure

    > bytes=read(inhandle,buffer,512);
    Unlike higher level functions like fgets(), this does not add a \0 to the end of the buffer (see your use of strlen() later on)
    Ditto on the error checking

    > j=strlen(buffer);
    j = bytes;
    would be better
    Which kinda makes j redundant
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    Hi,

    Thanks a lot for the rep. i was able to get the output.

    But i still have two problems

    1. how can i use write to print the result. ( for now i am using the for loop)

    2. how to incorporate the end of page thing.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    The basic problem is that you need to count newlines in the buffer you read in.

    How else are you supposed to know that you've printed just 24 lines.

    This kinda means you can't just blast out a whole block of characters and hope it all fits nicely on screen.

    Standard fopen/fgets seems to be better, at least you know where all the newlines are then
    Code:
    while ( fgets( buff, sizeof buff, fp ) != NULL ) {
        fputs( buff, stdout );
        if ( ++count == 25 ) {
            count = 0;
            /* pause */
        }
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by orthanc
    Hi,

    Thanks a lot for the rep. i was able to get the output.

    But i still have two problems

    1. how can i use write to print the result. ( for now i am using the for loop)

    2. how to incorporate the end of page thing.
    1) As Salem mentioned, switch to fopen() and fgets() instead of open/read

    Then for your output use fprintf() specifying stdout for the screen. Depending on your compiler, stdprn may be defined for the printer. If not, fopen() the printer for output.

    2) The trouble with adding the end-of-page thing is figuring out how big a page is. 24 lines used to be normal but I for one have used only 43, 50, or larger for years. depending on your OS and compiler, there may be functions you can call to get the window/screen size.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. low level I/O
    By Saya in forum C Programming
    Replies: 6
    Last Post: 01-21-2004, 02:06 PM

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