Jump to content
  • Advertisement
Sign in to follow this  
the_cyberlord

(unsolved) iostreams. Reading strings separated by spaces and returns AND SLASHES.

This topic is 4997 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can you read strings, for example into an array, separated by spaces,returns and slashes from a file, with iostreams? Thanks in advance, Adriaan [Edited by - the_cyberlord on November 13, 2004 10:22:10 AM]

Share this post


Link to post
Share on other sites
Advertisement
example.txt
I like pie.
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN


string_at_a_time.cpp

#include <fstream>
#include <vector>
#include <string>

std::vector< std::string > results;
std::ifstream file;

int main ( void )
{
file.open("example.txt"); //note: example.txt must be in the current working directory for this to work, which may not happen in your IDE environment
while (file.good())
{
std::string temp;
file >> temp;
results.push_back(temp);
}
//results[0] == "I"
//results[1] == "like"
//results[2] == "pie."
//results[3] == "C:\\DOS"
//results[4] == "C:\\DOS\\RUN"
//results[5] == "RUN\\DOS\\RUN"
}


line_at_a_time.cpp

#include <fstream>
#include <vector>
#include <string>

std::vector< std::string > results;
std::ifstream file;

int main ( void )
{
file.open("example.txt"); //note: example.txt must be in the current working directory for this to work, which may not happen in your IDE environment
while (file.good())
{
std::string temp;
readline(file,temp);
results.push_back(temp);
}
//results[0] == "I like pie."
//results[1] == "C:\\DOS"
//results[2] == "C:\\DOS\\RUN"
//results[3] == "RUN\\DOS\\RUN"
}

Share this post


Link to post
Share on other sites
I managed to read strings by myself, but I need to separate in slashes too...


ifstream f_in;
f_in.open("man1.obj",ios_base::in);
while (!f_in.eof())
{
string f;
f_in >> f;
cout << f << endl;
}

Share this post


Link to post
Share on other sites
In fact, that doesn't solve my question, because it parses strings. I mean, if a file iostream can get strings separated by spaces and newlines, why isn't it posible to just add one there, and also make it separate on slashes?

Many thanks in advance!
Adriaan

Share this post


Link to post
Share on other sites

ifstream inf("man1.obj");
string name;
while(!getline(inf, name, '|').eof())
{
string inputstring;
getline(inf, inputstring, '/n');

}

this code gives these errors:

C:\Documents and Settings\Lord Myth\My Documents\3D\main.cpp(42) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : could not deduce template ar
gument for 'class std::basic_istream<_E,_Tr> &' from 'class std::basic_ifstream<char,struct std::char_traits<char> >'
C:\Documents and Settings\Lord Myth\My Documents\3D\main.cpp(42) : error C2782: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : template parameter '_E' is a
mbiguous
could be 'int'
or 'char'
C:\Documents and Settings\Lord Myth\My Documents\3D\main.cpp(42) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : expects 2 arguments - 3 provided
c:\microsoft visual studio\vc98\include\string(145) : see declaration of 'getline'


And BTW, you can't enter multiple separators like McCloud said, it gives an error. So what do I do???

Share this post


Link to post
Share on other sites
/n should be \n; as is, you're giving two characters - a forward slash and a lowercase n - which as you've already figured out is non-kosher.

If you have control over the file format, you might want to redesign it so that you don't have to worry about having several different separators. Otherwise you are stuck with either parsing things twice (once to read lines and again to split up individual lines using stringstreams), or reading a character at a time (and checking each time for each separating character; i.e. doing it all manually).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!