Thread: line reading problem

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

    Unhappy line reading problem

    i am trying to wtite a small program which reads a file and writes out the nmber of lines in that file.i wrote a function which reads one line which works fine.my problem is how to make this program to read big files while i dont know the number of lines.please help

  2. #2
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    try this:

    Code:
    #define MAX 150
    #define file "cool.txt"
    FILE *fp;
    char line[MAX]={0};
    int num=0;
    
    fp = fopen(file, "rb");
    while (fgets(line, MAX, fp)!=NULL)
     num++;
    fclose(fp);
    printf("%s has %d number of lines.", file, num);
    Last edited by Devil Panther; 11-20-2001 at 03:20 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Hi, I'm a newbie, but I use this:

    Code:
    int count_lines ( ) {
    
       char buff[BUFSIZ];
       int contador = 0;
       char  file_name[PATH_MAX];
       FILE *input;
    
       strcpy (file_name, material);
       input = fopen(file_name, "r");
       while ( fgets( buff, BUFSIZ, input ) != NULL ) {
             contador++;
       }
       fclose( input );
       return (contador);
    
    }

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

    still not working

    here is the code,but is still not working fine:

    #include<stdio.h>
    #include<stdlib.h>

    typedef char * STRNG;
    STRNG code[80];
    STRNG get_line(FILE * inf)
    {
    STRNG s;
    char tok = getc(inf);
    int count = 0;
    s = malloc(80);
    while(tok!='\n')
    {
    s[count++] = tok;
    tok = getc(inf);
    }
    s[count] = '\0';
    return s;
    }

    int main(int argc, char *argv[])
    {
    char* filename;
    FILE *stream;
    filename =argv[1];
    int n=0;

    stream=fopen(filename,"r");
    if(stream ==NULL)
    { printf("Enter a valid parameter.Program will exit.\n");
    exit(1);
    }

    code[n]=get_line(stream);
    while(code[n]!=NULL){
    n++;
    }
    printf("%d\n",n);
    return 1;
    }

    this program reading the file below is suppose to read 11 lines:

    line1
    line2

    line4: follows a blank line
    line5
    line6: This contains the words cat and dog.

    line8: follows a blank line

    line10: This also contains cat, dog, and rabbit.
    line11: One more containing the word cat.

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

    still not working

    here is the code,but is still not working fine:

    #include<stdio.h>
    #include<stdlib.h>

    typedef char * STRNG;
    STRNG code[80];
    STRNG get_line(FILE * inf)
    {
    STRNG s;
    char tok = getc(inf);
    int count = 0;
    s = malloc(80);
    while(tok!='\n')
    {
    s[count++] = tok;
    tok = getc(inf);
    }
    s[count] = '\0';
    return s;
    }

    int main(int argc, char *argv[])
    {
    char* filename;
    FILE *stream;
    filename =argv[1];
    int n=0;

    stream=fopen(filename,"r");
    if(stream ==NULL)
    { printf("Enter a valid parameter.Program will exit.\n");
    exit(1);
    }

    code[n]=get_line(stream);
    while(code[n]!=NULL){
    n++;
    }
    printf("%d\n",n);
    return 1;
    }

    this program reading the file below is suppose to read 11 lines:

    line1
    line2

    line4: follows a blank line
    line5
    line6: This contains the words cat and dog.

    line8: follows a blank line

    line10: This also contains cat, dog, and rabbit.
    line11: One more containing the word cat.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    13
    should i better deal with EOF instead of looking for NULL?

  7. #7
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    I would try something like

    while (!feof(stream))
    {
    /* Do stuff */
    }

    The feof macro is designed to detect EOF on a stream, very useful..
    Last edited by foniks munkee; 11-20-2001 at 10:31 PM.

  8. #8
    Unregistered
    Guest

    thanks foniks

    thanks foniks
    the program almost works fine exept that it wont print out the final value of i.is like the printf doesnt exist.can somebody tell me why?
    Code:
    int main(int argc, char *argv[])
    {
    FILE *stream;
    stream=fopen(argv[1],"r");
    if(stream ==NULL){
        printf("Enter a valid parameter.Program will exit.\n");
        exit(1);
        }
    
    while(!feof(stream)){
    	code[n]=get_line(stream);
    	printf("%s\n",code[n]);
    	i++;
    
        }
    
    printf("%d\n",i);/*Here is the problem*/
    
    return 1;
    }
    thanks in advance

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it breaks with a segmentation fault here.

    The main problem is that feof() isn't all that it seems to be.

    In order for feof() to return true, first char tok = getc(inf); must return EOF.
    But as soon as it does that, it will always return EOF - which is a problem for your while loop, which only exits when it sees a \n.

    The upshot of this, is that this code loops at EOF until count reaches a sufficiently large number to cause a problem.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    The fgets returns NULL and not EOF when it reaches to the end of the file, no more lines to read!

  11. #11
    Unregistered
    Guest

    replay

    the program is supposed to read empty lines as well.i changed the code into this:
    Code:
    int main(int argc, char *argv[])
    {
    
        int n=0;
    	int c;
    FILE *stream;
    stream=fopen(argv[1],"r");
    if(stream ==NULL){
        printf("Enter a valid parameter.Program will exit.\n");
        exit(1);
        }
    
    do{
    	code[n]=get_line(stream);
    	printf("%s\n",code[n]);
    
    
        }while(c=fgetc(stream)!=EOF);
    
    printf("anything");
    fclose(stream);
    return 0;
    }
    now the program terminates but it cant read the empty lines and the starting character frome some lines is missing too.i guess the problem is in the while loop which is in the get_line function.i am confused and i cant think of something else.
    i guess is a challenge coze two of my tutors couldnt resolve the problem and they claim to be c experts.please help

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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
    }

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    13
    thanks Salem.that was real good.

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