I just started learning C 3 days ago, and I am trying to make a program that takes in a file, reads the characters, and deletes all spaces and tabs from it. I am a beginning programmer who has only learned a little bit of MATLAB prior to starting to learn C. I am using Ubuntu 12.04.
Here is my code:
#include <stdio.h>
int main ()
{
FILE * pFile;
FILE * pFile2;
int c;
pFile = fopen ("spaces.txt","r");
pFile2 = fopen ("nospaces.txt","w");
if (pFile==NULL) perror ("Error opening file");
else
{
while (c != EOF)
{
c = fgetc (pFile);
if (!(c == ' ' || c == ' '))
{
fputc (c, pFile2);
}
}
fclose (pFile2);
fclose (pFile);
}
return 0;
}
When I open the new file "nospaces.txt" everything is fine except there is weird character at the end. Gedit says it is /FF or /00 with a red background and complains that I shouldn't edit the file because I could corrupt it. No matter what I tried to do I cannot get rid of that strange character at the end. Here are examples of things I have tried:
-Adding EOF, '\0', and other random characters to the end of pFile2 using fputc (EOF , pFile2) -Putting random constraints on the value of c so that it doesn't pick characters that aren't letters or numbers (like 40 < c < 125) -Making it a do while instead of a while loop (I don't know the difference)
Please help. Thank you.
while (c != EOF){ c = fgetc (pFile);-->while ((c = fgetc (pFile)) != EOF){