I have written the program (see below) which is supposed to take a number from a .txt file but the number may or may not have spaces in between the numbers. I am trying to remove the spaces and then make the number so that I can later perform calculations on it.
E.g. if the input is: 1 . 456 7 7
then the output should be: 1.45677
I am having problems with the "remove" section of my program (lines 32 to 37). Can anyone help?
Code:
#include <stdio.h>
main()
// open the file
{
FILE*fp;
int letter;
if((fp=fopen("quote.txt","r"))==NULL)
{
puts("Cannot open the file");
}
//declarations
int i;
i=0;
char a[20];
int j;
char b[20];
int k=0;
//read each char from file
while((letter=fgetc(fp)) !=EOF)
{
printf("%c",letter);
a[i] = letter;
i++;
}
printf("\n");
//remove spaces
for (j=0; j<20; j++) {
if (a[j] == ' ') {b[k] = a[j]; k++;}
printf("%c", b[k]);
}
//output the result in form ready to do mathematical manipulations
printf("\n");
printf("%s", b);
fclose(fp);
return 0;
}