Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Actually passing an iterator also wont do. I have vectors of different struct types. A "space separated" string is stored in one of the struct variables (all of type string) in each vector. I have to remove spaces from the "space separated" string and replace it with a comma to make it a csv string but except at one place i-e I want to keep that substring still a "space separated" string and I have also stored that "space separated" string separately in one of the struct elements.
e-g
Code:
element-1"space separated" string part of the string: "abc 123" which is for example the fifth/sixth substring in the example below
element-2 of vector-structtype-1: "shnd 23j l94 hsi8 abc 123 md38 fhw0 8fdj"
csv element-2 of vector-structtype-1: "shnd, 23j, l94, hsi8, abc 123, md38 fhw0 8fdj"
element-1 of vector-structtype-2: "space separated" string part of the string: "klm 987" which is for example the fifth/sixth substring in the example below
element-2 of vector-structtype-2: "dnhs j32 49l 8ish klm 987 83dm 0wfh jdf8"
csv element-2 of vector-structtype-2: "dnhs, j32, 49l, 8ish, klm 987, 83dm, 0wfh, jdf8"
The code below works fine but I dont know how to keep that specific substring still a "space separated" string.
Code:
void liffe_breach::CSVline(int Count, int ordIDno, string str) // I want this str to be an elements of a specific vector. Is it possible?
{ // because vectors are of different (struct) types
std::stringstream Brchno;
Brchno << Count;
std::stringstream IDno;
IDno << ordIDno;
stringstream ss(str);
string word;
string cvsLine = "";
while ( ss >> word )
{
cvsLine = cvsLine + word + ",";
}
}
You can temporarily replace the spaces you don't want to transform into another character (for example "_"), run your program, and then re-insert the space back in:
EDIT: You know, you wouldn't be having all these problems if you were actually modeling your data, instead of always trying to do string operations.
EDIT2: perl (and regexes in general) is much better suited for this task. This is a 3-line perl script.
Last edited by monarch_dodra; February 23rd, 2011 at 05:03 AM.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Bookmarks