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;
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
add a comment |
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
Don't forget that after you've determinedchis a digit, you have already removed that character from the input stream. And so the call tofscanfwill 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 fromfscanfis equal to 1, otherwise you cannot rely on the value inNbeing valid. Finally, you must not setmin = max = Ninside the loop. Your test is also wrong: compare withN, notch.
– paddy
Mar 26 at 0:30
1
s/char ch;/int ch;/[for a start]
– wildplasser
Mar 26 at 0:34
Yet more errors:minis a pointer, so you must not just set it to some integer value. Instead, you probably wantif (*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
add a comment |
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
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
c file-io
asked Mar 26 at 0:18
PeterPeter
135 bronze badges
135 bronze badges
Don't forget that after you've determinedchis a digit, you have already removed that character from the input stream. And so the call tofscanfwill 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 fromfscanfis equal to 1, otherwise you cannot rely on the value inNbeing valid. Finally, you must not setmin = max = Ninside the loop. Your test is also wrong: compare withN, notch.
– paddy
Mar 26 at 0:30
1
s/char ch;/int ch;/[for a start]
– wildplasser
Mar 26 at 0:34
Yet more errors:minis a pointer, so you must not just set it to some integer value. Instead, you probably wantif (*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
add a comment |
Don't forget that after you've determinedchis a digit, you have already removed that character from the input stream. And so the call tofscanfwill 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 fromfscanfis equal to 1, otherwise you cannot rely on the value inNbeing valid. Finally, you must not setmin = max = Ninside the loop. Your test is also wrong: compare withN, notch.
– paddy
Mar 26 at 0:30
1
s/char ch;/int ch;/[for a start]
– wildplasser
Mar 26 at 0:34
Yet more errors:minis a pointer, so you must not just set it to some integer value. Instead, you probably wantif (*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
add a comment |
1 Answer
1
active
oldest
votes
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;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
add a comment |
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;
add a comment |
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;
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;
answered Mar 26 at 2:43
Suven PandeySuven Pandey
6712 silver badges16 bronze badges
6712 silver badges16 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Don't forget that after you've determined
chis a digit, you have already removed that character from the input stream. And so the call tofscanfwill 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 fromfscanfis equal to 1, otherwise you cannot rely on the value inNbeing valid. Finally, you must not setmin = max = Ninside the loop. Your test is also wrong: compare withN, notch.– paddy
Mar 26 at 0:30
1
s/char ch;/int ch;/[for a start]– wildplasser
Mar 26 at 0:34
Yet more errors:
minis a pointer, so you must not just set it to some integer value. Instead, you probably wantif (*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