this is supossed to read a file and write out the first line, and all non blanks lines preceded by a blank line.
it recognises the blang lines but all that it prints is <null>
please help
it recognises the blang lines but all that it prints is <null>
Code:
/*This program reads a file given as command line argument
and writes out the first lines and all(non blank) lines
preceded by a blank line.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char * STRNG;
STRNG code[80];
STRNG get_line(FILE * inf) {/*Start of the function which reads one line*/
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,c, n = 0;
filename =argv[1];
stream=fopen(filename,"r");
if(stream ==NULL)
{
printf("Enter a valid parameter.Program will exit.\n");
exit(1);
}
code[n]=get_line(stream);
printf("%s",code[n]);
while ( (code[n]=get_line(stream))!= NULL)
{
if(strlen(code[n])==1)/*here is the problem*/
{
printf("%s\n",code[n+1]);
}
}
return 0; // 0 is success
}