question

How would i go about making a program read a line such as "aword</word>" from a file and make it stop reading when it encounters </ ?
Read up on stream libs:

1
2
3
#include <iostream>
#include <fstream>
#include <sstream> 


open file

 
std::ifstream ifs("page.txt");


read file

1
2
3
4
5
6
char ch;
ifs.peek(); // look ahead to next char
ifs.get(ch); // read next char

std::string line;
std::getline(ifs, line, '<'); // read up to a specific char into variable line 


Alternatively you could investigate either regular expressions or if your data is XML then possibly an XML parser.
Last edited on
yea I know how to open a file and read it but how do i make it stop at </ ?
Last edited on
Keep reading characters until you find a '<' followed by a '/' ?
What are you trying to achieve and what code do you have so far?
ok what im asking is how do you tell it to stop at </ . What I'm trying to make is a simple filezilla ftp recoverer that retrieves your username and password for the ftp server. right now it assumes that the length of the host is 24 characters, the username is 8 characters and the password is 9 characters. Obviously this is the incorrect way of doing it since the length varies. So what im trying to make it do is just stop reading the data when it gets to </ . My code so far is
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
#include <fstream>
#include <iostream>
#include <string>
#include <Shlobj.h>
using namespace std;
int main()
{
	char homedirectory[MAX_PATH];
	string filelocation;
	char host[25];
	char username[9];
	char password[10];

	//Retrieves Users Home Directory
	SHGetFolderPathA(NULL,CSIDL_PROFILE,NULL,0,homedirectory);
	fstream filestr;
	filelocation=homedirectory;
	filelocation+="/AppData/Roaming/FileZilla/sitemanager.xml";
	filestr.open(filelocation.c_str(),fstream::in);
	
	//Host Is 123 Characters In
	filestr.seekg(123);
	filestr.read(host,24);
	cout<<"Host: "<<host<<endl;
	delete[] host;

	//Username Is 267 Characters In If The Host Is 24 Characters
	filestr.seekg(267);
	filestr.read(username,8);
	cout<<"Username: "<<username<<endl;
	delete[] username;

	//Password Is 302 Characters In If Host Is 24 Characters And The Username Is 8 Characters
	filestr.seekg(302);
	filestr.read(password,9);
	cout<<"Password: "<<password<<endl;
	cin.ignore();
	return 0;
}


Edit: I forgot to say the data is stored in an xml file
Last edited on
Would it be fair to say that all your data needs to be extracted from a construct such as this:


<tagname>info to be found</tagname>


?

To be honest I think your going to have to write a simple parser yourself or use a proper XML parser, or use regular expressions.

To write a simple parser yourself you will need to use the functions I mentionned in my previous post.

1
2
3
4
5
char ch;
file.get(ch); // read a single char

std::string data;
std::getline(file, data, '<'); // read up to specific char 


And add whatever if() statements are required.

I would probably write a function to read up to the beginning of a tag, one to read the tag and another to read the data between the tag and its end tag.

Something like:

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
std::ifstream ifs("file.xml");

// position the read pointer to the beginning of a tag
std::istream& read_to_start_tag(std::istream& is)
{
    while(is.peek() != '<') { is.ignore(); }
    return is;
}

// You could use this to read the data from a tag
std::string data;
std::getline(is, data, '>');

// test the name of the tag
const std::string password_tag_name = "password";

if(data.substr(0, password_tag_name.length()) == password_tag_name)
{
   // you read the password tag
}

// Then if the tag name is the one you want read the info between this tag and the next one:

std::string value;
std::getline(is, data, '<');


Those are just fairly crude examples. You can tailor how you scan through the XML file to fit its particular format and grab what you want.

You might be better served, though, spending some time learning to use an proper XML parser that can do this kind of stuff very easily.
Topic archived. No new replies allowed.