Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

line reading problem

Status
Not open for further replies.

papous

MIS
Feb 2, 2003
3
US
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>
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,&quot;r&quot;);
    if(stream ==NULL)
    {
        printf(&quot;Enter a valid parameter.Program will exit.\n&quot;);
        exit(1);
    }
    code[n]=get_line(stream);
	printf(&quot;%s&quot;,code[n]);
    while ( (code[n]=get_line(stream))!= NULL)
	    {

                      if(strlen(code[n])==1)/*here is the problem*/
                        {
	         printf(&quot;%s\n&quot;,code[n+1]);
                        }

    }




    return 0;  // 0 is success
}
please help

 
i found the mistake so dont reply.thanks anyway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top