Like Tree1Likes
  • 1 Post By smokeyangel

how to stop reading char???

This is a discussion on how to stop reading char??? within the C Programming forums, part of the General Programming Boards category; Hello everybody i m using visual studio 2012.....in below code i m writing data in to a test.txt file but ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    14

    Unhappy how to stop reading char???

    Hello everybody i m using visual studio 2012.....in below code i m writing data in to a test.txt file but i dont know with which key file stop accepting char...i tried ctrl+z and ctrl+d but not working plzz help meee....
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    main()
    {
    	char ch;
    	FILE *ptr;
    	ptr=fopen("test.txt","w");
    	printf("enter data for file\n");
    	while((ch=getchar())!=EOF)
    	{
    		fputc(ch,ptr);
    	}
    	fclose(ptr);
    	getch();
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Under windows the keystroke is CTRL-Z, repeated twice.

    Preferably, given your code, after a end of line (carriage return) is entered.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    Quote Originally Posted by grumpy View Post
    Under windows the keystroke is CTRL-Z, repeated twice.

    Preferably, given your code, after a end of line (carriage return) is entered.
    not working dude...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Ah - it also depends on the implementation of getchar(). Try CTRL-Z followed by carriage return. (depending on implementation of getchar(), keystrokes - even CTRL-Z which is the EOF sentinel - will be buffered until a carriage return is encountered).
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    I think it's because you're using a char to store the result of getchar(). getchar() returns an int. EOF is -1, which can't be represented by a char.

    Try changing ch to int.

    I'm pretty sure CTRL-Z is the right key sequence -- do it a few times then hit enter and it should die.
    jwroblewski44 likes this.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    I think the problem is because you're using EOF to check a character, try
    Code:
    while((ch=getchar())!='0')
    instead of
    Code:
    while((ch=getchar())!=EOF)
    I think you'll find that the program exits after you enter 0 and press another key(because of the getch)

    Hope I was helpful!!
    Last edited by Dakshin; 02-23-2013 at 07:13 AM.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    thanks everyone now its working....it was not working because of getch() function it was needed two enter for exit...

  8. #8
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    Code:
    #include <stdio.h>
    
    main()
    {
        int ch;   //char to int
        FILE *ptr;
        ptr = fopen( "test.txt" , "w" );
        printf( "enter data for file\n" );
        while((ch=getchar())!=EOF)
        {
            fputc(ch,ptr);
        }
        fclose(ptr);
    }
    

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stop reading until newline in file pointer
    By ulti-killer in forum C Programming
    Replies: 4
    Last Post: 11-09-2012, 12:52 PM
  2. How to stop reading input...
    By intull in forum C Programming
    Replies: 8
    Last Post: 02-20-2011, 02:38 AM
  3. fputs doesn' t stop reading when \0 is found?
    By brack in forum C Programming
    Replies: 5
    Last Post: 09-03-2010, 10:43 AM
  4. Replies: 2
    Last Post: 10-18-2009, 01:05 PM
  5. Stop reading from a file
    By Zalbik in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2002, 04:58 PM

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