Won't open resource file?
Nov 27, 2015 at 2:55am UTC
Here is the code i am using. The code runs, however it will not open the resource file (address.xml) I am using visual studio and at a loss for ideas on what is wrong at the moment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
void intro();
void address(ifstream& fin, string& line);
int main()
{
string line;
ifstream fin;
fin.open("address.xml" );
if (fin.fail()) //opens the file
{
cout << "Something is wrong here. \n" ;
exit(1);
}
intro();
address(fin, line); //this is my function call
return 0;
}
void intro()
{
cout << "This program will output addresses only located in Palmdale \n" ;
}
void address(ifstream& fin, string& line) //the function that outputs the addresses
{
int pos1, pos2;
while (getline(fin, line))
{
if ((pos1 = line.find("<name>" )) << string::npos) //string::npos indicates the length of the string
{
pos1 = line.find(">" ); //this locates the numered location of <name>
pos2 = line.find("</name>" ); //locates the numbered location of </name>
cout << endl << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
//the function line.substr outputs what is in between of the memory location of pos1 and pos2
//pos + 1 reads the first output after <name>
//pos2 - pos - 1 cancels the +1 to pos1
}
if ((pos1 = line.find("<street>" )) << string::npos)
{
pos1 = line.find(">" );
pos2 = line.find("</street>" );
cout << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
}
if ((pos1 = line.find("<city>" )) << string::npos)
{
pos1 = line.find(">" );
pos2 = line.find("</city>" );
cout << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
}
if ((pos1 = line.find("<state>" )) << string::npos)
{
pos1 = line.find(">" );
pos2 = line.find("</state>" );
cout << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
}
if ((pos1 = line.find("<zip>" )) << string::npos)
{
pos1 = line.find(">" );
pos2 = line.find("</zip>" );
cout << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
}
}
}
Last edited on Nov 27, 2015 at 2:57am UTC
Nov 27, 2015 at 8:10am UTC
Have you put the file in the correct location? I think visual studio uses project folder (not the executable folder) as woriking directory for the program.
Last edited on Nov 27, 2015 at 8:10am UTC
Nov 27, 2015 at 1:27pm UTC
What do you think (pos1 = line.find("<name>" )) << string::npos
evaluates to?
Topic archived. No new replies allowed.