Mmmmmm
Code:
#include<stdio.h>
#include<stdlib.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;
FILE *stream;
int i, n = 0; // Moved C++ declaration
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++ ) {
printf( "%02d %s", i, code[i] );
}
return 0; // 0 is success
}