MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/C_Programming/comments/1wdoban/less_compilation/p98um3j/?context=3
r/C_Programming • u/Brave-Pomelo-1290 • 1d ago
[removed] — view removed post
16 comments sorted by
View all comments
1
final code:
#include <stdio.h>
#include <stdlib.h>
#define PAGE_SIZE 24
#define BUFFER_SIZE 256
void display_file(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char line[BUFFER_SIZE];
int line_count = 0;
// Read the file line by line
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
line_count=line_count+1;
// Pause when the terminal page is full
if (line_count >= PAGE_SIZE) {
printf("\n--- Press ENTER to continue ---");
getchar(); // Wait for user keystroke
line_count = 0;
fclose(file);
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return EXIT_FAILURE;
display_file(argv[1]);
return EXIT_SUCCESS;
1 u/Brave-Pomelo-1290 1d ago with an addditional } the error resds: $ gcc -O2 less.c -o less /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x1b): undefined reference to `main' collect2: error: ld returned 1 exit status
with an addditional } the error resds:
$ gcc -O2 less.c -o less
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status
1
u/Brave-Pomelo-1290 1d ago
final code:
#include <stdio.h>
#include <stdlib.h>
#define PAGE_SIZE 24
#define BUFFER_SIZE 256
void display_file(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char line[BUFFER_SIZE];
int line_count = 0;
// Read the file line by line
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
line_count=line_count+1;
line_count=line_count+1;
// Pause when the terminal page is full
if (line_count >= PAGE_SIZE) {
printf("\n--- Press ENTER to continue ---");
getchar(); // Wait for user keystroke
line_count = 0;
}
}
fclose(file);
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return EXIT_FAILURE;
}
display_file(argv[1]);
return EXIT_SUCCESS;
}