How can I get input to stop at 'Esc' instead of \n ?

This is a discussion on How can I get input to stop at 'Esc' instead of \n ? within the C Programming forums, part of the General Programming Boards category; I'm using Code::Blocks 10.05 in Win XP SP3. How can I get input to stop at 'Esc' instead of \n ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    How can I get input to stop at 'Esc' instead of \n ?

    I'm using Code::Blocks 10.05 in Win XP SP3.

    How can I get input to stop at 'Esc' instead of \n ?
    This is the line of my code I'm having trouble with:
    Code:
        fgets(entry, sizeof(entry), stdin);
    I'd like to know how to input text with line breaks and then exit input when 'Esc' is pressed e.g.:
    Code:
    Things I did this morning\n
    \n
    Things I did this afternoon\n
    \n
    Things I did this evening
    I've tried something along the lines of
    Code:
    while input != ESC
        userInput
    I've looked through Google, rewording it several times.
    I've looked through my "The C Programming Language" book.
    I've looked through the FAQ and forums here but so far found nothing.

    Here's the line in context if it helps (ps. the code below runs with no errors):

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        time_t current_time;
        struct tm * time_info;
        FILE *fp;
    
        char timeString[16], fileName[20], entry[1000], *p;  // space for "YYYY.mm.dd HHMM\0"
    
        time(&current_time);
        time_info = localtime(&current_time);
    
        strftime(timeString, sizeof(timeString), "%Y.%m.%d %H%M", time_info);   //define value of timeString
        strftime(fileName, sizeof(fileName), "%Y.%m.%d.txt", time_info);   // define value of fileName
    
        printf("Diary entry for %s\n",timeString);   // print variable timeString
        printf("Entry: ");
        fgets(entry, sizeof(entry), stdin);
            
        fp=fopen(fileName, "a");    // filename = date&time
        fprintf(fp, "%s\n\n%s\n\n\n", timeString, entry);
        int fclose(FILE *);
        return(0);
    
    }

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Read about the curses library.

    You need to use it (or a similiar one) and read the keyboard (I mean stdin) data character-by-character, building the strings step-by-step.
    I don't think there's a ready made library that allows you to read the keyboard (I mean stdin) and stop at specific characters; but with curses it is somewaht easy to do it yourself.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If I understand your question, this is not possible without using OS-specific functions. On Windows, you might do this:
    Code:
    #include <stdio.h>
    #include <conio.h>  // _getch()
    
    int main() {
        char entry[100];
        while (1) {
            int ch;
            printf("Entry: ");
            ch = _getch(); // read char without echoing and without waiting for newline
            if (ch == 27)  // Escape key
                break;
            entry[0] = ch;
            putchar(ch);
            fgets(entry + 1, sizeof entry - 1, stdin);
            printf("%s", entry);
        }
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Quote Originally Posted by oogabooga View Post
    If I understand your question, this is not possible without using OS-specific functions. On Windows, you might do this:
    Code:
    #include <stdio.h>
    #include <conio.h>  // _getch()
    Really? ALL compilers on Windows have conio.h and getch()??? I thought only 1% of the compilers had them! Whoo'da thunk it?

    Please before stating erroneous information, check it first. He even mentions he uses Code::Blocks -- does it's compiler have getch()?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Quote Originally Posted by WaltP View Post
    Really? ALL compilers on Windows have conio.h and getch()??? I thought only 1% of the compilers had them! Whoo'da thunk it?

    Please before stating erroneous information, check it first. He even mentions he uses Code::Blocks -- does it's compiler have getch()?
    Relax, yes most still do have these files and functions for backward compatibility. BUT they're obsolete and everyone should be discouraged from using them.
    Devoted my life to programming...

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,829
    ALL compilers on Windows have conio.h and getch()?
    O_o

    How did you get that from his post?

    Soma

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably he saw the "conio.h" header and went batty. That's understandable since we seem to have a big focus on portability on the forum and people love to hate conio. But gcc does have it, so I used it. I was actually trying to show the simplest "solution" to his problem. The code I posted has some serious drawbacks, however. It will only detect Escape if it's the first key pressed on that line, and after you enter a different char and it goes on to the fgets, you won't be able to backspace over the initial character! Still, it might fit his purpose. To fix those "bugs" you'd have to read all chars with _getch() (or some similar function from curses) and handle backspacing yourself.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    >> they're obsolete and everyone should be discouraged from using them.
    I wouldn't say that. MS has supported that function from the beginning. Borland copied it.
    https://en.wikipedia.org/wiki/Conio.h
    https://en.wikipedia.org/wiki/Lattice_C

    Since MinGW uses the MS CRT, it has it too. Slightly easier than using the Win32 API.

    gg

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,304
    Quote Originally Posted by WaltP View Post
    Really? ALL compilers on Windows have conio.h and getch()??? I thought only 1% of the compilers had them! Whoo'da thunk it?

    Please before stating erroneous information, check it first. He even mentions he uses Code::Blocks -- does it's compiler have getch()?
    Yes.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thanks for the replies. For now I've got:
    Code:
    fp=fopen(fileName, "a");    // filename = date&time
        puts(timeString);   // output variable timeString
        puts("Press Ctrl + C to save and exit");
        printf("Entry: ");
        fprintf(fp, "\n%s\n\n", timeString);
        while (fgets(entry, sizeof(entry), stdin) != NULL)
        {
            if ((p = strchr(entry, '\n')) != NULL)
            *p = '\0';
            fprintf(fp, "%s\n\n", entry);
        }
        int fclose(FILE *);
    This seems to do the trick for now. This way, my text file gets only one time&date stamp per entry not per paragraph. It exits on Ctrl+C which isn't ideal but it does the trick.

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    You could also have the user type an EOF when they are done. This is done by typing Ctrl-Z and Enter on Windows, or just Ctrl-D on basically every other OS (Unix, MacOS X, Linux, etc). This makes fgets return NULL which will make your loop end.

    Also you're not closing the file after the loop ends. You're not actually calling fclose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] Force stop for input
    By Arbyn Acosta in forum C Programming
    Replies: 19
    Last Post: 09-17-2012, 11:30 PM
  2. How to stop reading input...
    By intull in forum C Programming
    Replies: 8
    Last Post: 02-20-2011, 02:38 AM
  3. Scanf sometimes do not stop for input
    By kapil1089thekin in forum C Programming
    Replies: 1
    Last Post: 10-03-2010, 07:25 AM
  4. Stop from bypassing string input?
    By Djanvk in forum C++ Programming
    Replies: 2
    Last Post: 08-21-2008, 03:32 AM
  5. stop input being echoed
    By Kinasz in forum C Programming
    Replies: 11
    Last Post: 07-25-2004, 11:02 AM

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