comparing chars with pointer to char
the code below is suppose to read from a file and to print out all alphanumeric characters(a-z,A-Z,0-0) spaces and newlines, throwing away the rest.
anybody can suggest something better or help solve it?
the code below is suppose to read from a file and to print out all alphanumeric characters(a-z,A-Z,0-0) spaces and newlines, throwing away the rest.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char * STRNG;
STRNG code[80];
STRNG get_line(FILE * inf) {
STRNG s = NULL;
int count = 0;
int tok; // must be int - EOF is an int, not a char
while ( (tok=getc(inf)) != EOF && tok != '\n' ) {
if ( s == NULL ) s = malloc( 80 );
s[count++] = tok;
}
if ( tok != EOF ) {
if ( s == NULL ) s = malloc( 80 ); // possibly a newline on its own
s[count++] = tok; // append the \n
s[count] = '\0';
}
return s;
}
int main(int argc, char *argv[]) {
char *filename;
char *loc;
FILE *stream;
int i,j, n = 0; // Moved C++ declaration
char alpha[64]={'q','w','e','r','t','y','u','i','o','p'
,'a','s','d','f','g','h','j','k','l','m','n',
'b','v','c','x','z','Q','W','E','R','T','Y','U','
I','O','P','L','K','J','H','G','F','D','S','A','Z'
,
'X','C','V','B','N','M','1','2','3','4','5','6','
7','8','9','0',' ','\n'};
filename =argv[1];
stream=fopen(filename,"r");
if(stream ==NULL)
{
printf("Enter a valid parameter.Program will exit.\n");
exit(1);
}
while ( (code[n]=get_line(stream)) != NULL ) {
n++;
}
printf( "%d lines found\n", n );
for ( i = 0 ; i < n ; i++ )
{
for(j=0;j<64;j++)
{
loc=strstr(code[i],alpha[j]);
if (loc!=NULL){
printf("%s",alpha[j]);
}
}
}
return 0; // 0 is success
}
anybody can suggest something better or help solve it?