There are possibly countless ways to get this done. But in the end you need to test each read value for char or int properties.
Try this:
We declare the necessary variables:
const char * filename = "input.txt";
FILE * fp = fopen(filename,"r");
int c;
Why declare c as an int? So we can distinguish between the end of input value and other chars.
We can use a simple read function like getchar which returns one character from the standard input(usually the stuff you type in your terminal or command prompt window). For our purposes we will use the getc function. Which is more or less the same as getchar in that it reads one char, but instead it takes one argument. A File pointer. Now we can read from a file like this:
c = getc(fp);
The pseudo code i gave in the earlier post was:
while( there is a char that is not EOF)
if ( isdigit(char_read) ) print(char_read)
What the while loop will do is read a char then test the char for the EOF value. Once it is not this value we continue.
while ( (c = getc(fp)) != EOF)
Variable c now contains a character read from the input file. Now test for int property, if satisfied we print the character:
if ( isdigit(c) != 0 ) putchar(c);
putchar is used to print one char to standard input.
Details of getchar, putchar, getc , etc, can be found
here
How to differentiate between alpha chars, int chars, other chars? Easy. Use functions from the
ctype.h library. isdigit is what we will need for the job at hand. isdigit will return zero if it finds a value other than a decimal and any other value for a decimal. So we basically just need to test for a value other than zero.
So lets put all this together.
const char * filename = "input.txt";
FILE * fp = fopen(filename,"r");
int c;
if(fp == NULL) {
printf("Error opening %s\n",filename);
exit(1);
}
while ( (c = getc(fp)) != EOF)
{
if ( isdigit(c) != 0 ) putchar(c);
}
fclose(fp);
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused: