943,458 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5140
  • C++ RSS
May 20th, 2003
0

Help me please

Expand Post »
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] );
}
}
}

}
BMF
Reputation Points: 11
Solved Threads: 0
Newbie Poster
BMF is offline Offline
5 posts
since Mar 2003
May 21st, 2003
0
Re: Help me please
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.

C++ Syntax (Toggle Plain Text)
  1. /*
  2. +-----------------------------------------------------------------+
  3. | Author: Lim Chong Liang, Andrew |
  4. | Filename: comment.cpp |
  5. | Description: This program takes a C source file, and copies the |
  6. | text into a new .txt file, with all the comments |
  7. | removed. The program only removes C-style |
  8. | comments and by default copies itself. |
  9. | Copyright: Lim Chong Liang, Andrew, 2003 |
  10. | Website: http://www.abdn.ac.uk/~u02cll2/ |
  11. | Email: demandred90@hotmail.com OR u02cll2@abdn.ac.uk |
  12. +-----------------------------------------------------------------+
  13. */
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. void copyFileWithoutComments(char[], char[]) ;
  20.  
  21. /*
  22.  * The main function.
  23.  */
  24. int main(int argc, char *argv[])
  25. {
  26. char fileToRead[] = "comment.c" ;
  27. char fileToWrite[] = "comment.txt" ;
  28. copyFileWithoutComments(fileToRead,fileToWrite) ;
  29. printf("'%s' has been copied to '%s', with all C-style comments removed.\n",
  30. fileToRead, fileToWrite) ;
  31. system("PAUSE");
  32. return 0;
  33. }
  34.  
  35. /*
  36.  * Copies a C source file to a new text file, with all C-style comments removed.
  37.  * fileToRead - The name of the C source file.
  38.  * fileToWrite - The name of the new text file.
  39.  */
  40. void copyFileWithoutComments(char fileToRead[], char fileToWrite[])
  41. {
  42. FILE * fRead ;
  43. FILE * fWrite ;
  44.  
  45. /* buffer for a line */
  46. char lineBuffer[256] ;
  47.  
  48. /* open file for reading */
  49. fRead = fopen(fileToRead,"r");
  50.  
  51. /* stop reading if file pointer is NULL */
  52. if (fRead == NULL)
  53. {
  54. printf("Could not open file for reading.") ;
  55. return ;
  56. }
  57.  
  58. /* open file for writing */
  59. fWrite = fopen(fileToWrite,"w") ;
  60. if (fWrite == NULL)
  61. {
  62. printf("Could not create file for writing.") ;
  63. return ;
  64. }
  65.  
  66. /* holds character read */
  67. char next ;
  68.  
  69. /* to check if currently in a comment */
  70. int inComment = 0 ;
  71.  
  72. /* keep reading until end of file */
  73. while ( (next = fgetc(fRead)) != EOF )
  74. {
  75. /* We are NOT within a comment. */
  76. if ( !inComment )
  77. {
  78. if ( next == '/' ) {
  79. char temp = fgetc(fRead) ;
  80. if ( temp == '*' )
  81. inComment = 1 ;
  82. else {
  83. fputc(next,fWrite) ;
  84. fputc(temp,fWrite) ;
  85. }
  86. }
  87. else
  88. fputc(next,fWrite) ;
  89. }
  90. /* We are in a comment. */
  91. else
  92. {
  93. if ( next == '*' ) {
  94. char temp = fgetc(fRead) ;
  95. if ( temp == '/' )
  96. inComment = 0 ;
  97. }
  98. }
  99. }
  100.  
  101. /* close files */
  102. fclose(fWrite) ;
  103. fclose(fRead) ;
  104. }
Reputation Points: 13
Solved Threads: 0
Light Poster
Dante Shamest is offline Offline
46 posts
since Apr 2003
May 22nd, 2003
0
Re: Help me please
Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks alot...

I don't know how can I reward you?????

THANX
BMF
Reputation Points: 11
Solved Threads: 0
Newbie Poster
BMF is offline Offline
5 posts
since Mar 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++
Next Thread in C++ Forum Timeline: amateur coders needed , project in audio streaming





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC