Example of C ++ reading and sorting the integer numbers in a file

Source: Internet
Author: User
Tags integer numbers

The program

Your program
Will read a file name from the command line. The file will contain in integers.
The program will read the file (up to 1000 integers), sort them, then report
The integers. The "Report" will show each integer once (in the sorted
Order) along with the number of times that the number appears in the file.
Example, if the file
Numbers.txt
Has
Contents:

 

2
5 3 99-8

5
99 99 100-8

23
-21 3 99 5

Running you
Program will show:

 

C:/cs222> prog2
Numbers.txt

-21
(1)

-8
(2)

2
(1)

3
(2)

5
(3)

23
(1)

99
(4)

100
(1)

Opening and reading files

AFile
In C is a "stream" represented by a pointer of type file * (file is
Defined in the file stdlib. h) and opened with the Library Function

 

File
* Fopen (char * filename, "R ")

The
"R"Must
Be enclosed in double (not single) quotes and
Indicates that the file is being open for reading. If the file cannot be opened
(It may not exist, may be protected, etc.) fopen will return the NULL pointer.
Thus you may open a file with (here
Name
Is the name
Of the file to be opened ).

 

File
* Infile = fopen (name, "R ");

If
(! Infile)

{


/* The operation was unsuccessful */


Printf ("unable to open % s/n ",
Name );


Exit (1 );
/* End the Program */

}

You can read
The file
Fscanf
Which works just like
Scanf
But takes
And extra (first) parameter which is the input stream. The Library Function

 

File
* Feof (File * Stream)

Returns
Non-zero value if the last attempt to read from the stream read no data
Reached the end of file. Thus, a file,
Infile
, Of Integers
Can be read:

 

Int
In;

Do

{


Fscanf (infile, "% d ",
& In );
/* Read from infile into int
Variable in */


If (! Feof (infile ))
/* Only process in if didn't reach
End of file */


{


/* Process in, the last int read, here */


}

}
While (! Feof (infile ));

Fclose (infile );

Sorting an array

You can sort
An array with the Library Function
Qsort
Which uses
The very efficient quicksort algorithm. It has the rather discouraging
Prototype:

 

Void
Qsort (void * base, int N, int size, INT (* compare) (const void *, const void *));

Here
Base
Is a pointer
To the base of the array (usually just the array name ),
N
Is
Number of elements in the array (or the number of array elements to be sorted ),

Size
Is the size of the type stored in the array,
And the fourth Parameter
Compare
Is a pointer
To a function which compares two array elements.
Compare
Must take
Pointers as parameters and must return
Int
. The return
Value must be negative if the first parameter precedes the second, positive if
The first parameter follows the second, and 0 if they are equal. This function
Usually takes some awkward type casts. A Functiom which will work for us is:

 

Int
Compare (const void * s, const void * t)

{


Return * (const int *) S-* (const int *) T;

}

Just pass
Name
Of this function as the fourth parameter
Qsort
.

If you use
Qsort ()
You must
# Include
<Stdlib. h>
.

Counting duplicates

After
User's input has been read into an array and the array has been sorted
Duplicates are easy to identify: duplicate values appear grouped together. Some
Thought is required to do this correctly.

To turn in

You will turn
In your well organized and well commented source code and a sample Terminal
Session using an input file which will be provided to you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.