Thread: line reading problem

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    line reading problem

    am posting this code again.i changed this code to read a file and write out the first line, and all non blanks lines preceded by a blank line.
    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
    }
    the programs recognises the blank lines but it prints out only <null>

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > the programs recognises the blank lines but it prints out only <null>
    Yeah, so?

    > printf("%s\n",code[n+1]);
    But you haven't read in code[n+1] yet, so trying to print it will fail.

    Tip:
    Read the whole file in using the while loop I showed you earlier.

    Then use a for ( i = 0 ; i < n ; i++ )
    to scan the array, and output lines according to your rules.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    13
    thanks again Salem. sometimes am realy stupid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading input
    By gp364481 in forum C Programming
    Replies: 3
    Last Post: 09-24-2008, 05:10 PM
  2. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  3. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  4. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  5. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM