(unsolved) iostreams. Reading strings separated by spaces and returns AND SLASHES.
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]
example.txt
string_at_a_time.cpp
line_at_a_time.cpp
I like pie.C:\DOSC:\DOS\RUNRUN\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"}
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; }
I've never done it myself, but this recomends using find_first_of(...). It seems straight forward enough. Just make your separator="\\ \n".
CM
CM
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
Many thanks in advance!
Adriaan
Just read the file into a string, and parse it with find_first_of. If you need more advanced parsing, you can use a stringstream object as well.
CM
CM
I think he wants to not have to do this in two steps.
Is this helpful? :) (This page was the second hit ;\ My new [google] technique is unstoppable!)
(Edit: Be sure to read the following section of that page too. char *'s are evil.)
Is this helpful? :) (This page was the second hit ;\ My new [google] technique is unstoppable!)
(Edit: Be sure to read the following section of that page too. char *'s are evil.)
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 argument 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 ambiguous 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???
/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).
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement