Need help fixing a function for finding max_min_numbers in a text-fileFind and restore a deleted file in a Git repositoryHow do I save a String to a text file using Java?How to append text to an existing file in JavaFind all files in a directory with extension .txt in PythonReading a plain text file in JavaPython Print String To Text FileHow to read a large text file line by line using Java?C - fscanf raises EOF before end-of-fileReading numbers from a text file to an array in user defined function in CC Segmentation fault with fscanf

What happens to unproductive professors?

Were Pandaria, Broken Isles, Northrend, Kul'Tiras and Zandalar also affected by the Cataclysm?

Party going through airport security at separate times?

Can I play a mimic PC?

Why does the US seem to have a rather low economic interest in Africa?

Is there a method for differentiating informative comments from commented out code?

How was the Shuttle loaded and unloaded from its carrier aircraft?

Found and corrected a mistake on someone's else paper -- praxis?

Write a function

Is it okay to use open source code to do an interview task?

Did the Ottoman empire suppress the printing press?

What are similar black and/or white permanents to Divine Visitation?

Is it okay to roll multiple attacks that all have advantage in one cluster?

When I press the space bar it deletes the letters in front of it

The origin of a particular self-reference paradox

Stacked light circle effect in Photoshop?

Misrepresented my work history

What is the parallel of Day of the Dead with Stranger things?

What do three diagonal dots above a letter mean in the "Misal rico de Cisneros" (Spain, 1518)?

Why different specifications for telescopes and binoculars?

What would +1/+2/+3 items be called in game?

Having decision making power over someone's assets

Automatically add points geometry while GPS logging?

Why is a mixture of two normally distributed variables only bimodal if their means differ by at least two times the common standard deviation?



Need help fixing a function for finding max_min_numbers in a text-file


Find and restore a deleted file in a Git repositoryHow do I save a String to a text file using Java?How to append text to an existing file in JavaFind all files in a directory with extension .txt in PythonReading a plain text file in JavaPython Print String To Text FileHow to read a large text file line by line using Java?C - fscanf raises EOF before end-of-fileReading numbers from a text file to an array in user defined function in CC Segmentation fault with fscanf






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to create a function with which I can find the lowest and highest number in a txt file. The problem is I don't know how to do it when there are letters and symbols involved. I also don't understand how to read a number, when it is glued to a letter or symbol.



I am basically stuck I created the function to read only from a text file full with numbers and now I want to make it to read the same numbers while ignoring letters.



int max_min_numbers(FILE *fp_in, int *min) 

rewind(fp_in); // Because it is a part of a sub-menu
char ch;
int max = 0, N;
while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

if (isdigit(ch)) // if it is a number -->

fscanf(fp_in, "%d", &N); //reads from stream
min = max = N;
if (min > ch) min = ch; //checks values
if (max < ch) max = ch;


printf("Biggest number is: %dn", max);
printf("Lowest number is: %dn", min); //prints



Data read: a55 5 12 3 3a;



Expected output: 55, 3; Actual output: 51,3;









share|improve this question






















  • Don't forget that after you've determined ch is a digit, you have already removed that character from the input stream. And so the call to fscanf will only read from the character that follows it. Use ungetc to put the character back into the stream. And before you update your min/max values, you should first test that the return value from fscanf is equal to 1, otherwise you cannot rely on the value in N being valid. Finally, you must not set min = max = N inside the loop. Your test is also wrong: compare with N, not ch.

    – paddy
    Mar 26 at 0:30







  • 1





    s/char ch;/int ch;/ [for a start]

    – wildplasser
    Mar 26 at 0:34











  • Yet more errors: min is a pointer, so you must not just set it to some integer value. Instead, you probably want if (*min > N) *min = N;

    – paddy
    Mar 26 at 0:35






  • 1





    Closing this question as "Too Broad" given the non-specific topic, and almost every line of code contains an error. This is unlikely to provide any value to future visitors of the site searching for answers to a specific question. The comments already provide valid feedback to help you, and you are welcome to post a new question if you are still having difficulty after implementing these suggestions.

    – paddy
    Mar 26 at 1:36

















0















I want to create a function with which I can find the lowest and highest number in a txt file. The problem is I don't know how to do it when there are letters and symbols involved. I also don't understand how to read a number, when it is glued to a letter or symbol.



I am basically stuck I created the function to read only from a text file full with numbers and now I want to make it to read the same numbers while ignoring letters.



int max_min_numbers(FILE *fp_in, int *min) 

rewind(fp_in); // Because it is a part of a sub-menu
char ch;
int max = 0, N;
while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

if (isdigit(ch)) // if it is a number -->

fscanf(fp_in, "%d", &N); //reads from stream
min = max = N;
if (min > ch) min = ch; //checks values
if (max < ch) max = ch;


printf("Biggest number is: %dn", max);
printf("Lowest number is: %dn", min); //prints



Data read: a55 5 12 3 3a;



Expected output: 55, 3; Actual output: 51,3;









share|improve this question






















  • Don't forget that after you've determined ch is a digit, you have already removed that character from the input stream. And so the call to fscanf will only read from the character that follows it. Use ungetc to put the character back into the stream. And before you update your min/max values, you should first test that the return value from fscanf is equal to 1, otherwise you cannot rely on the value in N being valid. Finally, you must not set min = max = N inside the loop. Your test is also wrong: compare with N, not ch.

    – paddy
    Mar 26 at 0:30







  • 1





    s/char ch;/int ch;/ [for a start]

    – wildplasser
    Mar 26 at 0:34











  • Yet more errors: min is a pointer, so you must not just set it to some integer value. Instead, you probably want if (*min > N) *min = N;

    – paddy
    Mar 26 at 0:35






  • 1





    Closing this question as "Too Broad" given the non-specific topic, and almost every line of code contains an error. This is unlikely to provide any value to future visitors of the site searching for answers to a specific question. The comments already provide valid feedback to help you, and you are welcome to post a new question if you are still having difficulty after implementing these suggestions.

    – paddy
    Mar 26 at 1:36













0












0








0








I want to create a function with which I can find the lowest and highest number in a txt file. The problem is I don't know how to do it when there are letters and symbols involved. I also don't understand how to read a number, when it is glued to a letter or symbol.



I am basically stuck I created the function to read only from a text file full with numbers and now I want to make it to read the same numbers while ignoring letters.



int max_min_numbers(FILE *fp_in, int *min) 

rewind(fp_in); // Because it is a part of a sub-menu
char ch;
int max = 0, N;
while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

if (isdigit(ch)) // if it is a number -->

fscanf(fp_in, "%d", &N); //reads from stream
min = max = N;
if (min > ch) min = ch; //checks values
if (max < ch) max = ch;


printf("Biggest number is: %dn", max);
printf("Lowest number is: %dn", min); //prints



Data read: a55 5 12 3 3a;



Expected output: 55, 3; Actual output: 51,3;









share|improve this question














I want to create a function with which I can find the lowest and highest number in a txt file. The problem is I don't know how to do it when there are letters and symbols involved. I also don't understand how to read a number, when it is glued to a letter or symbol.



I am basically stuck I created the function to read only from a text file full with numbers and now I want to make it to read the same numbers while ignoring letters.



int max_min_numbers(FILE *fp_in, int *min) 

rewind(fp_in); // Because it is a part of a sub-menu
char ch;
int max = 0, N;
while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

if (isdigit(ch)) // if it is a number -->

fscanf(fp_in, "%d", &N); //reads from stream
min = max = N;
if (min > ch) min = ch; //checks values
if (max < ch) max = ch;


printf("Biggest number is: %dn", max);
printf("Lowest number is: %dn", min); //prints



Data read: a55 5 12 3 3a;



Expected output: 55, 3; Actual output: 51,3;






c file-io






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 0:18









PeterPeter

135 bronze badges




135 bronze badges












  • Don't forget that after you've determined ch is a digit, you have already removed that character from the input stream. And so the call to fscanf will only read from the character that follows it. Use ungetc to put the character back into the stream. And before you update your min/max values, you should first test that the return value from fscanf is equal to 1, otherwise you cannot rely on the value in N being valid. Finally, you must not set min = max = N inside the loop. Your test is also wrong: compare with N, not ch.

    – paddy
    Mar 26 at 0:30







  • 1





    s/char ch;/int ch;/ [for a start]

    – wildplasser
    Mar 26 at 0:34











  • Yet more errors: min is a pointer, so you must not just set it to some integer value. Instead, you probably want if (*min > N) *min = N;

    – paddy
    Mar 26 at 0:35






  • 1





    Closing this question as "Too Broad" given the non-specific topic, and almost every line of code contains an error. This is unlikely to provide any value to future visitors of the site searching for answers to a specific question. The comments already provide valid feedback to help you, and you are welcome to post a new question if you are still having difficulty after implementing these suggestions.

    – paddy
    Mar 26 at 1:36

















  • Don't forget that after you've determined ch is a digit, you have already removed that character from the input stream. And so the call to fscanf will only read from the character that follows it. Use ungetc to put the character back into the stream. And before you update your min/max values, you should first test that the return value from fscanf is equal to 1, otherwise you cannot rely on the value in N being valid. Finally, you must not set min = max = N inside the loop. Your test is also wrong: compare with N, not ch.

    – paddy
    Mar 26 at 0:30







  • 1





    s/char ch;/int ch;/ [for a start]

    – wildplasser
    Mar 26 at 0:34











  • Yet more errors: min is a pointer, so you must not just set it to some integer value. Instead, you probably want if (*min > N) *min = N;

    – paddy
    Mar 26 at 0:35






  • 1





    Closing this question as "Too Broad" given the non-specific topic, and almost every line of code contains an error. This is unlikely to provide any value to future visitors of the site searching for answers to a specific question. The comments already provide valid feedback to help you, and you are welcome to post a new question if you are still having difficulty after implementing these suggestions.

    – paddy
    Mar 26 at 1:36
















Don't forget that after you've determined ch is a digit, you have already removed that character from the input stream. And so the call to fscanf will only read from the character that follows it. Use ungetc to put the character back into the stream. And before you update your min/max values, you should first test that the return value from fscanf is equal to 1, otherwise you cannot rely on the value in N being valid. Finally, you must not set min = max = N inside the loop. Your test is also wrong: compare with N, not ch.

– paddy
Mar 26 at 0:30






Don't forget that after you've determined ch is a digit, you have already removed that character from the input stream. And so the call to fscanf will only read from the character that follows it. Use ungetc to put the character back into the stream. And before you update your min/max values, you should first test that the return value from fscanf is equal to 1, otherwise you cannot rely on the value in N being valid. Finally, you must not set min = max = N inside the loop. Your test is also wrong: compare with N, not ch.

– paddy
Mar 26 at 0:30





1




1





s/char ch;/int ch;/ [for a start]

– wildplasser
Mar 26 at 0:34





s/char ch;/int ch;/ [for a start]

– wildplasser
Mar 26 at 0:34













Yet more errors: min is a pointer, so you must not just set it to some integer value. Instead, you probably want if (*min > N) *min = N;

– paddy
Mar 26 at 0:35





Yet more errors: min is a pointer, so you must not just set it to some integer value. Instead, you probably want if (*min > N) *min = N;

– paddy
Mar 26 at 0:35




1




1





Closing this question as "Too Broad" given the non-specific topic, and almost every line of code contains an error. This is unlikely to provide any value to future visitors of the site searching for answers to a specific question. The comments already provide valid feedback to help you, and you are welcome to post a new question if you are still having difficulty after implementing these suggestions.

– paddy
Mar 26 at 1:36





Closing this question as "Too Broad" given the non-specific topic, and almost every line of code contains an error. This is unlikely to provide any value to future visitors of the site searching for answers to a specific question. The comments already provide valid feedback to help you, and you are welcome to post a new question if you are still having difficulty after implementing these suggestions.

– paddy
Mar 26 at 1:36












1 Answer
1






active

oldest

votes


















0














Here, it prints maximum and minimum value and returns maximum value. It does not handle negative numbers. I would also suggest you to stay away from pointers if you are just starting to learn.



int max_min_numbers(FILE *fp_in, int *min) 

rewind(fp_in); // Because it is a part of a sub-menu
char ch;
int max = 0, N = 0;
while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

if (isdigit(ch)) // if it is a number -->

N = N*10 + ch; //if previous digit is 2 and new digit is 3 N is set to 23

else

if (*min > N) *min = N; //checks values
if (max < N) max = N;
N = 0;


printf("Biggest number is: %dn", max);
printf("Lowest number is: %dn", *min); //prints
return max;






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348173%2fneed-help-fixing-a-function-for-finding-max-min-numbers-in-a-text-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here, it prints maximum and minimum value and returns maximum value. It does not handle negative numbers. I would also suggest you to stay away from pointers if you are just starting to learn.



    int max_min_numbers(FILE *fp_in, int *min) 

    rewind(fp_in); // Because it is a part of a sub-menu
    char ch;
    int max = 0, N = 0;
    while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

    if (isdigit(ch)) // if it is a number -->

    N = N*10 + ch; //if previous digit is 2 and new digit is 3 N is set to 23

    else

    if (*min > N) *min = N; //checks values
    if (max < N) max = N;
    N = 0;


    printf("Biggest number is: %dn", max);
    printf("Lowest number is: %dn", *min); //prints
    return max;






    share|improve this answer



























      0














      Here, it prints maximum and minimum value and returns maximum value. It does not handle negative numbers. I would also suggest you to stay away from pointers if you are just starting to learn.



      int max_min_numbers(FILE *fp_in, int *min) 

      rewind(fp_in); // Because it is a part of a sub-menu
      char ch;
      int max = 0, N = 0;
      while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

      if (isdigit(ch)) // if it is a number -->

      N = N*10 + ch; //if previous digit is 2 and new digit is 3 N is set to 23

      else

      if (*min > N) *min = N; //checks values
      if (max < N) max = N;
      N = 0;


      printf("Biggest number is: %dn", max);
      printf("Lowest number is: %dn", *min); //prints
      return max;






      share|improve this answer

























        0












        0








        0







        Here, it prints maximum and minimum value and returns maximum value. It does not handle negative numbers. I would also suggest you to stay away from pointers if you are just starting to learn.



        int max_min_numbers(FILE *fp_in, int *min) 

        rewind(fp_in); // Because it is a part of a sub-menu
        char ch;
        int max = 0, N = 0;
        while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

        if (isdigit(ch)) // if it is a number -->

        N = N*10 + ch; //if previous digit is 2 and new digit is 3 N is set to 23

        else

        if (*min > N) *min = N; //checks values
        if (max < N) max = N;
        N = 0;


        printf("Biggest number is: %dn", max);
        printf("Lowest number is: %dn", *min); //prints
        return max;






        share|improve this answer













        Here, it prints maximum and minimum value and returns maximum value. It does not handle negative numbers. I would also suggest you to stay away from pointers if you are just starting to learn.



        int max_min_numbers(FILE *fp_in, int *min) 

        rewind(fp_in); // Because it is a part of a sub-menu
        char ch;
        int max = 0, N = 0;
        while ((ch = fgetc(fp_in)) != EOF) // reading until the file is over

        if (isdigit(ch)) // if it is a number -->

        N = N*10 + ch; //if previous digit is 2 and new digit is 3 N is set to 23

        else

        if (*min > N) *min = N; //checks values
        if (max < N) max = N;
        N = 0;


        printf("Biggest number is: %dn", max);
        printf("Lowest number is: %dn", *min); //prints
        return max;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 2:43









        Suven PandeySuven Pandey

        6712 silver badges16 bronze badges




        6712 silver badges16 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348173%2fneed-help-fixing-a-function-for-finding-max-min-numbers-in-a-text-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해