0

So what I'm basically doing here is using a command line argument to open a file but only open it 4 lines at a time, then a prompt to print out add'l lines. I can get the file to print out but I cannot figure out how to get it to only print out a few lines at a time. This is where I'm at....thoughts?

#include <stdio.h>

int main(int argc, char *argv[])
{

char line[1000];
FILE *pt;

  pt = fopen(argv[1], "r");
  if(pt ==  NULL) return -1;
  printf(argv[1], line);
  while(fgets(line, 1000, pt) != NULL)
    printf("%s", line);
  fclose(pt);
  return 0;
}
VolAnd
  • 6,367
  • 3
  • 25
  • 43
Kaz
  • 39
  • 1
  • 6
  • 2
    Please provide an MCVE; as it currently stands, without the file you're opening, we can't reproduce your issue. – autistic Jul 12 '17 at 04:06
  • Sorry, amateur here. I had just made up a random file with lines of text to use called testdata.c. When I ran it i just put ./a.out testdata.c. and it printed out the file line by line. What should I do here? – Kaz Jul 12 '17 at 04:15
  • @Seb: I believe the source code suffices as the input file. Kaz, you need to count the number of lines you've read. When you've read a multiple of 4 lines, you do your prompt and read a response. Remember you'll need to get a newline from standard input; it isn't clear whether you care about what else you get. – Jonathan Leffler Jul 12 '17 at 04:26
  • 1
    Try changing **printf(argv[1],line)** to **printf("%s\n", argv[1]);** Printing line without initialization may cause some kind of lockup on the output – cup Jul 12 '17 at 04:29
  • What exactly is your question? Just 'This is where I'm at....thoughts?' is not a question. – ckruczek Jul 12 '17 at 04:52
  • @ckruczek It is really hart to understand the problem, but I suppose the question is, how to print not whole file in the `while` loop, but make pauses after each 4 lines.... – VolAnd Jul 12 '17 at 04:59
  • @Kaz ... I hope I understand what the question is about – VolAnd Jul 12 '17 at 05:00
  • @JonathanLeffler You're free to *believe* what you like, however in this case as the file is open in *text* mode, we need to be able to see the *text* to ensure its contents are being read in full. See [the accepted answer to this question](https://stackoverflow.com/a/20863975/1989425) for more details... – autistic Jul 20 '17 at 06:47

1 Answers1

1

I start from strange line of your code, and then I will try to answer the question.

Statement

 printf(argv[1], line);

make me curious - what you what to print, actually?

Here line is not initialized, and argv[1] can hardly be used as format line.

So I suppose it should be just

 printf(argv[1]);

or

 printf("Filename is %s\n", argv[1]);

As for reading from a file with name provided as argv[1] your code looks able to work, I mean your code read line by line till the end of file and prints these lines at the screen.

If you want to change this logic, e.g. read only 4 first line, add condition with counter, e.g.:

int cnt;
for (cnt = 0; cnt < 4; cnt++) // repeat reading 4 times
{
    if (fgets(line, 1000, pt) != NULL)
        printf("%s", line);
    else
        break; // stop when reading fails
}

or (I prefer this version)

int cnt = 0;
while (fgets(line, 1000, pt) != NULL && cnt < 4)
{
    printf("%s", line);
    cnt++;
}

Such changes allows to stop reading (as well as output), so only 4 or less lines will be shown at console screen.

Finally, for case when you want to show file by groups of 4 (or other constant value), consider the following snippet:

#include <stdio.h>

#define MAX_LINES_TO_PRINT 4

int main(int argc, char *argv[])
{

    char line[1000];
    FILE *pt;

    pt = fopen(argv[1], "r");
    if (pt == NULL) return -1;
    printf("Filename is %s\n", argv[1]);
    int cnt = 0;
    while (fgets(line, 1000, pt) != NULL)
    {
        printf("%s", line);
        cnt++;
        if (cnt % MAX_LINES_TO_PRINT == 0)
        {
            int answer;
            printf("[%d lines printed] Continue? (Y/N) : ", cnt);
            answer = getchar(); // get user's response
            while (getchar() != '\n'); // clean input buffer after getchar
            if (toupper(answer) == 'N')
            {
                break; // stop reading the file
            }
        }
    }
    fclose(pt);
    return 0;
}

Try this program with your file and ask question if something is unclear.

Changing the value in the line #define MAX_LINES_TO_PRINT 4 you can regulate maximum number of lines printed at once (before the next request to continue), e.g. #define MAX_LINES_TO_PRINT 15 make your program printing up to 15 lines.

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • You guys are the greatest. I got it working but even better I actually understand it. It's clicking a little bit, not even close to being half way crappy at programming but it's starting to sink in!! – Kaz Jul 12 '17 at 05:01
  • @Kaz Since I guessed the essence of the question, I changed the title of the question (let it be an example about how important are the correct wording) – VolAnd Jul 12 '17 at 05:08