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);
}