Thread: Programming a Tiny More Binary

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    6

    Programming a Tiny More Binary

    Hello,

    I have an issue with scanf. I would like to press enter like "more" does.

    Sorry if my programme is really for beginners and very basic.

    Would you know about possibilities here to solve this?

    Code:
    
    // to compile: tcc nmore.c -o nmore
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include <sys/ioctl.h>
    #include <unistd.h>
    //printf ("lines %d\n", w.ws_row);
    //printf ("columns %d\n", w.ws_col);
    
    
    
    
    
    
    int main()
    {
    
    
            char str[500];
    
    
            struct winsize w;
            ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
            double linemax= (double)(w.ws_row);
            double linepos = 0;
            double charpos=0 ; 
    
    
            int begin = 1;
            int c ; 
            c = getchar();
            while( c != EOF )
            {
               if ( begin == 1 ) 
               {
                   if ( linepos >= linemax )  
                   {
                         linepos = 0;
                         printf( "New Line\n" );
                         scanf("%[^\t\n]s",str);    // <-- this is not working
                         // press key
                   }
    
    
                   printf( "%.0f: ", charpos++);
                   linepos++;
               }
               putchar( c );
               begin = 0;
               if ( c == '\n' ) begin = 1;
               c = getchar();
            }
    	return 0;
     }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The FAQ has some thoughts on reading a keypress without having to wait for return.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    6
    Quote Originally Posted by spartrekus View Post
    Hello,

    I have an issue with scanf. I would like to press enter like "more" does.

    Sorry if my programme is really for beginners and very basic.

    Would you know about possibilities here to solve this?

    Code:
    
    // to compile: tcc nmore.c -o nmore
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include <sys/ioctl.h>
    #include <unistd.h>
    //printf ("lines %d\n", w.ws_row);
    //printf ("columns %d\n", w.ws_col);
    
    
    
    
    
    
    int main()
    {
    
    
            char str[500];
    
    
            struct winsize w;
            ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
            double linemax= (double)(w.ws_row);
            double linepos = 0;
            double charpos=0 ; 
    
    
            int begin = 1;
            int c ; 
            c = getchar();
            while( c != EOF )
            {
               if ( begin == 1 ) 
               {
                   if ( linepos >= linemax )  
                   {
                         linepos = 0;
                         printf( "New Line\n" );
                         scanf("%[^\t\n]s",str);    // <-- this is not working
                         // press key
                   }
    
    
                   printf( "%.0f: ", charpos++);
                   linepos++;
               }
               putchar( c );
               begin = 0;
               if ( c == '\n' ) begin = 1;
               c = getchar();
            }
        return 0;
     }
    The FAQ is huge.


    Could you be specific on how to exit the input?

    look, it is readily possible. It can be proved:
    echo test | less
    echo test | more


    HELP

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by spartrekus View Post
    The FAQ is huge.


    Could you be specific on how to exit the input?

    look, it is readily possible. It can be proved:
    echo test | less
    echo test | more


    HELP
    Maybe,
    look at what your checking:
    Code:
     double linemax= (double)(w.ws_row);double linepos = 0;
    ....
    if (linepos >= linemax )
    I don't fully understand it, but, reading more of your code even though it looks like it will meet that requirement in your if statement.

    should it not check for an action instead?
    if key press do something
    mock code
    Code:
    while ( wait)
    if ( action  == desired action / condition )
    do it
    else
    wait for it
    something like that perhaps?
    or
    Code:
    while ( doing something && desired action / condition not reached)
    {
       just keep on doing it, even if the doing something is just waiting. 
    }
    or
    Code:
    while ( doing something || until desired action / condition is reached)
    {
    just keep on doing it, even if the doing something is just waiting.
    else
    break;  on condition reached.
    }
    that last one look funny, don't trust it. until tried.
    plus you're using a Pipe so you got to factor that in too. yes?
    Last edited by userxbw; 09-23-2017 at 09:10 AM.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    6
    Quote Originally Posted by userxbw View Post
    Maybe,
    look at what your checking:
    Code:
     double linemax= (double)(w.ws_row);double linepos = 0;
    ....
    if (linepos >= linemax )
    I don't fully understand it, but, reading more of your code even though it looks like it will meet that requirement in your if statement.

    should it not check for an action instead?
    if key press do something
    mock code
    Code:
    while ( wait)
    if ( action  == desired action / condition )
    do it
    else
    wait for it
    something like that perhaps?
    or
    Code:
    while ( doing something && desired action / condition not reached)
    {
       just keep on doing it, even if the doing something is just waiting. 
    }
    or
    Code:
    while ( doing something || until desired action / condition is reached)
    {
    just keep on doing it, even if the doing something is just waiting.
    else
    break;  on condition reached.
    }
    that last one look funny, don't trust it. until tried.
    plus you're using a Pipe so you got to factor that in too. yes?
    I am not so sure that you understood what does "more" or this "tiny more" applications. Probably you are using Windows, and haven't Unix background.

    The pipe is use, but of course, keypress and pipe aren't compatible, as stated above.

    Is there anyone having a possible idea or workaround?

    thank you in advance !!

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Simply put, in Unix systems you need to switch the buffering mode, get your input and then switch it back. This FAQ page provides a possible solution at the bottom.
    FAQ > How do I get my program to wait for a keypress? - Cprogramming.com
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    6
    Quote Originally Posted by GReaper View Post
    Simply put, in Unix systems you need to switch the buffering mode, get your input and then switch it back. This FAQ page provides a possible solution at the bottom.
    FAQ > How do I get my program to wait for a keypress? - Cprogramming.com
    i tried in this way, but without success, according to above.

    here it seems, that there is no single way to interrupt stdin for scanf ...
    c - How to interrupt standard input stream for another - Stack Overflow

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    6
    Quote Originally Posted by spartrekus View Post
    i tried in this way, but without success, according to above.

    here it seems, that there is no single way to interrupt stdin for scanf ...
    c - How to interrupt standard input stream for another - Stack Overflow

    Tada !!!

    Ok, I got it!


    I have here made a project for it.
    nmore/nmore.c at master * spartrekus/nmore * GitHub

    Howto:
    Code:
    gcc nmore.c -o nmore ;  cat nmore.c | ./nmore
    The little problem is that the |NMORE| line is still displayed. How to delete it ???
    (idea of getting the whole buffer, and delete last line, and bring that again, but it is far too complex).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One tiny error.
    By KidFlash in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2010, 07:44 PM
  2. not able to understand this tiny tiny method
    By noobcpp in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2008, 10:42 AM
  3. Need help with a tiny program
    By Rahiiyja in forum C++ Programming
    Replies: 22
    Last Post: 07-05-2007, 02:15 PM
  4. Well I tried but i have a tiny problem
    By dapernia in forum C Programming
    Replies: 0
    Last Post: 09-03-2003, 01:20 PM
  5. Just a tiny little problem...
    By Nutshell in forum C Programming
    Replies: 26
    Last Post: 02-08-2003, 04:54 AM

Tags for this Thread