loading a string from file

A

Allan Bruce

I want to load config from a file. I know how to load in numbers using an
ifstream, but I cant get it to load a string. Can somebody tell me how to
load a string from file? Here is my code:


int mNumServers;
short *mPorts;
std::string *mServers;
std::string *mPaths;

std::ifstream infile(HST_SERVERS_FILE);
infile >> mNumServers;
AllocServersMem();
for (int i=0; i<mNumServers; i++)
{
std::string temp;
// XXX how do I load a string easily?
//infile >> mServers;
//infile >> mPaths;
infile >> mPorts;
}
infile.close();


I get an error saying that:
error C2679: binary '>>' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)

Thanks
Allan
 
K

Kevin Goodsell

Allan said:
I want to load config from a file. I know how to load in numbers using an
ifstream, but I cant get it to load a string. Can somebody tell me how to
load a string from file? Here is my code:


int mNumServers;
short *mPorts;
std::string *mServers;
std::string *mPaths;

std::ifstream infile(HST_SERVERS_FILE);
infile >> mNumServers;
AllocServersMem();
for (int i=0; i<mNumServers; i++)
{
std::string temp;
// XXX how do I load a string easily?
//infile >> mServers;
//infile >> mPaths;
infile >> mPorts;
}
infile.close();


I get an error saying that:
error C2679: binary '>>' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)


Give us a minimal complete program that demonstrates the problem. As it
is, I don't believe you've provided sufficient information to diagnose
the problem.

-Kevin
 
A

Allan Bruce

Kevin Goodsell said:
Allan said:
I want to load config from a file. I know how to load in numbers using an
ifstream, but I cant get it to load a string. Can somebody tell me how to
load a string from file? Here is my code:


int mNumServers;
short *mPorts;
std::string *mServers;
std::string *mPaths;

std::ifstream infile(HST_SERVERS_FILE);
infile >> mNumServers;
AllocServersMem();
for (int i=0; i<mNumServers; i++)
{
std::string temp;
// XXX how do I load a string easily?
//infile >> mServers;
//infile >> mPaths;
infile >> mPorts;
}
infile.close();


I get an error saying that:
error C2679: binary '>>' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)


Give us a minimal complete program that demonstrates the problem. As it
is, I don't believe you've provided sufficient information to diagnose
the problem.

-Kevin


Here is my minumum program and the file I would wish to load:
Thanks
Allan




// LoadServers.cpp
#include <fstream>

#define HST_SERVERS_FILE "servers.dat"

int main()
{
int mNumServers;
short mPorts;
std::string mServers;
std::string mPaths;

std::ifstream infile(HST_SERVERS_FILE);
infile >> mNumServers;

std::string temp;
// XXX how do I load a string easily?
infile >> mServers;
infile >> mPaths;
infile >> mPorts;

infile.close();

return 0;
}



// file to load - servers.dat
1
www.erg.abdn.ac.uk /servlet/WinGalagaHST 80
 
K

Karl Heinz Buchegger

Make it a habit of yours to include *all* headers you
need for your program (but not more).

Your program uses std::string, thus you ought to

#include <string>

Which also solves your problem.
 
A

Allan Bruce

Karl Heinz Buchegger said:
Make it a habit of yours to include *all* headers you
need for your program (but not more).

Your program uses std::string, thus you ought to

#include <string>

Which also solves your problem.

Thanks, I thought I was close. What a stupid mistake!
Allan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top