Thread: Why is this code snippet refusing to work ?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Why is this code snippet refusing to work ?

    Code:
    #include <stdio.h>
    
    main()
    {
    	FILE *fp;
    
    	fp=fopen("Welcome.txt","r+");
    
    	char c;
    
    	while((c=fgetc(fp))!=EOF)
    	{
    		if((c>='a')&&(c<='z'))
    			c -= 32;
    		fputc(c,stdout);
    	}
    
    	fclose(fp);
    }
    When I compile this code,it works perfectly, but when I execute it the following error message is displayed. Can anyone explain why ?
    Code:
    Exception 0xc0000005 Segment Violation Address: 0x73da15ce

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    FAQ > Explanations of... > Definition of EOF and how to use it effectively

    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's highly likely that Welcome.txt doesn't exist.

    Code:
    		if((c>='a')&&(c<='z'))
    			c -= 32;
    Are you trying to convert letters to uppercase here? If so, look into toupper() in <ctype.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    2
    Actually, I created the file Welcome.txt with the text :

    Code:
    hello
    I can't understand the error message ( see the first topic ).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fp=fopen("Welcome.txt","r+");
    Try
    Code:
    fp=fopen("Welcome.txt","r+");
    if ( fp == NULL ) {
      printf( "Could not open file\n" );
      perror( "Error is" );
    }
    > char c;
    Should be
    int c;

    It should also be before the fopen() call, since C doesn't have mixed declarations and statements.
    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. A funny code snippet of "sizeof()"
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2008, 01:09 AM
  2. code wont work
    By lilhawk2892 in forum C++ Programming
    Replies: 9
    Last Post: 07-03-2006, 11:42 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM