Southperry.net
C++ help: File streams - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: C++ help: File streams (/showthread.php?tid=49385)



C++ help: File streams - Jedward - 2011-12-06

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.


C++ help: File streams - Fiel - 2011-12-06

You can't pass a string object into open(). You have to use a C style string.

in_file.open(inFilename.c_str());


C++ help: File streams - Jedward - 2011-12-06

ok, i r dum.
Thanks, Fiel.