Thread: How do I write to a file without hitting enter? (windows)

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    9

    How do I write to a file without hitting enter? (windows)

    This is basically my attempt to write to a file in real time without hitting the enter key. However there is an error occuring. This is written in visual studio 2015.

    The functionality of this program is that one can enter any keystroke and that keystroke will be sent to hello.txt updating it immediately. And when EOF is pressed, then the program ends.

    In debug mode the error seems to to stop at fprintf(fp, (const char * const)ch); <--- I casted the int type ch to (const char * const)

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <errno.h>
    #include <conio.h>
    
    
    int main()
    {
    	int ch = 0;
    	FILE *fp;
    	fp = fopen("hello.txt", "w");
    	if (fp != NULL)
    	{
    		while (ch != '-1')
    		{
    			ch = _getch();
    			fprintf(fp, (const char * const)ch);
    		}
    		fclose(fp);
    	}
    	else
    	{
    		printf("Error opening file\n");
    	}
    	return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Perhaps use the EOF macro instead of the incorrect '-1'

    Also you may have to hit enter anyway, because input like
    xyz^Z
    tends to not work.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fprintf(fp, (const char * const)ch);
    The first thing to do is remove all the casts so you get a meaningful error message.

    Pretending a single character is a pointer to a string is just going to screw you up.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    9
    You are right about EOF. How about pressing any character other than EOF.

    I made a similar program to make you get an idea what im trying to do.

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <conio.h>
    #define lim 1000
    int main(void)
    {
        int ch, i = 0;
        char str[lim];
        printf("Type 'Y' when finished typing keys: ");
        do
        {
            ch = _getch();
            str[i++] = ch;
        } while (ch != 'y' && i < lim);
        str[i] = '\0';
        printf("%s",str);
        return 0;
    }
    Now im still figuring out how to do the file handling.
    Last edited by BinaryProgamer; 09-26-2016 at 04:31 AM.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    9
    The error is that once i typed anything to the console, a error message displays in visual studio
    "Application has stopped working"

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I'd be willing to bet that if you run in debug mode, the failure will be when i reaches 1000. You're indexing the array before checking the validity of the index. A loop along these lines would work better:
    Code:
    while ((ch = _getch()) != 'y' && i < lim - 1)
    {
        str[i++] = (char)ch;
    }
    
    str[i] = '\0';
    This allows sufficient room for the typed characters, plus the terminating '\0', and excludes your stop character from the string. Personally, I'd favor using EOF instead of a potentially valid character to stop input, because at present you cannot write 'y' to your file in any meaningful way.

    This snippet isn't very robust, but I didn't want to complicate things for you just yet.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    9
    Quote Originally Posted by Prelude View Post
    I'd be willing to bet that if you run in debug mode, the failure will be when i reaches 1000. You're indexing the array before checking the validity of the index. A loop along these lines would work better:
    Code:
    while ((ch = _getch()) != 'y' && i < lim - 1)
    {
        str[i++] = (char)ch;
    }
    
    str[i] = '\0';
    This allows sufficient room for the typed characters, plus the terminating '\0', and excludes your stop character from the string. Personally, I'd favor using EOF instead of a potentially valid character to stop input, because at present you cannot write 'y' to your file in any meaningful way.

    This snippet isn't very robust, but I didn't want to complicate things for you just yet.
    The code I want checked is this (see first post)
    Code:
    int ch;
    ...
     while (ch != 'y'){
        ch = _getch();
        fprintf(fp, ch);
    }
    replace the str array to textfile

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fprintf(fp, ch);
    Still trying to pretend ch is a printf format string.
    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.

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    9
    Quote Originally Posted by Salem View Post
    > fprintf(fp, ch);
    Still trying to pretend ch is a printf format string.
    Code:
    fprintf(fp,"%c", ch);
    thanks, the code is now fixed

    tbh I haven't got an idea what fprintf does before posting this thread.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It should be easy to figure out when you realise

    printf("Hello %d\n", 123 );

    is exactly the same as

    fprintf(stdout,"Hello %d\n", 123 );

    fprintf shouldn't be a problem if you've already got to grips with printf.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does my program only continue on hitting a key + Enter
    By kiwifreak3 in forum C Programming
    Replies: 3
    Last Post: 05-12-2012, 10:20 AM
  2. Close console without hitting enter
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2009, 08:32 AM
  3. reading input w/o hitting enter?
    By Aalmaron in forum C Programming
    Replies: 1
    Last Post: 01-31-2006, 10:38 PM
  4. Help with hitting 'enter'
    By K-Zodron in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2005, 08:20 AM
  5. Hitting enter while FOCUS on an EDIT
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 09-02-2003, 10:26 AM

Tags for this Thread