473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

loading a string from file

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[i];
//infile >> mPaths[i];
infile >> mPorts[i];
}
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
Jul 22 '05 #1
4 2921
Allan Bruce wrote:
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[i];
//infile >> mPaths[i];
infile >> mPorts[i];
}
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:uU*****************@newsread2.news.pas.earthl ink.net...
Allan Bruce wrote:
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[i];
//infile >> mPaths[i];
infile >> mPorts[i];
}
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
Jul 22 '05 #3
Allan Bruce wrote:


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
Allan Bruce wrote:


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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris | last post by:
I have seen the posts on various places on the internet about .NET framework mismatch issues and I don't think that is my problem. ; ) When I execute the following C++.NET code: String...
2
by: Foehammer | last post by:
Hello, I'm trying to load an assembly dynamically using an app domain. This is a proof-of-concept for a larger project, so please excuse the lame class names. TestLib is the dll where all the...
1
by: Brian | last post by:
I've looked through the previous posts on this one and have verified permissions and location of my Dlls, but I am still getting an exception when I try to Load an assembly. The directory where...
3
by: Holmes | last post by:
Hello Ran into a bit of a problem here and have now exhausted my resources to getting this working What I am trying to do is load and show a simple vb form with a listbox in it Dim...
0
by: Terry Olsen | last post by:
I wrote the following code to load a DataTable with a CSV file. My problem is: If I include the line "MyTable.Clear()" before I start loading the Table, I'm presented with the 2nd line of my CSV...
1
by: Bala | last post by:
Hi, Currently i am loading the file names into datagrid (unbound) from a folder(sub folders too). so for this i am using this below code. but its too slow to loading all the file names. is it...
0
by: speedcoder | last post by:
hi all, i'm stumped. my applet used to load images over the network. (it was actually designed by someone else.) yes, the applet used to load each image file independently over the network and...
2
by: brianflannery | last post by:
Greetings! My situation is this. I currently have a database of which in a form displays jpeg pictures (one at a time) which are linked to the db and stored in a separate folder. I have set up a...
3
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type...
5
by: s0suk3 | last post by:
Hi, I wanted to know how cautious it is to do something like: f = file("filename", "rb") f.read() for a possibly huge file. When calling f.read(), and not doing anything with the return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.