Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C Why don't I get any output for negative numbers?

irirroi

New Coder
In the text file numbers.txt there is a series of integers separated by commas. Assume that the file is formatted correctly (no letters, spaces, etc., no commas after the last number). However, there is one small problem: these numbers can be significantly larger than the largest value that can be represented by the int type, and even long long can also be negative.

It is necessary to create a program that reads numbers from a file, finds the largest number in the file, and prints it on the screen. It is forbidden to use any arrays or strings in the program.

The program works but for positive inputs, but for negative i don't get any output.


C:
#include <stdio.h>

int main() {
    // Open the file "brojevi.txt" for reading
    FILE *input = fopen("brojevi.txt", "r");
    if (!input) {
        // Print an error message if the file cannot be opened
        printf("Error!\n");
        return 1;
    }

    char current, previous, temp;
    int counter = 0;
    long position = ftell(input), maxPosition = ftell(input);
    int maxCounter = 0;
    int i;
    char tempChar = ' ';
    char sign = '+';

    // Read characters from the file until the end
    do {
        current = fgetc(input);
        sign = (current == '-') ? '-' : sign;
        position = (tempChar == ',') ? ftell(input) - 1 : position;

        if (current == ',' || current == EOF) {
            // Check for the end of a sequence and update maximum if needed
            if (sign == '+' && counter > maxCounter) {
                maxCounter = counter;
                maxPosition = position;
            } else if (sign == '-') {
                if (maxCounter == 0 || (counter < maxCounter && maxCounter != 0)) {
                    maxCounter = counter;
                    maxPosition = position - 1;
                }
            } else if (counter == maxCounter) {
                for (i = 0; i < maxCounter; i++) {
                    fseek(input, position + i, SEEK_SET);
                    previous = fgetc(input);
                    fseek(input, maxPosition + i, SEEK_SET);
                    temp = fgetc(input);
                    if ((sign == '+' && previous > temp) || (sign == '-' && previous < temp)) {
                        maxPosition = position;
                        break;
                    }
                }
            }
            counter = -1;
            sign = '+';
        }
        tempChar = current;
        counter++;
    } while (current != EOF);

    // Set the file position to the beginning of the identified sequence
    fseek(input, maxPosition, SEEK_SET);

    // Print the identified sequence
    do {
        current = fgetc(input);
        if (current != ',' && current != EOF)
            printf("%c", current);
    } while (current != ',' && !feof(input));

    // Close the file
    fclose(input);
    return 0;
}
 
In the text file numbers.txt there is a series of integers separated by commas. Assume that the file is formatted correctly (no letters, spaces, etc., no commas after the last number). However, there is one small problem: these numbers can be significantly larger than the largest value that can be represented by the int type, and even long long can also be negative.

It is necessary to create a program that reads numbers from a file, finds the largest number in the file, and prints it on the screen. It is forbidden to use any arrays or strings in the program.

The program works but for positive inputs, but for negative i don't get any output.


C:
#include <stdio.h>

int main() {
    // Open the file "brojevi.txt" for reading
    FILE *input = fopen("brojevi.txt", "r");
    if (!input) {
        // Print an error message if the file cannot be opened
        printf("Error!\n");
        return 1;
    }

    char current, previous, temp;
    int counter = 0;
    long position = ftell(input), maxPosition = ftell(input);
    int maxCounter = 0;
    int i;
    char tempChar = ' ';
    char sign = '+';

    // Read characters from the file until the end
    do {
        current = fgetc(input);
        sign = (current == '-') ? '-' : sign;
        position = (tempChar == ',') ? ftell(input) - 1 : position;

        if (current == ',' || current == EOF) {
            // Check for the end of a sequence and update maximum if needed
            if (sign == '+' && counter > maxCounter) {
                maxCounter = counter;
                maxPosition = position;
            } else if (sign == '-') {
                if (maxCounter == 0 || (counter < maxCounter && maxCounter != 0)) {
                    maxCounter = counter;
                    maxPosition = position - 1;
                }
            } else if (counter == maxCounter) {
                for (i = 0; i < maxCounter; i++) {
                    fseek(input, position + i, SEEK_SET);
                    previous = fgetc(input);
                    fseek(input, maxPosition + i, SEEK_SET);
                    temp = fgetc(input);
                    if ((sign == '+' && previous > temp) || (sign == '-' && previous < temp)) {
                        maxPosition = position;
                        break;
                    }
                }
            }
            counter = -1;
            sign = '+';
        }
        tempChar = current;
        counter++;
    } while (current != EOF);

    // Set the file position to the beginning of the identified sequence
    fseek(input, maxPosition, SEEK_SET);

    // Print the identified sequence
    do {
        current = fgetc(input);
        if (current != ',' && current != EOF)
            printf("%c", current);
    } while (current != ',' && !feof(input));

    // Close the file
    fclose(input);
    return 0;
}
Hi there,
Quick question... for this line
C:
counter = -1
did you intend it to be set to -1, or were you trying to decrease the current value by 1?
 
I haven't written a C program for decades but a couple of things are jumping out at me.

Problem 1: The comparison logic needs two signs. A current sign and a maximum sign.

Problem 2: After the for loop, a fseek is needed to resume correctly.

This program needs to be broken up into sub routines. Reading a program should feel like admiring art, not doing a sudoku.

Just my opinion.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom