1

I've been working forever on this code, and I can't seem to figure out what's wrong with my code. I'm trying to make it so that the program accepts two command-line arguments, the first being the name of a text file to read, and the second being how many of the lines of the file to print. I have tried to make a loop that counts the number of lines and adds one each time it reaches the new line character, and another loop that prints the lines of the text, but I'm having a lot of trouble. I'm only in an introduction to programming class, and this is C language, and I've tried so many ways to make this work. Here is the function in my code that is supposed to do this. any help would be greatly appreciated!

char ch = 0;
 long num_lines = 0;

while(!feof(fileequals)){
    ch = fgetc(fileequals);
    if(ch == '\n'){
        num_lines++;
    }
}
fseek(fileequals, 0, SEEK_SET);

const int length = 100;
char line[length];
char *c = 0;
long line_count = 0;

if(num_rows_to_read > num_lines){
    num_rows_to_read = num_lines;
}

do{
    c = fgets(line, length, fileequals);
    if (c != NULL && (line_count >= num_lines - num_rows_to_read)){
        printf("%s", line);
        line_count++;
    }
    else{
        continue;
    }
}while (c != NULL);

}

Jules
  • 14,200
  • 13
  • 56
  • 101
emmag
  • 13
  • 1
  • 5
  • 1
    [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – kaylum May 29 '16 at 05:07
  • And what exactly is the problem with your current program? Please provide the inputs, the expected output and the actual output. And suggest you use a debugger to step through your program to find out where things start going wrong. – kaylum May 29 '16 at 05:09
  • the input is supposed to be any file with words in it, and the output should be the last n lines of the file, but when I use this code, I get no output at all, because I guess for some reason it never enters the loop that prints each line – emmag May 29 '16 at 05:12
  • So what does it print *currently*? In your problem statement you also need to state the desired behaviour. Also, `fgets` doesn't work if any of the lines is longer than the length of the buffer! Also, if you look closely, your loop is never broken, because `if (c == NULL)` the `continue` statement is always run; you should remove the whole `else` block. – Antti Haapala -- Слава Україні May 29 '16 at 05:12
  • Why do you have to guess? Use basic debugging techniques to find out for sure - namely a debugger and/or debug print statements to trace the execution of your program. – kaylum May 29 '16 at 05:13
  • If you suspect it doesn't go into the loop at all, please add some debug `printf`s to see where exactly does it go. The problem with the excerpt is that it is not a complete example, and the fault might very well be in code outside it. – Antti Haapala -- Слава Україні May 29 '16 at 05:13
  • none of the lines will be longer than the length of the buffer, the way I have to do this, no line will be longer than 100 characters. – emmag May 29 '16 at 05:14
  • also [`fgetc` returns an `int`](http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc/35356684#35356684) – Antti Haapala -- Слава Україні May 29 '16 at 05:16
  • Possible duplicate of [reading last n lines from file in c/c++](http://stackoverflow.com/questions/17877025/reading-last-n-lines-from-file-in-c-c) – Ani Menon May 29 '16 at 05:19

2 Answers2

0

"line_count++;" should be executed every time after fgets() is executed, irregardless of the if statement. The do-while loop should be like this:

do{
    c = fgets(line, length, fileequals);
    if (c != NULL && (line_count >= num_lines - num_rows_to_read)){
        printf("%s", line);            
    }
    line_count++;
}while (c != NULL);
gkso
  • 513
  • 3
  • 12
0

Try replacing this:

do{
    c = fgets(line, length, fileequals);
    if (c != NULL && (line_count >= num_lines - num_rows_to_read)){
        printf("%s", line);
        line_count++;
    }
    else{
        continue;
    }
}while (c != NULL);

With this:

do{
    c = fgets(line, length, fileequals);
    if (c != NULL && (line_count >= num_lines - num_rows_to_read)){
        printf("%s", line);            
    }
    line_count++;
}while (c != NULL);
alxlives
  • 5,084
  • 4
  • 28
  • 50