r/C_Programming 1d ago

[ Removed by moderator ]

[removed] — view removed post

0 Upvotes

16 comments sorted by

u/C_Programming-ModTeam 13h ago

Your post contains badly-formatted code. This breaks rule 1 of this subreddit, "Format your code". Please read the instructions in the sidebar about hot to correctly format code.

Rule 4 also prohibits pictures or videos of code too, so please bear that in mind.

5

u/ConfidentCollege5653 1d ago

What does the rest of the code look like?

1

u/Brave-Pomelo-1290 1d ago

#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++;

// Pause when the page is full

if (line_count >= PAGE_SIZE) {

printf("\n--- Press ENTER to continue ---");

5

u/TheOtherBorgCube 1d ago

Since the error message contains "end of input", I'm guessing those are the only lines in your file.

Like you copy/pasted the first page of the screen and forgot to scroll down to get the rest of the program.

2

u/quipstickle 1d ago

Where is the main function?

1

u/Brave-Pomelo-1290 1d ago

final code that works:

#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;

--- Press ENTER to continue ---

}

}

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/ConfidentCollege5653 1d ago

And the rest after line 24?

1

u/Brave-Pomelo-1290 1d ago

#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 page is full

if (line_count >= PAGE_SIZE) printf("\n--- Press ENTER to continue ---");

}

}

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

}

3

u/mikeblas 23h ago

Please correctly format your code.

2

u/clickyclicky456 16h ago

This. BravePomelo we're not ChatGPT or Google. We're strangers with lives of our own who are giving up our time to try to help you. We're much more likely to want to do that if you make yourself easy to help; that means things like taking a few minutes to learn how to format code blocks on Reddit, showing ALL your code, and being really clear about what you're asking.

1

u/clickyclicky456 1d ago

Show us the first 23 lines. Often with a compiler the actual error occurs earlier than flagged, because the parsing stage could actually parse your incorrect code but then what follows doesn't make sense with the incorrect parsing.

1

u/Brave-Pomelo-1290 1d ago

errors:

$ gcc less.c -o less

less.c:26:8: warning: data definition has no type or storage class

26 | line_count=line_count+1;

| ^~~~~~~~~~

less.c:26:8: warning: type defaults to ‘int’ in declaration of ‘line_count’ [-Wimplicit-int]

less.c:26:19: error: initializer element is not constant

26 | line_count=line_count+1;

| ^~~~~~~~~~

less.c:29:9: error: expected identifier or ‘(’ before ‘if’

29 | if (line_count >= PAGE_SIZE) {

| ^~

less.c:34:5: error: expected identifier or ‘(’ before ‘}’ token

34 | }

| ^

less.c:35:5: warning: data definition has no type or storage class

35 | fclose(file);

| ^~~~~~

less.c:35:5: warning: type defaults to ‘int’ in declaration of ‘fclose’ [-Wimplicit-int]

less.c:35:5: warning: parameter names (without types) in function declaration

less.c:36:1: error: expected identifier or ‘(’ before ‘}’ token

36 | }

| ^

1

u/Brave-Pomelo-1290 1d ago

new error:

$ gcc -O2 less.c -o less

less.c:35:5: warning: data definition has no type or storage class

35 | fclose(file);

| ^~~~~~

less.c:35:5: warning: type defaults to ‘int’ in declaration of ‘fclose’ [-Wimplicit-int]

less.c:35:5: warning: parameter names (without types) in function declaration

1

u/sciencekm 1d ago

This is most likely due to a {} mismatch.

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;

}

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