Input File Stream
- Design a program to request the user to input the name of a file.
- The format of the file is plaintext. In each line there is an
integer. The total length of the file is unknown.
- Use "ifstream" to read integers from the file. Calculate the
summation. Output the number of integers and the value of summation.
- Because you cannot predict the length of the data file in
advance, your program must be flexbile to handle files with arbitrary
number of lines. You may find the following code helpful:
while (fsIn >> n)
{
sum += n;
count++;
}
Suppose in your working directory you have a file "a.dat" (you may
create this file by "nano" editor):
1
2
3
4
You program may run as follows:
Please input the filename which stores integers -- a.dat
There are 4 integers; summation is 10