So I have figured out the reading and writing! However, do I need to close both files? When I run my code as shown below, the message "Error closing file comes up", but if I take out the close1 part, then I don't get that message. Should I close the file I'm reading from or is that unnecessary? If I do need to close it, where/how should I do it?
Code:
//preprocessor
#include <stdio.h>
#include <stdlib.h>
//prototypes (function announcement)
//function main
int main ()
{
// declarations and initialzations
FILE * frData;
FILE* fwClean;
char c;
int close1;
int close2;
//prompts and scans
frData=fopen("lane9.txt", "r");
if (!frData)
{
printf("could not open file\a\n");
exit (101);
}
fwClean=fopen("CleanLane9.txt", "w");
if (!(fwClean=fopen("CleanLane9.txt", "w")))
{
printf("Error opening CleanLane9.txt for writing");
return(1);
}
// calculation
do
{
c=getc(frData);
if (isdigit(c))
{
printf("%c", c); // shows what values are being read and will be written.
fputc(c,fwClean);
}
if (isspace(c))
{
printf("%c",c); //shows what values are being read and will be written.
fputc(c,fwClean);
}
}
while (c!=EOF);
printf("\nAbove are the values in the new file\n");
// closing
close1=fclose(frData);
if(close1=EOF)
{
printf("Error closing file\a\n");
return 100;
}
close2=fclose(fwClean);
if(close2==EOF)
{
printf("Error closing file\a\n");
return 100;
}
return 0;
}