Thread: Can anyone check this code

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    Question Can anyone check this code

    Question:

    write a program to store every character typed at the keyboard into a file. the procedure should come to an end as soon as character ~ is hit from the keyboard.


    I've coded and it is working fine but the only problem I'm facing is that I'm unable to check for the character ~ and terminate the program.



    here's the coding which I've done



    /*program to make the user enter the text from the keyboard and write them on to a file*/
    #include<stdio.h>
    #include<stdlib.h>

    main()
    {
    FILE *fp;
    char ch;

    fp=fopen("d:\\mannu\\try2.txt","w");

    if(fp==NULL)
    {
    puts("\nCannot open file");
    exit(1);
    }

    printf("\nEnter the characters ");
    while((ch=getc(stdin))!='\n')
    {

    if(ch=='~')
    {
    fclose(fp);
    exit(1);
    }
    else
    {
    fputc(ch,fp);
    }
    }


    fclose(fp);
    }


    thanx in advance......
    and also I'm glad the way I'm getting the response & help from u all......and hope the same in future.

    feeling myself lucky to be around such helpful persons

    bye

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while((ch=getc(stdin))!='\n')
    How about
    while((ch=getc(stdin))!='~')
    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
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    when you read this you'll smack yourself upside the head, and if you don't, just ask me what was wrong...

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(void) 
    { 
    	FILE *fp; 
    	char ch; 
    
    	fp=fopen("c:\\try.txt","w"); 
    
    	if(fp==NULL) 
    	{ 
    		puts("\nCannot open file"); 
    		return 1; 
    	} 
    
    	printf("\nEnter the characters "); 
    	while((ch=getc(stdin))!='~') 
    		 fputc(ch,fp); 
    
    	fclose(fp); 
    	return 0; 
    
    }

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    damnet salem, you just had to get there before me! you're jsut a showoff...

    seriously, you always seem to get in there as I'm replying...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > seriously, you always seem to get in there as I'm replying...
    Because I don't waste so much time farting about on the general discussion board.
    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.

  6. #6
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    well if you wanna get TECHNICAL about it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Check
    By Dae in forum C++ Programming
    Replies: 12
    Last Post: 01-08-2009, 07:01 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  5. Check my code Please
    By AdioKIP in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 08:52 PM