program not working

This is a discussion on program not working within the C Programming forums, part of the General Programming Boards category; this is a program to remove comments can anyone help me or give some hints Code: //Program to print a ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Question program not working

    this is a program to remove comments

    can anyone help me or give some hints

    Code:
    //Program to print a file by removing Comments
    #include<stdio.h>
    void main()
    {
            FILE *fp;
            char ch;
           //clrscr();
           fp=fopen("temp.txt","r");
           while(1)
           {
                      ch=fgetc(fp);
    
                         if(ch==EOF)
                          break;
                          else
                          {
                                        if(ch=='/'){
                                       ch=fgetc(fp);
                                        if(ch=='/') 
                                       {
                                              while(1)
                                              {
                                                           ch=fgetc(fp);
                                                           if(ch=='\n')
                                                          goto label;
                                              }
                                      }
                                        if(ch=='*')
                                        { 
                                                  while(1)
                                                  {
                                                            ch=fgetc(fp);
                                                            if(ch=='*')
                                                            { 
                                                                     ch=fgetc(fp);
                                                                      if(ch=='/')
                                                                      {
                                                                                  while(1)
                                                                                  {
                                                                                            ch=fgetc(fp);
                                                                                            goto label;
                                                                                   }
                                                                       }
                                                                       else printf("*");
                                                              }
                                                      }
                                                }
                                                else printf("/");
                                       }
                                  }
                                   label:printf("&#37;c",ch);
                        }
                          fclose(fp);
    }
    Last edited by rohit83.ken; 10-20-2007 at 07:21 AM. Reason: not indented

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    You need a proper code indendation. It is very difficult to read a code which i not intended which mean you wont get much help.

    And why do you want to make things much complicated by reading each char from the file, why dont read a line of text and check the first two chars. IF it is '//' then just skip that line and read the next file.

    ssharish

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Please don't cross-post.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 11:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM

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