Thread: Help with displaying a text file

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    Help with displaying a text file

    Hi Guys,

    I need some help with displaying a text file. I need to display the file 20 lines at a time, press C to continue Q to quit & have an option to print line numbers or not. I have the file printing and pausing at every 20 lines & I can get the line numbers to print out.

    Where I'm having the problem is 1. the C for continue/Q for quit & 2. Only printing the line numbers when I choose to.

    Heres my code so far.

    Any help would be appreciated.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_CHARACTERS 81
    
    main()
    {
    
        FILE *fp_in;
        char one_line[MAX_CHARACTERS];
        int line_number = 1;
        char reply [81];
       
    
    
        if ( (fp_in=fopen("dt249.txt","r"))==NULL)
            puts("Error in opening dt249.txt" );
    
        else
            printf("Print line No's? (Y/N)\n");
            scanf("%s",&reply);
    
    
            while(fgets(one_line,MAX_CHARACTERS,fp_in )!=NULL )
          {
                    if ( line_number %20== 0 );
                    {
                    printf(" %d %s ",line_number++,one_line);
    
                    }
                
                    if (line_number %20== 0)
                    {
                    printf("Press C/spacebar to continue or Q to quit");
                    gets(reply);
                    }
    
            }
    
    
            fclose(fp_in);
            printf("total line numbers = %d",line_number-1);
    }

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Well, as I can see, you want to print out the string called one_line when the line_number is 20. But the mistake comes from the fact that you are incrementing it ONLY if line_number divides perfectly with 20. Which will never be true, since its initial value is 1. Therefore, you should also add:
    Code:
     line_number++;
    right after the if instruction.

    As for the C and Q, well... you are not even testing what letter the user pressed. The code might look like so:
    Code:
     reply = getchar();
    if (reply == 'C' || reply == ' ')
    continue;
    if (reply == 'Q')
    break;
    Hope it helps.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    15
    This might help you out:

    Code:
    #include <stdio.h>
    
    #define MAXLINE 127
    
    #define TRUE 1
    #define FALSE 0
    
    int main(int argc, char **argv)
    {
          FILE *fp;
          char text_buf[80];
          char reply;
          int lineno = 1;
          int show_line_numbers;
    
          if ((fp = fopen(argv[1], "r")) == NULL)
            {
                fprintf(stderr, "ERROR: Can't open file! Exiting...\n");
                return -1;
            }
    
          printf("Print line numbers? (Y/N): ");
          scanf(" %c", &reply);
    
          if (reply == 'Y')
                show_line_numbers = TRUE;
          else if (reply == 'N')
                show_line_numbers = FALSE;
          else
            {
                printf("Invalid Option!\n");
                return -1;
            }
    
          while (fgets(text_buf, MAXLINE, fp))
            {
                if (show_line_numbers)
                      printf("%-4d: %s", lineno, text_buf);
                else
                      printf("%s", text_buf);
    
                lineno++;
    
                if (lineno % 20 == 0)
                  {
                      printf("Continue? (C/Q): ");
                      scanf(" %c", &reply);
    
                      if (reply == 'C')
                            continue;
                      else if (reply == 'Q')
                            break;
                  }
            }
    
          return 0;
    }
    Three notes:

    1.) For the problem of printing line numbers only when you choose: in this program we're going to show the line anyway, hence lines 38-41, the trick is to use a separate variable to test whether we should be showing line numbers.

    2.) You'll notice that in the scanf call when i read in the value for reply, there's a space before the character conversion and the reason for this is the way that scanf reads input, if you leave it out it there will be problems because when you enter a choice, you also unknowingly enter a newline into the standard input stream.

    3.) In the fopen call i used "argv[1]". This is the first command line parameter supplied to the program. so to use this program just call it with a filename on the command line e.g. "program_name any_file_name" without quotes. This makes the program more general so you can use it on any text file.

    best of luck.
    Last edited by Eclipse07; 05-02-2010 at 11:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM