Help me please

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2003
Posts: 5
Reputation: BMF is an unknown quantity at this point 
Solved Threads: 0
BMF BMF is offline Offline
Newbie Poster

Help me please

 
0
  #1
May 20th, 2003
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] );
}
}
}

}
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster
 
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2003
Posts: 5
Reputation: BMF is an unknown quantity at this point 
Solved Threads: 0
BMF BMF is offline Offline
Newbie Poster
 
0
  #3
May 22nd, 2003
Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks alot...

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

THANX
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC