How to terminate the entry by enter a ctrl+z

This is a discussion on How to terminate the entry by enter a ctrl+z within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define MAX 1000 int main() { FILE *outFile; char filename[50]; char message[MAX]; int i; printf("Please ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    How to terminate the entry by enter a ctrl+z

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 1000
    
    int main()
    {
        FILE *outFile;
        char filename[50];
        char message[MAX];
        int i;
        
        printf("Please input a filename: ");
        gets(filename);
        
        outFile = fopen(filename,"r");
        
        if (outFile == NULL)
        {
           outFile = fopen(filename,"w");
           printf("\nThe file %s is created successfully!.",filename);
    
           printf("\n\nYou may start to input your message now!!\n");
           printf("Please enter Control-Z when finished.\n\n");
           
           gets(message);
    
           ****problems here****
    
        }
        else
        {
           printf("\nThe file is currently exist.");
           printf("\nPlease enter another filename.");
        }
        
        fflush(stdin);
        getchar();
        
        return 0;
    }
    The problem is that it should terminates after '^Z' but not '\n'.
    What should I add to make it work?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> gets(filename);

    Not a great idea really.

    What's wrong with the user just pressing enter to finish whatever they're saying? look into fgets I'd say.

    Also fflush( stdin ); is not a very good idea either.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    i want to enter a string within '\n' (a newline)
    so, the program need a ctrl+z to terminate

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well u could use do..while loop to do that.

    ssharish2005

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Quote Originally Posted by ssharish2005
    well u could use do..while loop to do that.

    ssharish2005
    i'm sorry i don't really understand how to do it
    can you elaborate a little bit more, thanks

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by NelsonW
    i'm sorry i don't really understand how to do it
    can you elaborate a little bit more, thanks
    well while could still more better than do- while i guess. Have a look at this

    Code:
    while((ch=getchar()) != EOF && i < MAX)
              message[i++] = ch;
    ssharish2005
    Last edited by ssharish2005; 12-29-2006 at 12:18 PM.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Not that ssharish's solution is non-standard, <conio.h> must be included.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    I'll try by myself, anyway thanks

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by twomers
    Not that ssharish's solution is non-standard, <conio.h> must be included.
    Sorry i dint get you. How is it non standard. Are u pointing out the getchar(). anything else. If it is so getchar is standard fucntion. And ofcourse I missed a "&&" in my last post.

    NOTE: The code has been updated

    ssharish2005

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Sorry man. My bad. I read getch() instead of getchar(). Sorry again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Frustated with mess table printing
    By benedicttobias in forum C Programming
    Replies: 6
    Last Post: 04-16-2009, 06:50 PM
  2. endless loop for scanf - plz help
    By owi_just in forum C Programming
    Replies: 18
    Last Post: 03-20-2005, 01:41 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM
  5. Desperate for help - ugly nested if
    By baseballkitten in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:56 PM

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