Creating a string from a vector string..

Ok so I have a vector called listOfFiles that contains plenty of file names as strings. I randomly grab one of them with using a random number generator

listOfFiles[rng]

this will contain a filename such as "hello.txt" I want to access the second letter in the string of "hello.txt" and store it in a new string.

1
2
3
4
5
stringstream ss;
string newstring;

ss << listOfFiles[rng][1];
ss >> newstring;


So this stores the letter "e" in the newstring which is perfect.

I now want to make new string contain the second letter from a differernt file name in the vector.

1
2
ss << listOfFiles[rng2][1];
ss >> newstring;


and lets say for example listOfFiles[rng2] contains the string "big.txt".

newstring should in theory contain "ei" but I am obviously doing something wrong, any ideas?!

tried the following:

1
2
3
4
ss << listOfFiles[rng][1];
ss >> newstring;
ss << listOfFiles[rng2][1];
ss >> newstring;


and also

1
2
3
4
ss << listOfFiles[rng][1];
ss >> newstring[0];
ss << listOfFiles[rng2][1];
ss >> newstring[1];




Last edited on
1. Are you sure that each name has at least two characters?

2. Strings do already have characters. The use of stringstream is an overkill.

3. Operator >> replaces value. You could read into two different strings and then append/concatenate. However,

4. http://www.cplusplus.com/reference/string/string/operator+=/ the characters directly.
So this stores the letter "e" in the newstring which is perfect.

I now want to make new string contain the second letter


Why?

You should store characters into char not string.
As keskiverto said, stringstream is over the top here as you're just working with various strings.

But this works ok (newstring ends up as "eb")

1
2
3
4
5
6
7
8
9
10
11
12
    vector<string> listOfFiles;
    listOfFiles.push_back("hello.txt");
    listOfFiles.push_back("big.txt");

    stringstream ss;
    string newstring;

    ss << listOfFiles[0][1];
    ss << listOfFiles[1][0];
    ss >> newstring;

    cout << newstring << endl;


so I guess you're doing something differently.

Are you extracting the first char immediately after inserting it, and then trying to continue? Like

1
2
3
4
    ss << listOfFiles[0][1];
    ss >> newstring;
    ss << listOfFiles[1][0];
    ss >> newstring;


e


Here the first two lines behave ok. But note that because 'e' is the last character in the stream (well, it's the only one), then the call on line 2 of this last fragment causes the eof flag to be set on the stream which means that it doesn't respond to the subsequent input and output calls.

Adding a clear() call to reset the eof flag

1
2
3
4
5
    ss << listOfFiles[0][1];
    ss >> newstring;
    ss.clear(); // reset flags
    ss << listOfFiles[1][0];
    ss >> newstring;


changes the output to

b


this is because the extraction of 'e' also moved the stream position, which determines which char will be extracted next. As you've already extracted 'e', the next char available is the 'b'. But if we set the positions (seek it back) to the start of the stream, then you get "eb" as you wanted.

1
2
3
4
5
6
    ss << listOfFiles[0][1];
    ss >> newstring;
    ss.clear(); // reset flags
    ss.seekg(0); // seek back to start of stream
    ss << listOfFiles[1][0];
    ss >> newstring;


eb


but this code is far messier than

1
2
    newstring += listOfFiles[0][1];
    newstring += listOfFiles[1][0];


Andy

PS @aggsyb could you correct your tagging in your opening post so the fragments make more sense. Thanks!
Last edited on
Thanks for the replies! I had a taxi waiting for me outside midway through the OP so had to rush it! , code tags have been fixed.

listOfFiles holds all the file names and every filename is at minimum 2 characters each.

I want to create an array made up of the second characters in every file name it picks and I figured a string (as its just a char array) is the easiest way to do it.

So based on the example I gave above I would want new string to be an array that contains:

newstring = "e,i,o" //grabbed automatically and placed here though

hello , big , next file is "howdy.txt" 'o' gets added.

Sorry that the first post was messy and unclear.
Last edited on
Topic archived. No new replies allowed.