-
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 ();
}
-
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 :confused:.
-
Anyone knows how can I put '\0' at the end of my Text variable?
-
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);
}
}