For my C code, where I am reading a file and sorting it is giving me garbage outputs. Why?Why is “while ( !feof (file) )” always wrong?In C, I am having trouble with the algorithm of sorting elements from a file from largest to smallestWhy should text files end with a newline?problem with flushing input stream CHow to read all files in a folder from Java?How to read a file line-by-line into a list?Why is “while ( !feof (file) )” always wrong?File reading and store as int in Chow do i read file of ints and store in variables in cfscanf— trying to scan txt file of ints, fscanf only reads 1sHow to read text file in C without fopen
Is it possible to observe space debris with Binoculars?
What are some countries where you can be imprisoned for reading or owning a Bible?
Which costing factors go into the optimizer choosing different types of spools?
How do I make my fill-in-the-blank exercise more obvious?
To which airspace does the border of two adjacent airspaces belong to?
How do I anonymously report the Establishment Clause being broken?
GFI outlets tripped after power outage
How can I oppose my advisor granting gift authorship to a collaborator?
Darwin alternative to `lsb_release -a`?
Bidirectional Dictionary
What's the point of this macro?
Project Euler Problem 45
What quests do you need to stop at before you make an enemy of a faction for each faction?
Is there some sort of French saying for "a person's signature move"?
If I sell my PS4 game disc and buy a digital version, can I still access my saved game?
Does POSIX guarantee the paths to any standard utilities?
Why are some hotels asking you to book through Booking.com instead of matching the price at the front desk?
Are there mathematical concepts that exist in the fourth dimension, but not in the third dimension?
Undefined Hamiltonian for this particular Lagrangian
Are buttons really enough to bound validities by S4.2?
Why are all volatile liquids combustible
First Number to Contain Each Letter
Why is a pressure canner needed when canning?
Looking for a big fantasy novel about scholarly monks that sort of worship math?
For my C code, where I am reading a file and sorting it is giving me garbage outputs. Why?
Why is “while ( !feof (file) )” always wrong?In C, I am having trouble with the algorithm of sorting elements from a file from largest to smallestWhy should text files end with a newline?problem with flushing input stream CHow to read all files in a folder from Java?How to read a file line-by-line into a list?Why is “while ( !feof (file) )” always wrong?File reading and store as int in Chow do i read file of ints and store in variables in cfscanf— trying to scan txt file of ints, fscanf only reads 1sHow to read text file in C without fopen
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am doing an assignment which is:
Find k largest elements of a file. Allocate an array of size k and while you read the numbers from the file, store the k largest numbers in the array. When you read the next element from the file, find if the array needs to be modified or not. Assume that next element read is 80. Since 80 is larger than the smallest element, we need to shift elements < 80 to the right by 1 position and create space for 80. In main() use argc and argv to read the filename and k from the user and compute and print the k largest elements. Name your program assign3.c First parameter is filename and second parameter is k. You need to use atoi()in stdlib.h to convert strings to integer.
My problem is that I am getting garbage values for both arr and sorted arr?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
int main(int argc, char *argv[])//
FILE *iFile;//file pointer
int i = 0, n, temp = 0, count = 0, j;
int k = atoi(argv[1]);//convert strings into int
int *arr = (int *)malloc(k * sizeof(int));////allocate an array of size k
iFile = fopen("a.txt", "r");//opens file
if (iFile == NULL)
return -1;
while (feof(iFile) <= 0)
fscanf(iFile, "%d", arr);
printf("arr= %dn", arr);
count = count++;
for (i = 0; i < count; i++) //Loop for descending ordering
for (j = 1; j <= count; j++) //Loop for comparing other values
if (arr[j] < arr[i]) //Comparing other array elements
temp = arr[i]; //Using temporary variable for storing last value
arr[i] = arr[j]; //replacing value
arr[j] = temp; //storing last value
for (i = 0; i < k + 1; i++)
printf("sorted arr is =%dn", arr[i]);
fclose(iFile);
free(arr);
c arrays file loops
|
show 5 more comments
I am doing an assignment which is:
Find k largest elements of a file. Allocate an array of size k and while you read the numbers from the file, store the k largest numbers in the array. When you read the next element from the file, find if the array needs to be modified or not. Assume that next element read is 80. Since 80 is larger than the smallest element, we need to shift elements < 80 to the right by 1 position and create space for 80. In main() use argc and argv to read the filename and k from the user and compute and print the k largest elements. Name your program assign3.c First parameter is filename and second parameter is k. You need to use atoi()in stdlib.h to convert strings to integer.
My problem is that I am getting garbage values for both arr and sorted arr?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
int main(int argc, char *argv[])//
FILE *iFile;//file pointer
int i = 0, n, temp = 0, count = 0, j;
int k = atoi(argv[1]);//convert strings into int
int *arr = (int *)malloc(k * sizeof(int));////allocate an array of size k
iFile = fopen("a.txt", "r");//opens file
if (iFile == NULL)
return -1;
while (feof(iFile) <= 0)
fscanf(iFile, "%d", arr);
printf("arr= %dn", arr);
count = count++;
for (i = 0; i < count; i++) //Loop for descending ordering
for (j = 1; j <= count; j++) //Loop for comparing other values
if (arr[j] < arr[i]) //Comparing other array elements
temp = arr[i]; //Using temporary variable for storing last value
arr[i] = arr[j]; //replacing value
arr[j] = temp; //storing last value
for (i = 0; i < k + 1; i++)
printf("sorted arr is =%dn", arr[i]);
fclose(iFile);
free(arr);
c arrays file loops
fscanf
return the number of element that have been successfully parsed, usewhile(fscanf(iFile, "%d", &arr[i]) != 1)
– dvhh
Mar 28 at 4:38
2
If I understand you correctly, you have to read the largest k number of integers from a file containing an arbitrary number N integers, and you're asking how to find out the value of N? The value of N is not germane to the problem. You only need to read through the file once, no matter how large N is.
– Mark Benningfield
Mar 28 at 4:53
1
Don't read directly into the array; use a temp variable. Compare that to the smallest value that you have read in so far. If it is larger, and you haven't read in k values yet, stick it in the array at the current location (that you are also keeping track of). Once you have read in k values, if you read a value that is larger than your smallest value so far, dump the smallest, add the new value, and sort the array. Pick the new smallest, and continue until you reach the end of the file.
– Mark Benningfield
Mar 28 at 5:11
1
Please don't modify your code. This makes all previous comments and answers useless. No one wants to aim at a moving target. If you do modifications to your code, you can add it as new version but you shouldn't delete the initial code.
– Gerhardh
Mar 29 at 8:58
1
@Gerhardh I am sorry about that. From now on I will keep the old code commented at the end of my new code.
– Su.she
Mar 29 at 9:56
|
show 5 more comments
I am doing an assignment which is:
Find k largest elements of a file. Allocate an array of size k and while you read the numbers from the file, store the k largest numbers in the array. When you read the next element from the file, find if the array needs to be modified or not. Assume that next element read is 80. Since 80 is larger than the smallest element, we need to shift elements < 80 to the right by 1 position and create space for 80. In main() use argc and argv to read the filename and k from the user and compute and print the k largest elements. Name your program assign3.c First parameter is filename and second parameter is k. You need to use atoi()in stdlib.h to convert strings to integer.
My problem is that I am getting garbage values for both arr and sorted arr?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
int main(int argc, char *argv[])//
FILE *iFile;//file pointer
int i = 0, n, temp = 0, count = 0, j;
int k = atoi(argv[1]);//convert strings into int
int *arr = (int *)malloc(k * sizeof(int));////allocate an array of size k
iFile = fopen("a.txt", "r");//opens file
if (iFile == NULL)
return -1;
while (feof(iFile) <= 0)
fscanf(iFile, "%d", arr);
printf("arr= %dn", arr);
count = count++;
for (i = 0; i < count; i++) //Loop for descending ordering
for (j = 1; j <= count; j++) //Loop for comparing other values
if (arr[j] < arr[i]) //Comparing other array elements
temp = arr[i]; //Using temporary variable for storing last value
arr[i] = arr[j]; //replacing value
arr[j] = temp; //storing last value
for (i = 0; i < k + 1; i++)
printf("sorted arr is =%dn", arr[i]);
fclose(iFile);
free(arr);
c arrays file loops
I am doing an assignment which is:
Find k largest elements of a file. Allocate an array of size k and while you read the numbers from the file, store the k largest numbers in the array. When you read the next element from the file, find if the array needs to be modified or not. Assume that next element read is 80. Since 80 is larger than the smallest element, we need to shift elements < 80 to the right by 1 position and create space for 80. In main() use argc and argv to read the filename and k from the user and compute and print the k largest elements. Name your program assign3.c First parameter is filename and second parameter is k. You need to use atoi()in stdlib.h to convert strings to integer.
My problem is that I am getting garbage values for both arr and sorted arr?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
int main(int argc, char *argv[])//
FILE *iFile;//file pointer
int i = 0, n, temp = 0, count = 0, j;
int k = atoi(argv[1]);//convert strings into int
int *arr = (int *)malloc(k * sizeof(int));////allocate an array of size k
iFile = fopen("a.txt", "r");//opens file
if (iFile == NULL)
return -1;
while (feof(iFile) <= 0)
fscanf(iFile, "%d", arr);
printf("arr= %dn", arr);
count = count++;
for (i = 0; i < count; i++) //Loop for descending ordering
for (j = 1; j <= count; j++) //Loop for comparing other values
if (arr[j] < arr[i]) //Comparing other array elements
temp = arr[i]; //Using temporary variable for storing last value
arr[i] = arr[j]; //replacing value
arr[j] = temp; //storing last value
for (i = 0; i < k + 1; i++)
printf("sorted arr is =%dn", arr[i]);
fclose(iFile);
free(arr);
c arrays file loops
c arrays file loops
edited Mar 29 at 5:51
Su.she
asked Mar 28 at 4:23
Su.sheSu.she
256 bronze badges
256 bronze badges
fscanf
return the number of element that have been successfully parsed, usewhile(fscanf(iFile, "%d", &arr[i]) != 1)
– dvhh
Mar 28 at 4:38
2
If I understand you correctly, you have to read the largest k number of integers from a file containing an arbitrary number N integers, and you're asking how to find out the value of N? The value of N is not germane to the problem. You only need to read through the file once, no matter how large N is.
– Mark Benningfield
Mar 28 at 4:53
1
Don't read directly into the array; use a temp variable. Compare that to the smallest value that you have read in so far. If it is larger, and you haven't read in k values yet, stick it in the array at the current location (that you are also keeping track of). Once you have read in k values, if you read a value that is larger than your smallest value so far, dump the smallest, add the new value, and sort the array. Pick the new smallest, and continue until you reach the end of the file.
– Mark Benningfield
Mar 28 at 5:11
1
Please don't modify your code. This makes all previous comments and answers useless. No one wants to aim at a moving target. If you do modifications to your code, you can add it as new version but you shouldn't delete the initial code.
– Gerhardh
Mar 29 at 8:58
1
@Gerhardh I am sorry about that. From now on I will keep the old code commented at the end of my new code.
– Su.she
Mar 29 at 9:56
|
show 5 more comments
fscanf
return the number of element that have been successfully parsed, usewhile(fscanf(iFile, "%d", &arr[i]) != 1)
– dvhh
Mar 28 at 4:38
2
If I understand you correctly, you have to read the largest k number of integers from a file containing an arbitrary number N integers, and you're asking how to find out the value of N? The value of N is not germane to the problem. You only need to read through the file once, no matter how large N is.
– Mark Benningfield
Mar 28 at 4:53
1
Don't read directly into the array; use a temp variable. Compare that to the smallest value that you have read in so far. If it is larger, and you haven't read in k values yet, stick it in the array at the current location (that you are also keeping track of). Once you have read in k values, if you read a value that is larger than your smallest value so far, dump the smallest, add the new value, and sort the array. Pick the new smallest, and continue until you reach the end of the file.
– Mark Benningfield
Mar 28 at 5:11
1
Please don't modify your code. This makes all previous comments and answers useless. No one wants to aim at a moving target. If you do modifications to your code, you can add it as new version but you shouldn't delete the initial code.
– Gerhardh
Mar 29 at 8:58
1
@Gerhardh I am sorry about that. From now on I will keep the old code commented at the end of my new code.
– Su.she
Mar 29 at 9:56
fscanf
return the number of element that have been successfully parsed, use while(fscanf(iFile, "%d", &arr[i]) != 1)
– dvhh
Mar 28 at 4:38
fscanf
return the number of element that have been successfully parsed, use while(fscanf(iFile, "%d", &arr[i]) != 1)
– dvhh
Mar 28 at 4:38
2
2
If I understand you correctly, you have to read the largest k number of integers from a file containing an arbitrary number N integers, and you're asking how to find out the value of N? The value of N is not germane to the problem. You only need to read through the file once, no matter how large N is.
– Mark Benningfield
Mar 28 at 4:53
If I understand you correctly, you have to read the largest k number of integers from a file containing an arbitrary number N integers, and you're asking how to find out the value of N? The value of N is not germane to the problem. You only need to read through the file once, no matter how large N is.
– Mark Benningfield
Mar 28 at 4:53
1
1
Don't read directly into the array; use a temp variable. Compare that to the smallest value that you have read in so far. If it is larger, and you haven't read in k values yet, stick it in the array at the current location (that you are also keeping track of). Once you have read in k values, if you read a value that is larger than your smallest value so far, dump the smallest, add the new value, and sort the array. Pick the new smallest, and continue until you reach the end of the file.
– Mark Benningfield
Mar 28 at 5:11
Don't read directly into the array; use a temp variable. Compare that to the smallest value that you have read in so far. If it is larger, and you haven't read in k values yet, stick it in the array at the current location (that you are also keeping track of). Once you have read in k values, if you read a value that is larger than your smallest value so far, dump the smallest, add the new value, and sort the array. Pick the new smallest, and continue until you reach the end of the file.
– Mark Benningfield
Mar 28 at 5:11
1
1
Please don't modify your code. This makes all previous comments and answers useless. No one wants to aim at a moving target. If you do modifications to your code, you can add it as new version but you shouldn't delete the initial code.
– Gerhardh
Mar 29 at 8:58
Please don't modify your code. This makes all previous comments and answers useless. No one wants to aim at a moving target. If you do modifications to your code, you can add it as new version but you shouldn't delete the initial code.
– Gerhardh
Mar 29 at 8:58
1
1
@Gerhardh I am sorry about that. From now on I will keep the old code commented at the end of my new code.
– Su.she
Mar 29 at 9:56
@Gerhardh I am sorry about that. From now on I will keep the old code commented at the end of my new code.
– Su.she
Mar 29 at 9:56
|
show 5 more comments
1 Answer
1
active
oldest
votes
Use a while loop condition like this
While(!feof(iFile))
....
feof() checks whether end of file is reached and returns 0 otherwise
but that still just reads the file.
– Su.she
Mar 28 at 5:03
3
Why is “while (!feof(file))” always wrong? - `stackoverflow.com/questions/5431941/…'
– MayurK
Mar 28 at 6:12
the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop .
– Ajay T Jose
Mar 28 at 7:18
1
like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read.
– Jerry Jeremiah
Mar 29 at 6:19
@jerry yes you are correct, but it works for most cases as far as I've seen .
– Ajay T Jose
Mar 30 at 7:10
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%2f55390137%2ffor-my-c-code-where-i-am-reading-a-file-and-sorting-it-is-giving-me-garbage-out%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
Use a while loop condition like this
While(!feof(iFile))
....
feof() checks whether end of file is reached and returns 0 otherwise
but that still just reads the file.
– Su.she
Mar 28 at 5:03
3
Why is “while (!feof(file))” always wrong? - `stackoverflow.com/questions/5431941/…'
– MayurK
Mar 28 at 6:12
the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop .
– Ajay T Jose
Mar 28 at 7:18
1
like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read.
– Jerry Jeremiah
Mar 29 at 6:19
@jerry yes you are correct, but it works for most cases as far as I've seen .
– Ajay T Jose
Mar 30 at 7:10
add a comment |
Use a while loop condition like this
While(!feof(iFile))
....
feof() checks whether end of file is reached and returns 0 otherwise
but that still just reads the file.
– Su.she
Mar 28 at 5:03
3
Why is “while (!feof(file))” always wrong? - `stackoverflow.com/questions/5431941/…'
– MayurK
Mar 28 at 6:12
the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop .
– Ajay T Jose
Mar 28 at 7:18
1
like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read.
– Jerry Jeremiah
Mar 29 at 6:19
@jerry yes you are correct, but it works for most cases as far as I've seen .
– Ajay T Jose
Mar 30 at 7:10
add a comment |
Use a while loop condition like this
While(!feof(iFile))
....
feof() checks whether end of file is reached and returns 0 otherwise
Use a while loop condition like this
While(!feof(iFile))
....
feof() checks whether end of file is reached and returns 0 otherwise
answered Mar 28 at 4:34
Ajay T JoseAjay T Jose
1
1
but that still just reads the file.
– Su.she
Mar 28 at 5:03
3
Why is “while (!feof(file))” always wrong? - `stackoverflow.com/questions/5431941/…'
– MayurK
Mar 28 at 6:12
the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop .
– Ajay T Jose
Mar 28 at 7:18
1
like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read.
– Jerry Jeremiah
Mar 29 at 6:19
@jerry yes you are correct, but it works for most cases as far as I've seen .
– Ajay T Jose
Mar 30 at 7:10
add a comment |
but that still just reads the file.
– Su.she
Mar 28 at 5:03
3
Why is “while (!feof(file))” always wrong? - `stackoverflow.com/questions/5431941/…'
– MayurK
Mar 28 at 6:12
the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop .
– Ajay T Jose
Mar 28 at 7:18
1
like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read.
– Jerry Jeremiah
Mar 29 at 6:19
@jerry yes you are correct, but it works for most cases as far as I've seen .
– Ajay T Jose
Mar 30 at 7:10
but that still just reads the file.
– Su.she
Mar 28 at 5:03
but that still just reads the file.
– Su.she
Mar 28 at 5:03
3
3
Why is “while (!feof(file))” always wrong? - `stackoverflow.com/questions/5431941/…'
– MayurK
Mar 28 at 6:12
Why is “while (!feof(file))” always wrong? - `stackoverflow.com/questions/5431941/…'
– MayurK
Mar 28 at 6:12
the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop .
– Ajay T Jose
Mar 28 at 7:18
the loop will break when file pointer(iFile) has reached the end of the file .In that case feof(iFile) returns 1 hence !feof(iFile) will be 0 hence will break the loop .
– Ajay T Jose
Mar 28 at 7:18
1
1
like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read.
– Jerry Jeremiah
Mar 29 at 6:19
like @MayurK said feof() only checks to see if the end of file marker has been seen - it doesn't do anything for any other kind of end condition (including if the file doesn't have an of file marker) Never use feof as a loop condition - always use something that checks for successful read.
– Jerry Jeremiah
Mar 29 at 6:19
@jerry yes you are correct, but it works for most cases as far as I've seen .
– Ajay T Jose
Mar 30 at 7:10
@jerry yes you are correct, but it works for most cases as far as I've seen .
– Ajay T Jose
Mar 30 at 7:10
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%2f55390137%2ffor-my-c-code-where-i-am-reading-a-file-and-sorting-it-is-giving-me-garbage-out%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
fscanf
return the number of element that have been successfully parsed, usewhile(fscanf(iFile, "%d", &arr[i]) != 1)
– dvhh
Mar 28 at 4:38
2
If I understand you correctly, you have to read the largest k number of integers from a file containing an arbitrary number N integers, and you're asking how to find out the value of N? The value of N is not germane to the problem. You only need to read through the file once, no matter how large N is.
– Mark Benningfield
Mar 28 at 4:53
1
Don't read directly into the array; use a temp variable. Compare that to the smallest value that you have read in so far. If it is larger, and you haven't read in k values yet, stick it in the array at the current location (that you are also keeping track of). Once you have read in k values, if you read a value that is larger than your smallest value so far, dump the smallest, add the new value, and sort the array. Pick the new smallest, and continue until you reach the end of the file.
– Mark Benningfield
Mar 28 at 5:11
1
Please don't modify your code. This makes all previous comments and answers useless. No one wants to aim at a moving target. If you do modifications to your code, you can add it as new version but you shouldn't delete the initial code.
– Gerhardh
Mar 29 at 8:58
1
@Gerhardh I am sorry about that. From now on I will keep the old code commented at the end of my new code.
– Su.she
Mar 29 at 9:56