Can anyone check this code

This is a discussion on Can anyone check this code within the C Programming forums, part of the General Programming Boards category; Question: write a program to store every character typed at the keyboard into a file. the procedure should come to ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    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,230
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    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, 08:35 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 02:27 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 02:26 PM
  5. Check my code Please
    By AdioKIP in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 08:52 PM

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