Thread: input the number of the line you want to print - Text File

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    input the number of the line you want to print - Text File

    The code is working nice for me.


    But feel free to give me advice.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 100
    
    
    int main(int argc, char** argv) {
    
    
        FILE * pFile;
    
    
        char buffer[SIZE];
    
    
        if ((pFile = fopen("code.txt", "rt")) == NULL) {
            return -1;
        }
    
    
        int nrline_to_printf = 0;
        int nrline_on_the_file = 0;
    
    
        puts("What line you want to print ? (put a number)");
        scanf("%d", &nrline_to_printf);
    
    
        while (fgets(buffer, sizeof (buffer), pFile) != NULL) {
            if (nrline_to_printf == nrline_on_the_file) {
                fputs(buffer, stdout);
                return (EXIT_SUCCESS);
            }
            nrline_on_the_file++;
        }
    
    
        puts("That line does not exist");
        return (EXIT_SUCCESS);
    
    
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    A normal, non-programmer counts from one to some upper limit. A C Programmer counts from ZERO to some upper limit. You need to make sure you both are talking about the same line number! I leave it to you to figure out this simple change, and how to inform the user if the first line is one or zero!

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by rstanley View Post
    A normal, non-programmer counts from one to some upper limit. A C Programmer counts from ZERO to some upper limit. You need to make sure you both are talking about the same line number! I leave it to you to figure out this simple change, and how to inform the user if the first line is one or zero!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 100
    
    int count_lines(FILE * pFile, int nrlines_of_the_file);
    
    int main(int argc, char** argv) {
    
    
        FILE * pFile;
    
        char buffer[SIZE];
    
        if ((pFile = fopen("code.txt", "rt")) == NULL) {
            return -1;
        }
    
        int nrlines_of_the_file = 0;
        int nrline_to_printf = 0;
        int nrline_on_the_file = 0;
    
        // The file have at least one line
        nrlines_of_the_file = count_lines(pFile, nrlines_of_the_file);
    
    
        do {
            printf("What line you want to print ?\n(put a number between 1 and %d)\n", nrlines_of_the_file);
            scanf("%d", &nrline_to_printf);
        } while (nrline_to_printf < 1 || nrline_to_printf > nrlines_of_the_file);
    
        while (fgets(buffer, sizeof (buffer), pFile) != NULL) {
            if (nrline_to_printf == nrline_on_the_file + 1) {
                fputs(buffer, stdout);
                return (EXIT_SUCCESS);
            }
            nrline_on_the_file++;
        }
    
        return (EXIT_SUCCESS);
    }
    
    
    int count_lines(FILE * pFile, int nrlines_of_the_file) {
    
        char buffer[SIZE];
    
        while (fgets(buffer, sizeof (buffer), pFile) != NULL) {
            nrlines_of_the_file++;
        }
    
        rewind(pFile);
    
        return nrlines_of_the_file;
    }
    How can i use the buffer from main on that function ?

    int count_lines(FILE * pFile, int nrlines_of_the_file, char * buffer) .... // i am doing something wrong, it says my file have 11 lines but in reality the file have 4 lines.
    Last edited by thinkabout; 07-10-2017 at 12:00 PM.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by thinkabout View Post
    int count_lines(FILE * pFile, int nrlines_of_the_file, char * buffer) .... // i am doing something wrong, it says my file have 11 lines but in reality the file have 4 lines.
    How long is each line in the file? If any line is longer than 99 characters (including the line terminator), then it will take more than one fgets() call to read the entire line, and it will be counted as more than one line. You could change count_lines() to increment nrlines_of_the_file only when the string read by fgets() ends in a line terminator (\n).

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by christop View Post
    How long is each line in the file? If any line is longer than 99 characters (including the line terminator), then it will take more than one fgets() call to read the entire line, and it will be counted as more than one line. You could change count_lines() to increment nrlines_of_the_file only when the string read by fgets() ends in a line terminator (\n).
    Now i got other problem if the user forget to press enter on the last line, it counts 3 lines and not 4.


    Code:
    int count_lines(FILE * pFile, int nrlines_of_the_file) {
    
        char buffer[SIZE];
    
        while (fgets(buffer, sizeof (buffer), pFile) != NULL) {
            if (buffer[strlen(buffer) - 1] == '\n'){
                    nrlines_of_the_file++;
                }
            }
    
        rewind(pFile);
    
        return nrlines_of_the_file;
    }
    And i still want to user the variable buffer from the main function.
    Last edited by thinkabout; 07-10-2017 at 02:21 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > And i still want to user the variable buffer from the main function.
    How about this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    #define SIZE 50
    int count_lines(FILE * pFile, char *buffer, size_t bufsize ) {
      int nrlines_of_the_file = 0;
    
      while (fgets(buffer, bufsize, pFile) != NULL) {
        if (buffer[strlen(buffer) - 1] == '\n') {
          nrlines_of_the_file++;
        }
        else if ( feof(pFile) ) {  // only happens at end of file when last line doesn't have \n
          nrlines_of_the_file++;
        }
      }
      rewind(pFile);
    
      return nrlines_of_the_file;
    }
    
    int main ( ) {
      char buffer[SIZE];
      FILE *fp = fopen("foo.c","r");
      printf("%d\n", count_lines(fp,buffer,sizeof(buffer)) );
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    61
    I see, i dinīt know how to pass the strings, and no one teach me about size_t.

    I know i can use int bufsize, but i think the correct way is to use size_t (for sure a positive value).

    Why size_t matters | Embedded . - A little to advanced for me.

    size_t - C++ Reference


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 100
    
    
    int count_lines(FILE * pFile, char *buffer, size_t bufsize, int nrlines_of_the_file);
    int ask_user_input(int nrline_to_printf, int nrlines_of_the_file);
    int printf_the_line(FILE * pFile, char *buffer, size_t bufsize, int nrline_to_printf);
    
    
    int main(int argc, char** argv) {
    
    
        char buffer[SIZE];
        FILE * pFile;
    
    
        if ((pFile = fopen("code.txt", "rt")) == NULL)
            return -1;
    
    
        int nrlines_of_the_file = 0;
        int nrline_to_printf = 0;
    
    
        // The file have at least one line
        nrlines_of_the_file = count_lines(pFile, buffer, sizeof (buffer), nrlines_of_the_file);
        nrline_to_printf = ask_user_input(nrline_to_printf, nrlines_of_the_file);
        printf_the_line(pFile, buffer, sizeof (buffer), nrline_to_printf);
    
    
        return (EXIT_SUCCESS);
    
    
    }
    
    
    int count_lines(FILE * pFile, char *buffer, size_t bufsize, int nrlines_of_the_file) {
    
    
        while (fgets(buffer, bufsize, pFile) != NULL) {
            if (buffer[strlen(buffer) - 1] == '\n') {
                nrlines_of_the_file++;
            } else if (feof(pFile)) { // only happens at end of file when last line doesn't have \n - user forgets to press "Enter"
                nrlines_of_the_file++;
            }
        }
        rewind(pFile);
    
    
        return nrlines_of_the_file;
    }
    
    
    int ask_user_input(int nrline_to_printf, int nrlines_of_the_file) {
    
    
        do {
            printf("What line you want to print ?\n(put a number between 1 and %d)\n", nrlines_of_the_file);
            scanf("%d", &nrline_to_printf);
        } while (nrline_to_printf < 1 || nrline_to_printf > nrlines_of_the_file);
    
    
        return nrline_to_printf;
    
    
    }
    
    
    int printf_the_line(FILE * pFile, char *buffer, size_t bufsize, int nrline_to_printf) {
    
    
        int nrline_on_the_file = 0;
    
    
        while (fgets(buffer, bufsize, pFile) != NULL) {
            if (nrline_to_printf == nrline_on_the_file + 1) {
                fputs(buffer, stdout);
                return (EXIT_SUCCESS);
            }
            nrline_on_the_file++;
        }
        return 0;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    int count_lines(FILE * pFile, char *buffer, size_t bufsize, int nrlines_of_the_file) {
    There is no point passing in a parameter if you never use the initial value.

    I mean, this also works and simplifies things for the caller.
    Code:
    int count_lines(FILE * pFile, char *buffer, size_t bufsize ) {
        int nrlines_of_the_file = 0;
        // rest of your code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2013, 06:21 AM
  2. Replies: 5
    Last Post: 04-17-2012, 04:52 PM
  3. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 10:09 AM
  4. trying to print a line from a text file
    By kryonik in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2006, 09:14 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

Tags for this Thread