| | |
Help me please
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2003
Posts: 5
Reputation:
Solved Threads: 0
Hello>>>
How are you all?
I need a quick help with my assignment... it is in C not in C++... So, Could you help me?
I don't know what to do!!
the question is:
Write a program that reads a data file containing a C program and writes it to another file removing all comments.
the rubbish answer that i made
:
#include <stdio.h>
#include <string.h>
void main ()
{
FILE *ptr;
FILE *ptr2;
char *com = { "*/" };
char str[500], str2[500];
if( ( ptr = fopen( "program.dat", "w" ) ) == NULL )
printf( "\n Failed to open the file" );
else
{
printf( "\nEnter a C program:" );
fprintf( ptr, "%s", str );
}
strcpy( str2, str );
if( ( ptr = fopen( "program.dat", "r" ) ) == NULL )
printf( "\n Failed to open the file" );
else
{
while( !feof( ptr ) )
{
for( int i=0; str[i]!= NULL; i++ )
if( ( ptr2 = fopen( "newfile.dat", "w+" ) ) == NULL )
printf( "\n Failed to open the file" );
else
{
if( str[i]=='/' && str[i+1]=='*' )
{
com = strstr( str, com );
while( com != NULL )
{
com = strstr( com+1, str );
}
}
else
fprintf( ptr2, "%c", str2[i] );
}
}
}
}
How are you all?
I need a quick help with my assignment... it is in C not in C++... So, Could you help me?
I don't know what to do!!
the question is:
Write a program that reads a data file containing a C program and writes it to another file removing all comments.
the rubbish answer that i made
:#include <stdio.h>
#include <string.h>
void main ()
{
FILE *ptr;
FILE *ptr2;
char *com = { "*/" };
char str[500], str2[500];
if( ( ptr = fopen( "program.dat", "w" ) ) == NULL )
printf( "\n Failed to open the file" );
else
{
printf( "\nEnter a C program:" );
fprintf( ptr, "%s", str );
}
strcpy( str2, str );
if( ( ptr = fopen( "program.dat", "r" ) ) == NULL )
printf( "\n Failed to open the file" );
else
{
while( !feof( ptr ) )
{
for( int i=0; str[i]!= NULL; i++ )
if( ( ptr2 = fopen( "newfile.dat", "w+" ) ) == NULL )
printf( "\n Failed to open the file" );
else
{
if( str[i]=='/' && str[i+1]=='*' )
{
com = strstr( str, com );
while( com != NULL )
{
com = strstr( com+1, str );
}
}
else
fprintf( ptr2, "%c", str2[i] );
}
}
}
}
0
#2 May 21st, 2003
I found this problem of yours harder than expected. Firstly, I'm better at C++ libraries than the C libraries. So I had to revise all my C File I/O. Secondly, I hate parsing. Anyway here's my solution to your problem.
My technique isn't perfect.
It removes all C-style comments. (/* */) but if it encounters the comments in a string (which is rare in real practical life), it will ignore anything between them in the string.
Plus, I have little experience in parsing text files, so the way I did it may seem messy.
I have posted the code below. The code in HTML format can be viewed here if you prefer syntax highlighting.
My technique isn't perfect.
It removes all C-style comments. (/* */) but if it encounters the comments in a string (which is rare in real practical life), it will ignore anything between them in the string.
Plus, I have little experience in parsing text files, so the way I did it may seem messy.
I have posted the code below. The code in HTML format can be viewed here if you prefer syntax highlighting.
C++ Syntax (Toggle Plain Text)
/* +-----------------------------------------------------------------+ | Author: Lim Chong Liang, Andrew | | Filename: comment.cpp | | Description: This program takes a C source file, and copies the | | text into a new .txt file, with all the comments | | removed. The program only removes C-style | | comments and by default copies itself. | | Copyright: Lim Chong Liang, Andrew, 2003 | | Website: http://www.abdn.ac.uk/~u02cll2/ | | Email: demandred90@hotmail.com OR u02cll2@abdn.ac.uk | +-----------------------------------------------------------------+ */ #include <stdio.h> #include <stdlib.h> void copyFileWithoutComments(char[], char[]) ; /* * The main function. */ int main(int argc, char *argv[]) { char fileToRead[] = "comment.c" ; char fileToWrite[] = "comment.txt" ; copyFileWithoutComments(fileToRead,fileToWrite) ; printf("'%s' has been copied to '%s', with all C-style comments removed.\n", fileToRead, fileToWrite) ; system("PAUSE"); return 0; } /* * Copies a C source file to a new text file, with all C-style comments removed. * fileToRead - The name of the C source file. * fileToWrite - The name of the new text file. */ void copyFileWithoutComments(char fileToRead[], char fileToWrite[]) { FILE * fRead ; FILE * fWrite ; /* buffer for a line */ char lineBuffer[256] ; /* open file for reading */ fRead = fopen(fileToRead,"r"); /* stop reading if file pointer is NULL */ if (fRead == NULL) { printf("Could not open file for reading.") ; return ; } /* open file for writing */ fWrite = fopen(fileToWrite,"w") ; if (fWrite == NULL) { printf("Could not create file for writing.") ; return ; } /* holds character read */ char next ; /* to check if currently in a comment */ int inComment = 0 ; /* keep reading until end of file */ while ( (next = fgetc(fRead)) != EOF ) { /* We are NOT within a comment. */ if ( !inComment ) { if ( next == '/' ) { char temp = fgetc(fRead) ; if ( temp == '*' ) inComment = 1 ; else { fputc(next,fWrite) ; fputc(temp,fWrite) ; } } else fputc(next,fWrite) ; } /* We are in a comment. */ else { if ( next == '*' ) { char temp = fgetc(fRead) ; if ( temp == '/' ) inComment = 0 ; } } } /* close files */ fclose(fWrite) ; fclose(fRead) ; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Visual C++
- Next Thread: amateur coders needed , project in audio streaming
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





