Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Write a program which will read a bunch of integers from an input file, sort them, and print the sorted result to an output file.

You must read the input from input.txt and you must write the output to output.txt. The input will contain one integer per line, and you're expected to stop reading when you reach EOF. The output should also have one integer per line.

Warning: Do not print anything else to output.txt. Do not print prompts like "Please enter a number." The output should not contain anything other than the sorted integers, one per line.

If your input is something like this:

3
4
7
1
5
The output should be

1
3
4
5
7

You must be able to read the input from a file called input.txt in the current directory. You must be able to read the file in a format exactly as shown above - do not assume that any other information will be provided in the file or on standard input. You must write your output to a file called output.txt in the current directory. Do not write or print anything else into output.txt other than the expected answer.

For C/C++ programs, do not use non-standard include files like conio.h and do not use non-standard functions like clrscr or getch. Use only standard C. Do not print anything extra to stdout or to the output file (if any).

What I have tried:

Actually, I don't know much more of File Handling in C. I tried it but I failed for the exact output which I want, so asking for help.
Please help me with this, and I need the program in C language.
Posted
Updated 6-Jan-17 14:54pm
Comments
Jochen Arndt 6-Jan-17 6:47am    
If you show us what you have tried so far and tell us where you got stuck we will help.

But we will not do your homework.

The file handling is the simplest part of this assignment. I will tell you the functions that might be used so that you can look up them:
fopen, fgets and atoi or fscanf, feof, fprintf, fclose.
Vrajesh Bhavsar 6-Jan-17 6:53am    
Buddy this is not my homework, I had taken part in an online competition but before that competition they give such questions. And I failed in solving this. Because I don't know File Handling. That's why asking for help.
Vrajesh Bhavsar 6-Jan-17 7:00am    
I know theories based on it, and I'm in college's first year. And our teacher didn't teach us a single practical program of File Handling. So I don't know what to do in this, I am cleared with the logics behind the question but can't get it done with the program.
Richard MacCutchan 6-Jan-17 7:03am    
So go to the link I gave you and start learning how to read and write simple text files. We are not going to do the work for you.

As this is obviously homework I think no one is going to give you full answer. Instead if you post specific questions, what you have done and in what you're stuck at I believe that you'll get help to those specific problems.

Remember that after all the point in the homework is that you learn and that can be achieved only by doing and solving. If someone else does all the thinking for you, you won't learn these things.

If you just put your mind into it I'm sure you start learning things quickly. :)
 
Share this answer
 
Comments
Vrajesh Bhavsar 6-Jan-17 6:55am    
This is not my homework, I had taken part in an online competition and in that they give a sample question which we need to solve. But I was failed in solving this because I don't know the File handling. If you could help then it would be great.
Vrajesh Bhavsar 6-Jan-17 7:07am    
see here what I had done

#include<stdio.h>

void sort_func( int arr[], int size ){
/* make ur sort function */
}

int main(){
FILE *in = fopen("input.txt", "r"), *out = fopen("output.txt", "w+");

int intarr[100];
int no_of_int,i;

if(in != NULL){
for(no_of_int=0; !feof(in); no_of_int++)
fscanf(in, "%d", &intarr[no_of_int]);

sort_func(intarr, no_of_int);

for(i=0; i<no_of_int; i++)
fprintf(out, "%d\n", intarr[i]);
printf("process done!");
}
else printf("File not found!");
}
Wendelius 6-Jan-17 13:23pm    
Seems that you already got the problem solved, that's great.

Concerning the deleted comments, I didn't take them as rude so don't worry about that :)
Quote:
Buddy this is not my homework, I had taken part in an online competition but before that competition they give such questions
There will be probably a reason for such questions. They want to see if you have some skills (or show you if your skills are sufficient for the competition). But it does not care. CodeProject is not a code writing service.

Quote:
Just now can't getting the sorting, how to do that, just tell me that now.
That is rather rude and will not increase the chance to get an answer.

Anyway, to be back on topic and let this qualify as an answer:

Entering something like "c sort algorithm" into a search engine of your choice should point you to the C standard library function qsort - C++ Reference[^] (don't be irritated by the C++ in the link, it is about C).
 
Share this answer
 
Comments
Vrajesh Bhavsar 6-Jan-17 7:51am    
okay thanks and I was not talking rudely don't take it in that way. Thank you for the help.
You need to really learn C and C++ later, These links are very good lecture.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
Comments
Vrajesh Bhavsar 6-Jan-17 21:20pm    
oh thanks, I will definitely see the links.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900