Hello,
I am new to programming, and I am working my way through the C++ Primer Plus developers library to learn C++ independently.
I just reached the point where I am learning File I/O, and I have covered and understand the file output portion, but I am having trouble with the file input section. I have the following code for reading numbers from a file. At the end, the program reports the count of numbers read, the sum of the numbers, and the average of the numbers:
The file I am using for input is a .txt file, shown here:
This is the output I get from running the program in Xcode:
And this is the output I get from running the program in Terminal:
What this tells me is that the program is successfully opening the file, but is not reading information. I have made sure the file is a .txt file and not .rtf.
I am running Xcode 5.0.2 on OSX 10.9.1 on a MacBook Pro. Any help would be appreciated.
I am new to programming, and I am working my way through the C++ Primer Plus developers library to learn C++ independently.
I just reached the point where I am learning File I/O, and I have covered and understand the file output portion, but I am having trouble with the file input section. I have the following code for reading numbers from a file. At the end, the program reports the count of numbers read, the sum of the numbers, and the average of the numbers:
Code:
// sumafile.cpp -- reading file input
#include <iostream>
#include <cstdlib> // included for the exit() method
#include <fstream> // included for file I/O
const int Size = 60; // declares Size constant outside of main()
int main()
{
using std::ifstream; // allows us to create ifstream objects
using std::cin;
using std::cout;
using std::endl;
char array_of_char_filename[Size]; // creates an array of char to input a file name
ifstream fin; // declares an ifstream object fin
// prompts the user to enter the name of a file and assigns fin to the user-determined file
cout << "Enter the name of a data file: ";
cin.getline(array_of_char_filename, Size);
fin.open(array_of_char_filename);
// used to determine whether or not the file has successfully been opened
if (!fin.is_open())
{
cout << "Could not open the file " << array_of_char_filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
// declares all variables necessary to perform arithmetic actions below
double double_value;
double double_sum = 0.0;
int int_count = 0;
fin >> double_value; // gets value from the file
// this loop runs while the most recent input is accepted, as determined by good()
while (fin.good())
{
int_count++; // keeps count of the number of elements read
double_sum += double_value; // running sum of those values
fin >> double_value; // reads another value while input is acceptable
}
// this if else construct displays the reasons why the file input was terminated
// the eof() method is used first because the fail() method checks for EOF as well
// as other reasons, so if we execute the first if statement we know that the
// file simply reached the EOF and did not terminate for another reason
if (fin.eof())
{
cout << "End of file reached.\n";
}
else if (fin.fail())
{
cout << "Input terminated by data mismatch.\n";
}
else
{
cout << "Input terminated for unknown reason.\n";
}
// displays to the user the results of reading the file
if (int_count == 0)
{
cout << "No data processed.\n";
}
else
{
cout << "Items read: " << int_count << endl;
cout << "Sum: " << double_sum << endl;
cout << "Average: " << double_sum / double(int_count) << endl;
}
fin.close(); // close the file once finished
return 0;
}
The file I am using for input is a .txt file, shown here:
18 19 18.5 13.5 14
16 19.5 20 18 12 18.5
17.5
This is the output I get from running the program in Xcode:
Enter the name of a data file: test.txt
Input terminated by data mismatch.
No data processed.
Program ended with exit code: 0
And this is the output I get from running the program in Terminal:
Enter the name of a data file: test.txt
Input terminated by data mismatch.
No data processed.
What this tells me is that the program is successfully opening the file, but is not reading information. I have made sure the file is a .txt file and not .rtf.
I am running Xcode 5.0.2 on OSX 10.9.1 on a MacBook Pro. Any help would be appreciated.
Last edited: