Hello. Currently my code is supposed to be copying the 21st character or the last character if it's less than 21 onto a new file. I need to ignore spaces, and it's not. In addition, I need it to place all these character copied on one line. Which I can't figure out what to do. Here's my code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void){
FILE* sp1;
FILE* sp2;
int curCh; // variable 1
int preCh; // variable 2
int countCh = 0; // variable 3;
int whiteSpace = 0; // var4
int closeStatus;
printf("File copy has started\n");
if (!(sp1 = fopen ("FILE1.DAT", "r"))){
printf("Error opening FILES1.DAT for reading");
return (1);
}//if open
if (!(sp2 = fopen ("FILES2.DAT", "w"))){
printf("Error opening FILES2 for writing");
return (2);
}
while ((curCh = fgetc(sp1)) != EOF){
countCh++;
if ( curCh == ' ')
whiteSpace = 0;
if(curCh == '\n' ){
if (countCh < 21 && whiteSpace != 0)
fputc (preCh, sp2);
fputc (' ', sp2);
countCh = 0;
}
whiteSpace = 1;
if ( countCh == 21 && whiteSpace != 0){
fputc(curCh, sp2);
fputc (' ', sp2);
}
preCh = curCh;
}
fputc ('\n', sp2);
fclose(sp1);
closeStatus = fclose(sp2);
if (closeStatus == EOF)
{
printf("File close error.\a\n");
return 201;
} // if close error
printf("File successfully created\n");
system ("pause");
return 0;
}]