problem with fread

This is a discussion on problem with fread within the C Programming forums, part of the General Programming Boards category; Hi! I'm working on a program that will remove all comments from a .c file. I have problems with fread ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question problem with fread

    Hi!

    I'm working on a program that will remove all comments from a .c file. I have problems with fread function. I read that fread function is not good for reading text files and I guess this is true beacuse when I read and then copy characters into other file I always get some extra characters like this "" at the end of the file. How can I avoid that? And there is also a problem when I try to free a text variable.

    Please take a look at the code.

    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    
    
    void ClearScreen (void);
    void Wait (void);
    void RemoveComments (char *InFile, char *OutFile);
    long FileLength (FILE *File);
    
    int main ()
    {
    	char *InFile = NULL, *OutFile = NULL;
    
    
    
    	ClearScreen ();
    	if ((InFile = (char *) malloc (sizeof (char) * 255)) != NULL)
    	{
    		if ((OutFile = (char *) malloc (sizeof (char) * 255)) != NULL)
    		{
    			printf ("REMOVING COMMENTS FROM A FILE");
    			printf ("\n\nEnter file with comments: ");
    			scanf ("%s", InFile);
    			printf ("Enter output file: ");
    			scanf ("%s", OutFile);
    			RemoveComments (InFile, OutFile);
    			printf ("\n\nPRess any key to exit to the system ...");
    			Wait ();
    			// free it
    			free (OutFile);
    			OutFile = NULL;
    		}
    		else
    		{
    			printf ("\n\nError with \"OutFile\".");
    			Wait ();
    			exit (1);
    		}
    		// free memory
    		free (InFile);
    		InFile = NULL;
    	}
    	else
    	{
    		printf ("\n\nError with \"InFile\".");
    		Wait ();
    		exit (1);
    	}
    	return 0;
    }
    void RemoveComments (char *InFile, char *OutFile)
    {
    	FILE *InputFile = NULL, *OutputFile = NULL;
    	char *Text = NULL;
    	long Length = 0;
            short Loop = 1;
    
    	
    	
    	if ((InputFile = fopen (InFile, "rt")) != NULL)
    	{
    		Length = FileLength (InputFile);printf ("Length = %ld", Length);
    		if ((Text = (char *) malloc (sizeof (char) * Length)) != NULL)
    		{
    			if ((OutputFile = fopen (OutFile, "wt+")) != NULL)
    			{
    				fread (Text, Length, 1, InputFile); //->>>> strange characters occur
    				while (*Text)
    				{
    					if ( (*Text == '/') && (*(Text+1) == '/') )
    					{
    						while (*(Text+1) != '\n')
    						{
    						Text++;
    						}
    					}
    				else if ( (*Text == '/') && (*(Text+1) == '*') )
    					{
    						while (Loop)
    						{
    							if ((*Text == '*') && (*(Text+1) == '/')) Loop = 0;
    							Text++;
    						}
    					} else { fprintf (OutputFile, "%c", *Text); Loop = 1; }
    					Text++;
    				}
    				fclose (OutputFile);
    			}
    			else
    			{
    				printf ("\n\nError opening file \"%s\".", OutFile);
    				Wait ();
    				exit (1);
    			}
                        //  free (Text); ->>>>> why it is not working??
    			Text = NULL;
    		}
    		else
    		{
    			printf ("\n\nError with \"Text\".");
    			Wait ();
    			exit (1);
    		}
    		fclose (InputFile);
    	}
    	else
    	{
    		printf ("\n\nError opening file \"%s\".", InFile);
    		Wait ();
    		exit (1);
    	}
    }
    long FileLength (FILE *File)
    {
    	long Length = 0;
    
    
    	fseek (File, 0, SEEK_END);
    	Length = ftell (File);
    	rewind (File);
    	return Length;
    }
    void ClearScreen ()
    {
    	system ("cls");
    }
    void Wait ()
    {
    	getch ();
    }
    Last edited by GaPe; 02-22-2003 at 08:05 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Hm. I don't know how to use fgets function so that I'll be able to read the whole file.

    I tried fscanf function but then my code for excluding comments does not work. Why?

    I tried to put the '\0' at the end of the Text variable but it's not working.

    With that debug I get nowhere.

    Help me .
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Anyone knows how can I put '\0' at the end of my Text variable?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Ok, thanks Salem. Now everything works except that free function. I was checking the values and now the free function is in that place where the pointers are the same (same numbers), but I get the "unhandled exception" error. Where should I put the free function?
    And one more thing, there is a "Text = NULL;". This isn't enough for freeing the memory, right? If I don't use free then there will be memory leaks, right?
    Code:
    void RemoveComments (char *InFile, char *OutFile)
    {
    	FILE *InputFile = NULL, *OutputFile = NULL;
    	char *Text = NULL;
    	long Length = 0;
    	size_t BytesRead = 0;
    	short Loop = 1;
    
    	
    	
    	if ((InputFile = fopen (InFile, "rt+")) != NULL)
    	{
    		Length = FileLength (InputFile);
    		if ((Text = (char *) malloc (sizeof (char) * (Length + 1))) != NULL)
    		{
    			printf ("\nAt malloc = %p", Text);
    			if ((OutputFile = fopen (OutFile, "wt+")) != NULL)
    			{
    				BytesRead = fread (Text, sizeof (char), Length, InputFile);
    				Text[BytesRead] = '\0';
    				while (*Text)
    				{
    					if ( (*Text == '/') && (*(Text+1) == '/') )
    					{
    						while (*(Text+1) != '\n')
    						{
    							
    							Text++;
    						}
    					}
    					else if ( (*Text == '/') && (*(Text+1) == '*') )
    					{
    						while (Loop)
    						{
    							if ((*Text == '*') && (*(Text+1) == '/')) Loop = 0;
    							
    							Text++;
    						}
    					} else { fprintf (OutputFile, "%c", *Text); Loop = 1; }
    					printf ("\nAt free = %p", Text);getch ();
    					free (Text);
    					Text++;
    				}
    				fclose (OutputFile);
    			}
    			else
    			{
    				printf ("\n\nError opening \"%s\".", OutFile);
    				Wait ();
    				exit (1);
    			}
    			// free memory
    			Text = NULL;
    		}
    		else
    		{
    			printf ("\n\nError with \"Text\".");
    			Wait ();
    			exit (1);
    		}
    		fclose (InputFile);
    	}
    	else
    	{
    		printf ("\n\nError opening \"%s\".", InFile);
    		Wait ();
    		exit (1);
    	}
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problem
    By coolsmarty in forum C Programming
    Replies: 4
    Last Post: 01-13-2007, 01:43 PM
  2. Fread type problem
    By conor20_ie in forum C++ Programming
    Replies: 9
    Last Post: 12-15-2006, 04:24 AM
  3. return value of fread()
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-24-2006, 06:31 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. model load problem
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 01-05-2006, 05:33 PM

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