Chapter 10Programming Problems Using File
10.1 Sort a File of Integers
This program reads integers from a file, sorts them, and stores the sorted integers into another file. We have already learned how to read integers from a file in Section 9.1. Chapter 8 explains how to sort arrays. Here we put these two things together. Before sorting the numbers, these are the steps: (1) Check whether there are arguments for the input and the output file names. (2) Open the input file. (3) Read integers from the file and count the number of integers in the file. (4) Allocate memory to store the integers.
As explained earlier, a file is a stream. Every time something is read from the file, the stream moves forward. After counting ...
Get Intermediate C Programming, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.