Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ help: File streams
#1
I have no idea whatsoever what is wrong with this source code. inb4 I'm dumb.


Code:
// ENGG 233 Lab9 - Task 2
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

// Initialize a struct for the data point
struct DataPoint
{
    string date;
    double latitude;
    double longitude;
    double magnitude;
    string location;
};

int main()
{
    // Create a vector of DataPoint structs to store the input
    vector<DataPoint> inputData(0);

    // Initialize a variable for the input file
    string inFilename;

    // Prompt the user to enter the filename of the input file
    cout << "Please enter the filename of the text file you wish to be read in, and include the file extension: ";
    cin >> inFilename;

    // Opens the file specified by the user
    ifstream in_file;
    in_file.open(inFilename);

    // If the file fails to open, the program terminates
    if(in_file.fail())
    {
        cout << "The file you are trying to open does not exist." << endl;
        return 0;
    }

    else
    {
        // Read until the end of the file
        while (!in_file.eof())
        {
            // Initialize a variable for the current input struct
            DataPoint readInput;

            // Read the input
            in_file >> readInput.date >> readInput.latitude >> readInput.longitude >> readInput.magnitude >> readInput.location;

            // Read the input into a new struct in the vector
            inputData.push_back(readInput);
        }
    }
    // Close the input file, as it is no longer needed
    in_file.close();

    // Create the output file
    ofstream out_file;
    out_file.open("earthquakeInformationSystem.kml");

    // Write the header code into the kml file
    out_file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl
            << "<kml xmlns=\"http://opengis.net/kml/2.2\">" << endl
            << "<Document>";

    // Make sure the size of inputData is stored as an integer to prevent unsigned vs. signed comparison
    int vectorSize = inputData.size();

    // Repeat inserting this code for each location
    for(int sentinel = 0; sentinel < vectorSize; sentinel++)
    {
        out_file << "   <Placemark>" << endl << "<name>"
                << inputData[sentinel].location << ", "
                << inputData[sentinel].magnitude << ", "
                << inputData[sentinel].date << "</name>" << endl
                << "      <Point>" << endl << "         <coordinates>"
                << inputData[sentinel].longitude << ","
                << inputData[sentinel].latitude << ",0</coordinates>"
                << "      </Point>" << endl << "   </Placemark>" << endl;
    }

    // Input the footer of the code
    out_file << "</Document>" << endl << "</kml>";

    // Close the output file
    out_file.close();
    return 0;
}

If you don't look in codepad, the error is below:
Code:
In function 'int main()':
Line 38: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
compilation terminated due to -Wfatal-errors.

Thanks.
Reply


Messages In This Thread
C++ help: File streams - by Jedward - 2011-12-06, 03:07 PM
C++ help: File streams - by Fiel - 2011-12-06, 03:12 PM
C++ help: File streams - by Jedward - 2011-12-06, 04:01 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)